为什么我的ajaxSuccess jQuery事件没有在Greasemonkey中触发? [英] Why is my ajaxSuccess jQuery event not being fired in Greasemonkey?

查看:126
本文介绍了为什么我的ajaxSuccess jQuery事件没有在Greasemonkey中触发?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的基本的脚本,在其中每次运行脚本的网站发送AJAX请求时,我都想运行一些代码.

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.

我正在使用ajaxSuccess jQuery处理程序,并且没有被解雇.我怀疑这可能是因为AJAX请求是在全局标记设置为false的情况下发送的(请参见 http://docs. jquery.com/Ajax_Events ).有没有更好的方法可以做到这一点,即使将global设置为false还是可以的?

I'm using the ajaxSuccess jQuery handler and it's not being fired. 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). Is there some better way to achieve this that will work even when global is set to false?

代码:

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

谢谢

推荐答案

您的用户脚本中的jQuery在单独的运行页面jQuery的环境.

您需要拦截 页面的 AJAX调用,以便可以使用(A)unsafeWindow或(B)插入脚本.

You need to intercept the page's AJAX calls so you can use either (A) unsafeWindow or (B) Inject your script.

(A)unsafeWindow看起来像:

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


(B)脚本注入看起来像:


(B) Script injection looks like:

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);


请注意,在两种情况下,您都必须注意数据不会轻易地从页面范围流回GM范围-请注意将两者混用.


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.

可以在此答案中找到一种用于在沙箱中传输数据的解决方法.

One workaround, for transmitting data across the sandbox, can be found in this answer.

这篇关于为什么我的ajaxSuccess jQuery事件没有在Greasemonkey中触发?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆