gpt4 book ai didi

javascript - 如何让 Firefox 插件监听页面中的 xmlhttprequests?

转载 作者:行者123 更新时间:2023-11-29 20:26:03 26 4
gpt4 key购买 nike

背景
我有一个现有的扩展,旨在配合基于浏览器的游戏(扩展是我的,游戏不是)。该扩展一直在抓取页面以获取所需的数据,并发出 ajax 请求以采取任何行动。

问题
游戏开发人员最近更改了网站上的一些操作以使用 ajax 请求,到目前为止我无法从这些请求中获取数据。

到目前为止我有什么

function TracingListener() {
}

TracingListener.prototype =
{
originalListener: null,
receivedData: [], // array for incoming data.

onDataAvailable: function(request, context, inputStream, offset, count)
{
var binaryInputStream = CCIN("@mozilla.org/binaryinputstream;1",
"nsIBinaryInputStream");
var storageStream = CCIN("@mozilla.org/storagestream;1", "nsIStorageStream");
binaryInputStream.setInputStream(inputStream);
storageStream.init(8192, count, null);

var binaryOutputStream = CCIN("@mozilla.org/binaryoutputstream;1",
"nsIBinaryOutputStream");

binaryOutputStream.setOutputStream(storageStream.getOutputStream(0));

// Copy received data as they come.
var data = binaryInputStream.readBytes(count);

this.receivedData.push(data);

binaryOutputStream.writeBytes(data, count);
this.originalListener.onDataAvailable(request, context,storageStream.newInputStream(0), offset, count);
},

onStartRequest: function(request, context) {
this.originalListener.onStartRequest(request, context);
},

onStopRequest: function(request, context, statusCode)
{
try {
if (request.originalURI && piratequesting.baseURL == request.originalURI.prePath && request.originalURI.path.indexOf("/index.php?ajax=") == 0) {

dump("\nProcessing: " + request.originalURI.spec + "\n");
var date = request.getResponseHeader("Date");

var responseSource = this.receivedData.join();
dump("\nResponse: " + responseSource + "\n");

piratequesting.ProcessRawResponse(request.originalURI.spec, responseSource, date);
}
} catch(e) { dumpError(e);}

this.originalListener.onStopRequest(request, context, statusCode);
},

QueryInterface: function (aIID) {
if (aIID.equals(Ci.nsIStreamListener) ||
aIID.equals(Ci.nsISupports)) {
return this;
}
throw Components.results.NS_NOINTERFACE;
}
}


hRO = {

observe: function(aSubject, aTopic, aData){
try {
if (aTopic == "http-on-examine-response") {
if (aSubject.originalURI && piratequesting.baseURL == aSubject.originalURI.prePath && aSubject.originalURI.path.indexOf("/index.php?ajax=") == 0) {
var newListener = new TracingListener();
aSubject.QueryInterface(Ci.nsITraceableChannel);
newListener.originalListener = aSubject.setNewListener(newListener);

dump("\n\nObserver Processing: " + aSubject.originalURI.spec + "\n");
for (var i in aSubject) {
dump("\n\trequest." + i);
}
}
}
} catch (e) {
dumpError(e);

}
},

QueryInterface: function(aIID){
if (aIID.equals(Ci.nsIObserver) ||
aIID.equals(Ci.nsISupports)) {
return this;
}

throw Components.results.NS_NOINTERFACE;

}
};


var observerService = Cc["@mozilla.org/observer-service;1"] .getService(Ci.nsIObserverService);

observerService.addObserver(hRO, "http-on-examine-response", false);

发生了什么
处理 http 请求时,上述代码会得到正确通知。 uri 也可用并且是正确的(它通过了域/路径检查)但是据我所知,被转储的 responseSource 始终是第一个 http 请求之后发出的内容浏览器打开,显然不是我所期待的。

以上代码大部分来自http://www.softwareishard.com/blog/firebug/nsitraceablechannel-intercept-http-traffic/ .我真的希望这只是我忽略的一件小事,但我已经用头撞 table 好几天了,所以现在我求助于 SO 的智慧。有什么想法吗?

最佳答案

but the responseSource that gets dumped is, as far as I can tell, always the contents of the first http request made after the browser opened and, obviously, not what I was expecting.

上面的代码有问题。 “receivedData”成员在原型(prototype)对象上声明并分配了空数组。这导致 TracingListener 类的每个实例化都将内存中的相同对象用于 receivedData。将您的代码更改为可能会解决他的问题:

function TracingListener() {
this.receivedData = [];
}

TracingListener.prototype =
{
originalListener: null,
receivedData: null, // array for incoming data.

/* skipped */

}

虽然不确定这是否会解决您原来的问题。

关于javascript - 如何让 Firefox 插件监听页面中的 xmlhttprequests?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/928735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com