且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在浏览器中禁用JS时,setTimeout在Greasemonkey用户脚本中不起作用

更新时间:2022-12-18 09:28:44

即使Greasemonkey JavaScript以提升的特权运行,如Pointy所说,setTimeout函数也会附加到页面的JavaScript空间中-根据需要包装在闭包中. (在正常操作中,Greasemonkey实例通常在任何已设置的计时器启动时就消失了.)

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.)

因此,如果页面的主要JavaScript被禁用,则计时器将永远不会运行.

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

可能的解决方法:

  • GM_xmlhttpRequest用作粗略延迟.您可以设置一个故意绘制其响应的页面.像这样的代码:

  • 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.

使用NoScript禁用除主页以外的所有页面JavaScript.例如,对于页面 YourSite.com/testpage.htm ,其中包括来自 * SpamGenerator.net ...的脚本 YourSite.com 中的脚本,但阻止它们来自 SpamGenerator.net .

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.