我有一个适用于 Firefox 的 greasemonkey 脚本,昨天运行良好。我今天尝试使用它(没有修改代码),我注意到它停止工作了。经过进一步检查,脚本现在抛出以下错误:
Error: Permission denied to access property 'handler'
此错误在以下代码块中引发:
$('body').click(function() {
// code here
});
当脚本昨天运行良好时,今天神奇地开始发生此错误。我不明白为什么在尝试做一些如此基本的事情时会发生此错误,例如在 jQuery 中添加事件处理程序。
我的脚本使用了 jQuery,它已经在脚本执行的页面中使用,所以我使用这段代码让 GM 可以访问它:
var $ = unsafeWindow.jQuery;
作为引用,如果需要,这里是我在脚本中使用的以下 Greasemonkey 函数:
// @grant GM_getResourceText
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// @grant GM_getResourceURL
我已尝试研究此错误,但找不到任何答案。所有看起来可能有用的问题都涉及 iframe,并且在我的代码或其运行的网站中找不到一个 iframe。我还尝试删除并重新安装脚本,但未能解决问题。
最佳答案
Greasemonkey 2.0 刚刚被推送到所有设置为自动更新的 Firefox 浏览器。 (GM 2 于 2014 年 6 月 17 日发布,但可能需要几周时间才能通过审核过程。)
Greasemonkey 2.0 radically changed unsafeWindow
handling :
Backwards incompatible changes:
- For stability, reliability, and security the privileged sandbox has been updated to match the new changes to unsafeWindow for the Add-on SDK. In order to write values to unsafeWindow you will need to use the new methods
cloneInto()
,exportFunction()
, and/orcreateObjectIn()
.- The
@grant none
mode is now the default, and grants will no longer be implied when not explicitly provided. See the post Sandbox API Changes in Greasemonkey 2.0 for more detail.
通常,要现场访问页面函数或变量,您可以切换到新方法,但在您的情况下,您使用的是 var $ = unsafeWindow.jQuery;
——这始终是不好的做法。
jQuery is a special case来回克隆它会破坏东西。
@require
改为 jQuery,EG:
// ==UserScript==
// @name _YOUR_SCRIPT_NAME
// @include http://YOUR_SERVER.COM/YOUR_PATH/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant GM_getResourceText
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// @grant GM_getResourceURL
// ==/UserScript==
...
关于javascript - 错误 : Permission denied to access property 'handler' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24719256/