猿问

如何从Greasemonkey脚本拦截XMLHttpRequests?

我想使用Greasemonkey捕获AJAX请求的内容。


有人知道怎么做这个吗?


烙印99
浏览 1031回答 3
3回答

汪汪一只猫

可接受的答案几乎是正确的,但是可以稍作改进:(function(open) {    XMLHttpRequest.prototype.open = function() {        this.addEventListener("readystatechange", function() {            console.log(this.readyState);        }, false);        open.apply(this, arguments);    };})(XMLHttpRequest.prototype.open);优先使用apply + arguments而不是call,因为这样您就不必显式地知道打开的所有可能改变的参数!

慕桂英546537

如何修改XMLHttpRequest.prototype.open或发送替换方法来设置自己的回调并调用原始方法?回调可以执行其操作,然后将回调称为指定的原始代码。换一种说法:XMLHttpRequest.prototype.realOpen = XMLHttpRequest.prototype.open;var myOpen = function(method, url, async, user, password) {    //do whatever mucking around you want here, e.g.    //changing the onload callback to your own version    //call original    this.realOpen (method, url, async, user, password);}  //ensure all XMLHttpRequests use our custom open methodXMLHttpRequest.prototype.open = myOpen ;
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答