使用Greasemonkey 2.0绑定到Firefox 30中unsafeWindow的事件

Binding to an event of the unsafeWindow in Firefox 30 with Greasemonkey 2.0

本文关键字:unsafeWindow 事件 Firefox Greasemonkey 绑定 使用      更新时间:2023-09-26

我正在维护一个Greasemonkey脚本,由于Mozilla在Firefox30中更改了unsafeWindow API,我遇到了一些麻烦。

我的脚本运行的页面会触发一个事件"MyEvent",我的脚本对该事件感兴趣。

使用jQuery 1.6.4 触发事件

以前,我用这个代码钩住这个事件:

var jQuery = unsafeWindow.jQuery;
jQuery(unsafeWindow.document)
    .bind("MyEvent", function() {
        console.log("MyEvent Triggered!");
    });

但由于Mozilla的改变,这将不再有效。

我试图在无冲突模式下插入我自己的jQuery,但我认为这不能访问由其他jQuery实例触发的事件?

有什么想法可以让我参与这个活动吗?

如果您不需要任何GM_函数并且您也不需要@require自己的jQuery,那么快速而肮脏的方法就是使用@grant none模式。这项工作:

// ==UserScript==
// @name     _unsafeWindow tests
// @include  http://jsbin.com/xaman/*
// @grant    none
// ==/UserScript==
var jQuery = window.jQuery;
jQuery(document).bind ("MyEvent", function () {
    console.log ("From GM script: MyEvent caught!");
} );

如果确实需要GM_函数,有时可以使用新的exportFunction()
不幸的是,jQuery和jQuery事件处理是一种特殊情况。根据您的尝试,您将收到以下错误消息:

访问属性"handler"的权限被拒绝

CloneNonReflectorsWrite错误

我只是发现使用任何新的unsafeWindow功能都无法做到这一点。你唯一的办法就是注入代码。像这样:

// ==UserScript==
// @name     _unsafeWindow tests
// @include  http://jsbin.com/xaman/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==
/*- The @grant directive is needed to work around a design change
    introduced in GM 1.0.   It restores the sandbox.
*/
function myEventHandler (zEvent) {
    console.log (
        'From GM script: "' + zEvent.type + '" triggered on ', zEvent.target
    );
}
function bindMyEvent () {
    //-- Gets "jQuery is not defined" if GM script does not also use jQuery.
    jQuery(document).bind ("MyEvent", myEventHandler);
    console.log ("The jQuery version being used is: ", jQuery.fn.jquery);
}
//-- Create A COPY OF myEventHandler in the target page scope:
addJS_Node (myEventHandler);
//-- Create A COPY OF bindMyEvent in the target page scope and immediately run it.
addJS_Node (null, null, bindMyEvent);
function addJS_Node (text, s_URL, funcToRun, runOnLoad) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    if (runOnLoad) {
        scriptNode.addEventListener ("load", runOnLoad, false);
    }
    scriptNode.type                         = "text/javascript";
    if (text)       scriptNode.textContent  = text;
    if (s_URL)      scriptNode.src          = s_URL;
    if (funcToRun)  scriptNode.textContent  = '(' + funcToRun.toString() + ')()';
    var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
    targ.appendChild (scriptNode);
}

您可以针对这个jsBin页面测试这两个脚本


如果需要从注入的事件处理程序中运行/调用GM_函数,请使用"如何从必须在目标页面范围内运行的代码中调用Greasemonkey';s GM_函数?"中所示的技术。

只是不要在unsafeWindow上绑定事件处理程序,而是使用常规的window对象:

window.document.addEventListener("MyEvent", function() {
  console.log("MyEvent Triggered!");
}, false, true);

请注意addEventListener的第四个参数(wantsUntrusted(——这个参数允许您的事件处理程序接收不受信任的事件。