无法拦截来自GreaseMonkey脚本的AJAX(XHR)请求 [英] Trouble intercepting AJAX (XHR) requests from GreaseMonkey script

查看:138
本文介绍了无法拦截来自GreaseMonkey脚本的AJAX(XHR)请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户脚本的任务非常简单-通过XHR桥接功能监听XHR请求,处理接收到的数据,然后将其返回.当然,所有事情都是透明发生的.

My userscript's task is fairly simple -to listen to XHR requests via an XHR bridge sort of functionality, manipulate the data received, and return it back. Everything happening transparently, of course.

我遇到了此回复如何我在SO上从Greasemonkey脚本拦截XMLHttpRequests吗? -提供了以下代码片段:

I came across this reply How can I intercept XMLHttpRequests from a Greasemonkey script? on SO -- which provides the following code-snippet:

(function(open) {  
    XMLHttpRequest.prototype.open = function(method, url, async, user, pass) {  
        this.addEventListener("readystatechange", function() {  
            console.log(this.readyState);  
        }, false);  
        open.call(this, method, url, async, user, pass);
    };  
})(XMLHttpRequest.prototype.open);

通过FireBug推送时,代码按预期工作.但是,它在Greasemonkey脚本中不执行任何操作.

The code works as expected when pushed via FireBug. It, however, doesn't do anything in a Greasemonkey script.

在进一步的搜索中,我遇到了另一条答复:如何用油腻子拦截xhr -提到:

On further searches, I came across another reply: how intercept xhr with greasemonkey -- which mentions that:

Greasmonkey和Firefox 3.x当前不支持原型"属性.请参阅以下票证以获取更多信息: http://greasemonkey.devjavu.com/ticket/164
Greasmonkey and Firefox 3.x doesn't currently support the "prototype"-property. Please see the following ticket for more information: http://greasemonkey.devjavu.com/ticket/164

我有两个基本查询:

  1. 这是否也适用于Fx v3.5.x? (注意:devjavu.com上的票证链接无法访问)
  2. 签名(function(){})()在Javascript中是什么意思. (请耐心,不是高级JS方面的专家.)
  1. Does this apply to Fx v3.5.x as well? (Note: The ticket link on devjavu.com isn't accessible)
  2. What does signature (function(){})() mean in Javascript. (Kindly bear, am not an expert in advanced JS).

推荐答案

function(){} 是匿名函数(lambda),在简单执行后添加()在飞行中.
为代码块保留不同的(唯一的)范围非常方便.

function(){} is anonymous function (lambda), adding () after simply executes it on the fly.
It's very handy to keep different (unique) scope for chunk of code.

(function(){
  var localVariable = 'temp';
})();
console.log(localVariable); // outputs undefined

这篇关于无法拦截来自GreaseMonkey脚本的AJAX(XHR)请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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