在Firefox扩展中检索当前网页的POST数据

时间:2011-07-09 10:52:35

标签: javascript firefox firefox-addon firefox-addon-sdk

我可以在Firefox扩展程序中获取当前页面URL,但我也想获取POST数据。 Firefox将其存储在某处以便在刷新时再次发送。我如何从我的扩展程序中获取它?

我可能会捕获像Firebug这样的每个请求,但我宁愿避免这种情况,只能按需获取POST数据。

我正在使用add-on SDK (jetpack)。我在我的主脚本中尝试了this page中的代码:

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

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

history = 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);

但它在

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

因为postdata is nullTypeError)。

我还在附加到活动标签页的内容脚本中尝试了此方法,但在获取Components.classes时失败并且权限被拒绝。

1 个答案:

答案 0 :(得分:2)

此代码检索最后一页的POST数据,而不是当前页面。

我将history.index-1更改为history.index并且有效。

相关问题