简体   繁体   English

为什么我的ajaxSuccess jQuery事件没有在Greasemonkey中被触发?

[英]Why is my ajaxSuccess jQuery event not being fired in Greasemonkey?

I have a basic greasemonkey script shown below, in which I want to run some code every time an AJAX request is sent by the website I'm running the script on. 我有一个基本的greasemonkey脚本,如下所示,我想在每次运行脚本的网站发送AJAX请求时运行一些代码。

I'm using the ajaxSuccess jQuery handler and it's not being fired. 我正在使用ajaxSuccess jQuery处理程序,它没有被触发。 I suspect this might be because the AJAX requests are being sent with the global flag set to false (see http://docs.jquery.com/Ajax_Events ). 我怀疑这可能是因为AJAX请求是在全局标志设置为false的情况下发送的(请参阅http://docs.jquery.com/Ajax_Events )。 Is there some better way to achieve this that will work even when global is set to false? 有没有更好的方法来实现这一点,即使全局被设置为假也会有效?

Code: 码:

// ==UserScript==
// @name           MyTestScript
// @require        http://code.jquery.com/jquery-1.7.1.min.js
// @namespace      NRA
// @include        http://www.mysamplewebsite.com/view/*
// ==/UserScript==

$(document).ajaxSuccess(function(e, xhr) {
    alert("ajax success hit!");
    // I will do my other handling here
});

Thanks 谢谢

The jQuery in your userscript runs in a separate environment from the page's jQuery . 用户脚本中的jQuery在与页面jQuery不同的环境中运行

You need to intercept the page's AJAX calls so you can use either (A) unsafeWindow or (B) Inject your script. 您需要拦截页面的 AJAX调用,以便可以使用(A) unsafeWindow或(B)注入脚本。

(A) unsafeWindow looks like: (一) unsafeWindow看起来像:

unsafeWindow.$(document).ajaxSuccess(function(e, xhr) {
    alert("ajax success hit!");
    // I will do my other handling here
});


(B) Script injection looks like: (B)脚本注入看起来像:

function scriptWrapper () {

    //--- Intercept Ajax
    $('body').ajaxSuccess (
        function (event, requestData) {
            alert ("ajax success hit!");
            doStuffWithAjax (requestData);
        }
    );

    function doStuffWithAjax (requestData) {
        console.log ('doStuffWithAjax: ', requestData.responseText);
    }

    //--- DO YOUR OTHER STUFF HERE.
    console.log ('Doing stuff outside Ajax.');
}

function addJS_Node (text, s_URL, funcToRun) {
    var D                                   = document;
    var scriptNode                          = D.createElement ('script');
    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);
}

addJS_Node (null, null, scriptWrapper);


Note that in both cases, you must be conscious that data does not flow from page scope back to GM scope easily -- be careful about mixing the two . 请注意,在这两种情况下,您都必须意识到数据不会轻易地从页面范围流回GM范围-小心地将两者混合

One workaround, for transmitting data across the sandbox, can be found in this answer . 这个答案中可以找到一种解决方法,用于在沙箱中传输数据。

It could have something to do with the greasemonkey sandboxing. 它可能与greasemonkey沙盒有关。

http://greasemonkey.mozdev.org/authoring.html http://greasemonkey.mozdev.org/authoring.html

Sometimes, you will want to access the global variables in the content document. 有时,您需要访问内容文档中的全局变量。 For example, maybe the content document defines a function which you want to call. 例如,内容文档可能定义了您要调用的函数。 In these cases, you can access the content document's global scope with the unsafeWindow variable. 在这些情况下,您可以使用unsafeWindow变量访问内容文档的全局范围。 Just be aware that accessing the members of this variable means that content scripts can detect your script and potentially interfere with it if they choose. 请注意,访问此变量的成员意味着内容脚本可以检测到您的脚本,如果他们选择,可能会干扰它。

Ie document in the greasemonkey script may not be the same as document in the actual web page, so you might have to use unsafeWindow.document or something. 即,油脂猴子脚本中的document可能与实际网页中的document ,因此您可能必须使用unsafeWindow.document或其他内容。

Or maybe jQuery's ajax methods simply don't work in greasemonkey without alteration (requiring special override of XHR objects)... 或者也许jQuery的ajax方法根本不能在greasemonkey中工作而不需要改动(需要特殊覆盖XHR对象)......

Eg http://www.monperrus.net/martin/greasemonkey+jquery+and+xmlhttprequest+together 例如http://www.monperrus.net/martin/greasemonkey+jquery+and+xmlhttprequest+together

and http://ryangreenberg.com/archives/2010/03/greasemonkey_jquery.php http://ryangreenberg.com/archives/2010/03/greasemonkey_jquery.php

... JQuery AJAX in Greasemonkey then looks like: ... Greasemonkey中的JQuery AJAX如下所示:

$.ajax({
  url: '/p/',// this even works for cross-domain requests by default
  xhr: function(){return new GM_XHR();},
  type: 'POST',
  success: function(val){
  .... 
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM