setTimeout not working in Greasemonkey user script when JS disabled in browser

前端 未结 3 2087
不知归路
不知归路 2021-02-15 23:25

I\'m working on a project that requires my user script be run on pages as they are rendered without executing any of the page\'s JavaScript. That is to say, we need to browse wi

3条回答
  •  醉梦人生
    2021-02-16 00:05

    Even though Greasemonkey JavaScript runs with elevated privileges, as Pointy said, setTimeout functions are appended to the page's JavaScript space -- wrapped in a closure as needed. (In normal operation, the Greasemonkey instance is often gone by the time any timers, it has set, fire.)

    So, if the page's main JavaScript is disabled, the timer will never run.

    Possible workarounds:

    • Use GM_xmlhttpRequest as a crude delay. You can setup a page that deliberately draws out its response. So code like:

      GM_xmlhttpRequest
      (
          {
              method: "GET",
              url:    "http://YourTestServer.com/DelayService.php?Seconds=2",
              onload: function (response) {YourDelayedFunctionHere (); }
          }
      );
      

      Would call a utility page that you set up to do the delay for you.

    • Use NoScript to disable all of the page's JavaScript except for the main page. For example, for page, YourSite.com/testpage.htm, which includes scripts from, say, *SpamGenerator.net... Allow scripts from YourSite.com but block them from SpamGenerator.net.

提交回复
热议问题