我正在尝试开发我的第一个Firefox插件,在单击toolabar按钮后,该插件应在当前选项卡中打开指定的url。在发出此请求之前,它还应该能够修改标题,cookie,引荐来源网址等。
我发现本教程https://developer.mozilla.org/en-US/docs/Creating_Sandboxed_HTTP_Connections关于修改请求标头,但对我似乎不起作用。
我的插件main.js看起来像这样:
var {ToggleButton}= require("sdk/ui/button/toggle");
var { Cc, Cu, Ci, components } = require('chrome');
var channel;
function StreamListener(function(data){console.log(data);}) {
this.mCallbackFunc = aCallbackFunc;
}
StreamListener.prototype = {
//code pasted from 'HTTP notifications' from previously mentioned link
};
// get an listener
var listener = new StreamListener(function(data){console.log(data)});
var button = ToggleButton({
id: "refresh-page",
label: "Refresh Page",
icon: {
"16": "./icon-16.png",
"32": "./icon-32.png",
"64": "./icon-64.png"
},
onChange: function (state) {
if (state.checked === true) {
// the IO service
var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
// create an nsIURI
var uri = ioService.newURI('http://google.com', null, null);
var channel = ioService.newChannelFromURI(uri);
channel.notificationCallbacks = listener;
channel.open(listener, null);
}
}
});
单击Firefox工具栏中的按钮后,没有任何反应。在控制台中不会引发任何错误。我想我在这里想念什么,但不确定。我试图将创建的通道添加到XMLHttpRequest(https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest),但是它不适用于服务器外部的url。有什么建议?
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句