问题描述
我的用户脚本的任务非常简单-通过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:
我有两个基本查询:
- 这是否也适用于Fx v3.5.x? (注意:devjavu.com上的票证链接无法访问)
- 签名
(function(){})()
在Javascript中是什么意思. (请耐心,不是高级JS方面的专家.)
- Does this apply to Fx v3.5.x as well? (Note: The ticket link on devjavu.com isn't accessible)
- 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)请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!