本文介绍了使用Greasemonkey 2.0绑定到Firefox 30中的unsafeWindow事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于。



我的脚本运行的页面触发了事件MyEvent和我的脚本对该事件感兴趣。



使用jQuery 1.6.4触发事件



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

但是由于Mozilla的改变,这将不再适用。



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



我有什么想法可以加入这个事件?

解决方案

快速而肮脏的方法, if你不需要任何 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(来自GM脚本:MyEvent捕获!);
});






如果确实需要 GM _ 函数,您有时可以使用。

不幸的是,jQuery和jQuery事件处理是一个特例。根据您的尝试,您将收到如下错误消息:

我只是发现没有办法使用任何新的 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 ==
/ * - 需要@grant指令来解决GM 1.0中引入的设计更改
。它恢复了沙箱。
* /
函数myEventHandler(zEvent){
console.log(
'来自GM脚本:'+ zEvent.type +'触发',zEvent.target
);
}

函数bindMyEvent(){
// - 如果GM脚本也不使用jQuery,则获取jQuery未定义。
jQuery(document).bind(MyEvent,myEventHandler);
console.log(正在使用的jQuery版本是:,jQuery.fn.jquery);
}

// - 在目标页面范围内创建一个myEventHandler的COPY:
addJS_Node(myEventHandler);
// - 在目标页面范围内创建一个COPY的bindMyEvent并立即运行它。
addJS_Node(null,null,bindMyEvent);

函数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);
}






您可以同时测试两者这些脚本针对。






如果你需要在注入的事件处理程序中运行/调用 GM _ 函数,请使用如何调用Greasemonkey的GM_函数来自必须在目标页面范围内运行的代码?。


I'm maintaining a Greasemonkey script and got some trouble due to Mozilla's change to the unsafeWindow API in Firefox 30.

The page my script runs on, triggers an event "MyEvent" and my script is interested in that event.

The event is fired using jQuery 1.6.4

Before, I used this code to hook into this event:

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

But due to Mozilla's change this won’t work anymore.

I tried to insert my own jQuery in conflict-free mode but I don't think this can access events that are triggered by the other jQuery instance?

Any ideas how I could hook into this event?

解决方案

The quick and dirty way to do this, if you don't need any GM_ functions and you don't @require your own jQuery, is to use @grant none mode. This works:

// ==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!");
} );


If you did need GM_ functions, you can sometimes use the new exportFunction().
Unfortunately, jQuery and jQuery event handling is a special case. Depending on what you try, you will get error messages like:

I've simply found no way to do this using any of the new unsafeWindow functionality. Your only recourse is to inject the code. Like so:

// ==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);
}


You can test both of these scripts against this jsBin page.


If you need to run/call GM_ functions from within the injected event handler(s), use techniques shown in "How to call Greasemonkey's GM_ functions from code that must run in the target page scope? ".

这篇关于使用Greasemonkey 2.0绑定到Firefox 30中的unsafeWindow事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 17:43