window.load doesn't fire always on chrome?

前端 未结 2 1847
小鲜肉
小鲜肉 2020-12-11 09:34

I have this userscript:

// ==UserScript==
// @name          test
// @namespace     test
// @description   show\'s an alert on load
// @include       http://*         


        
相关标签:
2条回答
  • 2020-12-11 10:14

    This call may be being loaded too late on the page. Add this code to the very top of the page and see if that fixes it.

    If that isn't it please post some additional information. (Where the call is being made? Some more information on the page? Is this being called from an external .js file?)

    0 讨论(0)
  • 2020-12-11 10:21

    The cause of the problem is that the Content script is sometimes executed when the document has already fully loaded. This is illustrated using the following Content script ("User Script"):

    // ==UserScript==
    // @name          Test
    // @namespace     Test
    // @include       *
    // ==/UserScript==
    
    window.addEventListener('load', function(){alert("loaded");}, false);
    alert(document.readyState);   // When this prints "complete", onload never runs
    

    To solve the issue, use the @run-at document-start meta-rule, which causes the script to execute before the document is created:

    ...
    // @run-at   document-start
    // ==/UserScript==
    
    0 讨论(0)
提交回复
热议问题