how to run greasemonkey script before the page content is displayed?

前端 未结 3 692
别跟我提以往
别跟我提以往 2020-12-13 14:24

I am writing a plug-in for Firefox and using greasemonkey script to do that (I compile the user script using this tool http://arantius.com/misc/greasemonkey/script-compiler)

3条回答
  •  长情又很酷
    2020-12-13 15:24

    In addition to the previous answers I've found a solution that works flawlessly, just like previous versions of Firefox & GreaseMonkey. First off, run-at must be set to the earliest moment, so document-start:

    // @run-at      document-start
    

    Because the DOM is probably not loaded yet, wait for the document to get a readyState of interactive:

    document.onreadystatechange = function () {
        if (document.readyState === "interactive") {
           // Do something
        }
    }
    

    While Greasemonkey implements more or less the same with document-end, I've noticed that the technique above works faster. Using this has resolved all issues I had in Firefox with flashing / jumping screens on pages when the DOM changes kicked in and directly loads the page as intended with the userscript.

提交回复
热议问题