1

I tried to use addProgressListener but I don't know how to get post data in onStateChange and send it back.

exports.main = function() {
    var {Cc, Ci, Cu} = require("chrome");
    var windows = require("sdk/windows").browserWindows;
    const windowUtils = require("sdk/window/utils");
    var {XPCOMUtils} = Cu.import("resource://gre/modules/XPCOMUtils.jsm");
    var { setTimeout, clearTimeout } = require("sdk/timers");
    var gBrowser = windowUtils.getMostRecentBrowserWindow().getBrowser();
    var tabs = require("sdk/tabs");
    const STATE_START = Ci.nsIWebProgressListener.STATE_START;
    const STATE_STOP = Ci.nsIWebProgressListener.STATE_STOP;
    const STATE_REDIRECTING = Ci.nsIWebProgressListener.STATE_REDIRECTING;
    const STATE_TRANSFERRING = Ci.nsIWebProgressListener.STATE_TRANSFERRING;
    const STATE_RESTORING = Ci.nsIWebProgressListener.STATE_RESTORING;
    var timer = null;
    var counter =  0;
    var start_url = '';
    var retry = false;

    var myListener = {

        QueryInterface: XPCOMUtils.generateQI(Ci),
        onState: function(aWebProgress, aRequest, aFlag, aStatus) {
            console.log('test');
        },
        onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) {
            Ci.nsIUploadChannel2
            if(!aRequest.name.startsWith('http')) {
                return false;
            }

            if (!(start_url && (start_url == aRequest.name))) {
                if (!aWebProgress.isLoadingDocument && !retry) {
                    return false;
                }
            }
            retry = false;
            if (aFlag & STATE_START) {
                if (timer) {
                    clearTimeout(timer);
                    timer = null
                    start_url = '';
                    counter = 0
                }
                console.log("start");
                start_url = aRequest.name;
                timer = setTimeout(function() {
                    if (counter < 5) {
                        // console.log("after start");
                        console.log("aRequest.name: ", aRequest.name);
                        new_tab = gBrowser.addTab(aRequest.name);
                        gBrowser.removeCurrentTab();
                        gBrowser.selectedTab = new_tab;
                        retry = true;
                        counter += 1
                        myListener['onStateChange'](aWebProgress, aRequest, 
                                                                  aFlag, aStatus);
                    } else {
                        gBrowser.removeCurrentTab();
                    }
                }, 10000)   

            }
            if (aFlag & STATE_STOP) {
                if (timer) {
                    console.log("stop");
                    clearTimeout(timer);
                    timer = null
                    start_url = '';
                    counter = 0
                }
            }
        },

        onLocationChange: function(aProgress, aRequest, aURI) {
            start_url = aRequest.name;
            console.log('onLocationChange: ', aRequest.name)
        },
        onProgressChange: function(aWebProgress, aRequest, curSelf,
                                                 maxSelf, curTot, maxTot) {
            console.log('onProgressChange: ', aRequest.name)
        },
        onStatusChange: function(aWebProgress, aRequest, aStatus, aMessage) {
            console.log('onStatusChange: ', aRequest.name)
        },
        onSecurityChange: function(aWebProgress, aRequest, aState) {
            console.log('onSecurityChange: ', aRequest.name)
        }
    }
    gBrowser.addProgressListener(myListener);
}

I also tried to use observerService but none of those topics makes it possible to keep track of each request and make my plans.

observerService.addObserver(this, "http-on-opening-request", false);
observerService.addObserver(this, "http-on-examine-merged-response", false);
observerService.addObserver(this, "http-on-examine-cached-responsee", false);
observerService.addObserver(this, "http-on-examine-response", false);

I assume that the data request can be obtained using nsIUploadChannel but I do not understand how to use it.

Makyen
  • 31,849
  • 12
  • 86
  • 121
Tany
  • 393
  • 1
  • 4
  • 16

1 Answers1

0

Try this, untested, didnt even check for typos.

onStateChange: function(aWebProgress, aRequest, aFlag, aStatus) {
    if (aRequest instanceof Ci.nsIHttpChannel) {
            if (aRequest.QueryInterface(Ci.nsIHttpChannel).uploadStream) {
                // see line 10 - https://gist.github.com/Noitidart/28624e20bfee2dc128c4#file-gistfile1-txt-L10
            }
    }
    if(!aRequest.name.startsWith('http')) {
        return false;
    }
Noitidart
  • 35,443
  • 37
  • 154
  • 323
  • what is it `this.oHttp`? if i do [pastebin](http://pastebin.com/1p3wVSwQ) i got `Type Error ` – Tany Sep 10 '15 at 15:38
  • @tany `this.oHttp == aRequest.QueryInterface(Ci.nsIHttpChannel)` – Noitidart Sep 10 '15 at 15:49
  • `http = aRequest.QueryInterface(Ci.nsIHttpChannel)` - > got error `is null` , `this.oHttp` -> got `this.oHttp is undefined` – Tany Sep 10 '15 at 15:58