我尝试使用addProgressListener
,但我不知道如何在onStateChange
中获得帖子数据并将其发送回去。
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);
}
我也尝试使用observerService,但这些主题都不能跟踪每个请求并制定计划。
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);
我假设数据请求可以使用nsIUploadChannel
获得,但我不明白如何使用它
试试这个,未经测试,甚至没有检查拼写错误
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;
}