2

背景
我有一个现有的扩展,旨在伴随基于浏览器的游戏(扩展是我的,游戏不是)。该扩展程序一直在抓取页面以获取所需的数据并发出 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/。我真的希望这只是我忽略的一些小事,但我已经在这个问题上把头撞在桌子上好几天了,所以现在我求助于 SO 的智慧。有任何想法吗?

4

1 回答 1

3

但是,据我所知,被转储的 responseSource 始终是浏览器打开后发出的第一个 http 请求的内容,显然,这不是我所期望的。

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

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

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

/* skipped */

}

不确定这是否会解决您最初的问题。

于 2009-05-30T06:44:30.030 回答