English 中文(简体)
在每页页载量上运行我的脚本, *包括* AJAX页载量?
原标题:Run my script on each page load, *including* AJAX page-loads?

我想推迟一个特定网页(这里指的是Google)的页面浏览时间, 以便用户在倒计时计时完成之前无法查看网页。

这个问题受到启发"http://xkcd.com/862/" rel="nofollown noreferrer">>xkcd ,类似的问题是"https://stackoverflow.com/ questions/5043233/javascript-page-load-delay-flue-of-com-set-pages" >“特定页面的积页延迟”

我尝试过一个修改版本的 Jonathan s Greasemonkey 脚本(见下文),

如果Google在一个新标签中打开,或者用户从Google的链接中打开,然后返回,脚本又会再次打开。 但是,如果用户从不远离Google(比如,他们在每个搜索结果的简短摘要中找到了他们所寻找的答案,然后只是搜索其它东西),他们可以毫不迟延地搜索。

是否有办法迫使延迟屏幕在每次搜索后出现(而不是每次访问页面后出现)? -- -- 最好使用Greasemonkey或铬插件?

Script currently used:
(first sets blocked addresses to a value of "1" and all other addresses to a value of "0," then, if block>0, the script kicks in...)

(function(){
    // Note: This doesn t actually stop the page from loading, but hides it, so you know its 
    // there, waiting; The dopamine of internet candy becomes a torture.  Better to clean 
    // your room or open an irb prompt instead.
    window.seconds = 30;

    function resetCountDown()
    {
        seconds = 30;
    }

    // You can has cybersauce
    window.clearDelay = function()
    {
        document.getElementById( eightSixTwoDelay ).style.display =  none ;
    }

    var overlay = document.createElement( div );
    overlay.id =  eightSixTwoDelay ;
    overlay.style.backgroundColor =  #000 ;
    overlay.style.color =  #FFF ;
    overlay.style.fontSize =  56px ;
    overlay.style.fontFamily =  Helvetica, Arial, Sans ;
    overlay.style.fontWeight =  bold ;
    overlay.style.textDecoration =  none ;
    overlay.style.position =  absolute ;
    overlay.style.top =  0px ;
    overlay.style.left =  0px ;
    overlay.style.width =  100% ;
    // clientHeight changes as content loads, and JS, like the PHX Valley Metro system, does not wait for you to run.
    overlay.style.height = document.body.clientHeight +  px ; // 100% ; 
    overlay.style.paddingTop =  10px ;
    overlay.style.paddingLeft =  10px ;
    overlay.style.textAlign =  left ;
    overlay.style.zIndex =  10000 ; // OVER 9000

    overlay.addEventListener("click", resetCountDown, true); // THERE IS NO ESCAPE

    document.getElementsByTagName( body )[0].appendChild(overlay);

    window.displayDelay = function()
    {
        if (seconds > -1)
        {
            document.getElementById( eightSixTwoDelay ).innerHTML =  Page ready in   + seconds +   seconds. ;
            seconds -= 1;
            setTimeout(window.displayDelay, 1000);
        }
        else
        {
            clearDelay();
        }
    }


    window.onload = displayDelay();

})();
}
问题回答

当输入新的搜索时, Google 礼貌地更改了 URL 和新页面中的 AJAXing 。 所以, 倾听 < code> hashchange 事件, 以确定新搜索的运行时间 。

该脚本上的一些随机笔记 :

  1. Use @run-at document-start so that the blanking starts as soon as possible.
  2. The overlay should be position: fixed;.
  3. Avoid setting global or window. scope variables, AMAP.

以下是一个完整的脚本, 空白页面在每次新的 Google 搜索 :

// ==UserScript==
// @name        _Delay a page display, a la XKCD
// @namespace   _pc
// @match       http://*.google.com/*
// @run-at      document-start
// ==/UserScript==

/*--- Blank the screen as soon as the DOM is available, and also whenever
    the URL (hashtag) changes -- which happens when "new" pages are loaded
    via AJAX.
*/
window.addEventListener ("readystatechange",    FireWhenReady,          true);
window.addEventListener ("hashchange",          blankScreenForA_While,  true);

function FireWhenReady () {
    this.fired  = this.fired || false;

    if (    document.readyState != "uninitialized"
        &&  document.readyState != "loading"
        &&  ! this.fired
    ) {
        this.fired  = true;
        blankScreenForA_While ();
    }
}

function blankScreenForA_While () {
    /*  Note: This doesn t actually stop the page from loading, but hides it,
        so you know its there, waiting; The dopamine of internet candy becomes
        a torture.
        Better to clean your room or open an irb prompt instead.
    */
    //--- Settings:
    var pageDelaySeconds    = 5;
    var overlayID           = "gmEightSixTwoDelay"

    //--- One-time setup, for each new "page", START:
    function resetCountDown () {
        blankScreenForA_While.secondsRemaining  = pageDelaySeconds;
    }
    resetCountDown ();


    function createOverlay () {
        var overlay                 = document.getElementById (overlayID);
        if (overlay) {
            overlay.style.display   =  block ;  // Un-hide.
            return;
        }
        overlay                     = document.createElement ( div );
        overlay.id                  = overlayID;
        overlay.style.cssText       = "                                 
            font:                   bold 56px Helvetica,Arial,Sans;     
            text-decoration:        none;                               
            position:               fixed;                              
            top:                    0;                                  
            left:                   0;                                  
            width:                  100%;                               
            height:                 100%;                               
            z-index:                10000;  /* OVER 9000 */             
            margin:                 0;                                  
            overflow:               hidden;                             
            color:                  pink;                               
            background:             lime;                               
            line-height:            1.5;                                
            padding:                1em;                                
            text-align:             center;                             
        ";

        //--- Only use innerHTML the one time.
        overlay.innerHTML           =  Go do something important!<br> 
                                    +  Page ready in <span></span> seconds. 
                                    ;

        // THERE IS NO ESCAPE.
        overlay.addEventListener( "click", resetCountDown, true);

        document.body.appendChild (overlay);
    }
    createOverlay ();

    //--- One-time setup, for each new "page", :END


    // You can has cybersauce
    function clearDelay () {
        document.getElementById (overlayID).style.display =  none ;
    }


    function displayDelay () {
        if (blankScreenForA_While.secondsRemaining > -1) {
            var displaySpan         = document.querySelector (
                                        "#" + overlayID + " span"
                                    );
            displaySpan.textContent = blankScreenForA_While.secondsRemaining;

            blankScreenForA_While.secondsRemaining--;
            setTimeout (displayDelay, 1000);
        }
        else {
            clearDelay ();
        }
    }
    displayDelay ();

}//-- End of: blankScreenForA_While()




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签