Firefox Add
I'm trying to develop my first Firefox add-on, which should open specified url in current tab after clicking toolabar button. It should also be able to modify headers, cookies, referrer, etc before making this request.
I found this tutorial https://developer.mozilla.org/en-US/docs/Creating_Sandboxed_HTTP_Connections, about modyfing request headers, but it doesn't seem to work for me.
My addon main.js looks like this:
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);
}
}
});
After clicking button in firefox toolbar nothing happens. No errors are thrown in console. I think I'm missing something here but not sure what. I've tried to add created channel to XMLHttpRequest (https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest), but it doesn't work for url's external to server. Any suggestions?
which should open specified url in current tab
XHRs just perform requests and return the received content to a javascript callback, they do not load any content into a web page.
Instead you should just set the tab's url and modify the request headers of the http connection that will automatically be issued by firefox when loading the tab by intercepting it with a http observer. The SDK's system/event module provides convenient access to observer events.
链接地址: http://www.djcxy.com/p/79792.html上一篇: C中的数组和指针之间有什么区别?
下一篇: Firefox添加