我正在尝试编写一个 greasemonkey 脚本,如果页面显示 404 错误,它会在 firefox 中进行检查。如果是这样,脚本应该重新加载页面。脚本看起来像这样:
// ==UserScript==
// @name 404-reloader
// @namespace None
// @include *
// @version 1
// ==/UserScript==
url = "http://www.example.com";
if (document.getElementById("errorPageContainer"))
location.href = url;
我的问题是,它只是有时有效。为了测试它,我关闭了我的无线适配器并加载了一个页面。该脚本本身没有,所以我手动启动了它。我第一次启动它时,脚本运行并更改为 example.com,在那里我遇到了另一个未找到的错误,因为适配器仍处于关闭状态。我再次启动脚本并且它起作用了。第三次之后,我在脚本中遇到了异常:
/*
Exception: Permission denied to access property 'document'
@Scratchpad/2:10
*/
我发现当我更改 URL 时,脚本会运行一次。之后我得到这个异常(exception)。 当脚本不起作用时,我将它复制到 Firebug 控制台并运行它。它在那里工作,但仍然没有在 greasemonkey 脚本中。
现在我想知道为什么 greasemonkey 会这样,以及我如何处理异常问题以及 Greasemonkey 在 404 页面上不自行运行的问题。
我已经搜索了答案,但没有找到任何对我的问题有用的东西。
谢谢你的帮助
最佳答案
我认为 GM 不会在错误页面上运行实际上是一个功能,无论如何,这对我有用:
当您在 document-start
运行并为 DOMContentLoaded
添加监听器时,它不是默认在 document-end
运行,它即使在重新加载时也适合我。请注意,尽管 @run-at
是特定于 GM (>=1.0) 的,但据我所知目前还不能移植到 Chrome/Opera。
// ==UserScript==
// @name test123
// @namespace test123
// @version 1
// @include *
// @grant none
// @run-at document-start
// ==/UserScript==
document.addEventListener("DOMContentLoaded", function () {
console.log(document.getElementById('errorPageContainer'));
});
关于javascript - 如何处理 404 页面上的权限被拒绝异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17341159/