我有一个用于Firefox的greasemonkey脚本,昨天它运行得很好.我今天尝试使用它(没有代码被修改),我注意到它停止工作.经过进一步检查,脚本现在抛出以下错误:
Error: Permission denied to access property 'handler'
Run Code Online (Sandbox Code Playgroud)
以下代码块中抛出此错误:
$('body').click(function() {
// code here
});
Run Code Online (Sandbox Code Playgroud)
这个错误神奇地开始发生在今天脚本工作正常昨天.我不明白为什么只是尝试做一些基本的事情,例如在jQuery中添加事件处理程序时发生这种错误.
我的脚本使用的jQuery已经在脚本执行的页面中使用,因此我使用此代码使GM可以访问:
var $ = unsafeWindow.jQuery;
Run Code Online (Sandbox Code Playgroud)
如果需要,可供参考,以下是我在脚本中使用的以下Greasemonkey函数:
// @grant GM_getResourceText
// @grant GM_addStyle
// @grant GM_xmlhttpRequest
// @grant GM_getResourceURL
Run Code Online (Sandbox Code Playgroud)
我试过研究这个错误,我找不到任何答案.所有看起来可能有用的问题都涉及iframe,并且在我的代码或运行的网站中找不到单个iframe.我也尝试删除并重新安装脚本,但没有解决问题.
假设我正在使用以下网页:
<html>
<body>
<span id="click">click me</span>
<script>
var hello = function() {
alert('hello');
}
document.getElementById('click').addEventListener('click', function(e) {
hello();
});
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的Greasemonkey脚本是:
// ==UserScript==
// @name My Script
// @include http://example.com/hello.html
// @version 1
// @grant none
// ==/UserScript==
window.hello = function() {
alert('goodbye');
}
Run Code Online (Sandbox Code Playgroud)
禁用Greasemonkey脚本后,单击#click页面上的元素将显示"hello"警报.启用脚本后,单击该元素将显示"再见"警报.
很简单.在hello从网页的功能正在由Greasemonkey的脚本功能所取代.
现在让我们说我想使用Greasemonkey API.当我将@grant值设置为除'none'之外的有效值(例如// @grant GM_setClipboard)[导致Greasemonkey将脚本作为"内容脚本"运行时,而不是像'none'那样在页面的范围内运行),Greasemonkey脚本无法工作.
window.hello 不再定位页面上的正确对象.
更换window.hello用unsafeWindow.hello看起来像它的工作,而是下面的错误是在JS控制台抛出:
错误:拒绝访问对象的权限
如何在@grant GM_setClipboard设置目标并替换hello页面上的原始函数时重写Greasemonkey脚本?
系统信息: