开发者

Retrieve POST data of the current web page in a Firefox extension

I can get the current page URL in a Firefox extension but I'd like to get the POST data too. Firefox stores it somewhere to send it again on refresh. How can I get it from my extension ?

I could probably capture each request like Firebug, but I'd rather avoid that and get the POST data only on demand.

I'm using the add-on SDK (jetpack). I tried the code from this page in my main script:

{Cc,Ci} = require("chrome");

wm = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
mainWindow = wm.getMostRecentWindow("navigator:browser");

histor开发者_如何学JAVAy = mainWindow.gBrowser.selectedBrowser.webNavigation.sessionHistory;

postdata = history.getEntryAtIndex(history.index-1,false).QueryInterface(Ci.nsISHEntry).postData;

postdata.QueryInterface(Ci.nsISeekableStream).seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
stream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
stream.setInputStream(postdata);
postBytes = stream.readByteArray(stream.available());
poststr = String.fromCharCode.apply(null, postBytes);

console.log(poststr);

But it fails at

postdata.QueryInterface(Ci.nsISeekableStream).seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);

Because postdata is null (TypeError).

I also tried this method in a content script attached to the active tab, but it fails with a permission denied while getting Components.classes.


This code retrieves the POST data of the last page, not the current one.

I changed history.index-1 to history.index and it works.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜