在firefox扩展开发实例入门之comic扩展的基础上,添加了一项代理选择的功能,
对layout进行了扩展,鼠标右键菜单新添加了radio类型子菜单
代码如下:
xul可参考
http://xulplanet.com/references/elemref/
https://developer.mozilla.org/en/XUL_Reference
overlay.xul
<?xml version=”1.0” encoding=”UTF-8”?>
<overlay id=”comic-overlay”
xmlns=”http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script src=”chrome://comic/content/overlay.js”/>
<popup id=”contentAreaContextMenu”>
<menuitem id=”play-comic” label=”Play Online”
oncommand=”comic.play()”
insertbefore=”context-openlink” />
<!–menu id=”proxy-submenu” label=”proxy setting” insertbefore=”context-sep-properties”–>
<menu id=”proxy-submenu” label=”proxy setting” insertbefore=”context-openlink”>
<menupopup id=”proxy-popup”>
<menuitem id=”none” label=”No proxy” oncommand=”comic.set_proxy(‘none’)” type=”radio” checked=”true” />
<menuitem id=”zjupry” label=”...:“ oncommand=”comic.set_proxy(‘zjupry’)” type=”radio” />
<menuitem id=”stg12” label=”...:“ oncommand=”comic.set_proxy(‘stg12’)” type=”radio” />
</menupopup>
</menu>
</popup>
</overlay>
Using JavaScript in gecko-based browsers:
Gecko DOM Reference: https://developer.mozilla.org/en/Gecko_DOM_Reference
examples :https://developer.mozilla.org/en/Gecko_DOM_Reference/Examples
overlay.js
var comic = {
onLoad: function() {
this.initialized = true;
document.getElementById(“contentAreaContextMenu”)
.addEventListener(“popupshowing”,this.showContextMenu,false);
},
getURL:function(){
var URL;
if (gContextMenu && gContextMenu.onLink) {
var result = gContextMenu.linkURL.match(/.(.rmvb|.avi|.wma|.flv|.mp3)./ig);
if(result) {
URL = encodeURI(result);
URL = unescape(URL);
}
return URL;
}
},
showContextMenu: function() {
document.getElementById(“play-comic”).hidden = true;
if (comic.getURL()) {
document.getElementById(“play-comic”).hidden = false;
}
var p = Components.classes[“@mozilla.org/preferences-service;1”]
.getService(Components.interfaces.nsIPrefBranch).getCharPref(“jfo.proxy.selection”);
if(p) {
document.getElementById(p).setAttribute(‘style’, ‘font-weight:bold’);
document.getElementById(p).setAttribute(“checked”,true);
}
},
callProcess:function(path,args){
var file = Components.classes[“@mozilla.org/file/local;1”]
.createInstance(Components.interfaces.nsILocalFile);
file.initWithPath(path);
var process = Components.classes[“@mozilla.org/process/util;1”]
.createInstance(Components.interfaces.nsIProcess);
process.init(file);
process.run(false, args, args.length);
},
play:function(){
var path =Components.classes[“@mozilla.org/preferences-service;1”].
getService(Components.interfaces.nsIPrefBranch).getCharPref(“sjtu.comic.mplayer”);
var URL = comic.getURL();
var args = [];
args.push(URL);
comic.callProcess(path,args);
},
getProxy:function(){
var Proxy;
if (gContextMenu && gContextMenu.onLink) {
var result = gContextMenu.linkURL.match(/.(.rmvb|.avi|.flv|.mp3)./ig);
if(result) {
Proxy = encodeURI(result);
Proxy = unescape(Proxy);
}
return Proxy;
}
},
set_proxy:function(id){
var sel = Components.classes[“@mozilla.org/preferences-service;1”]
.getService(Components.interfaces.nsIPrefBranch).getCharPref(“jfo.proxy.selection”);
document.getElementById(sel).setAttribute(‘style’, ‘’);
var p = Components.classes[“@mozilla.org/preferences-service;1”]
.getService(Components.interfaces.nsIPrefBranch).setCharPref(“jfo.proxy.selection”, id);
if(id == “none”) {
Components.classes[“@mozilla.org/preferences-service;1”]
.getService(Components.interfaces.nsIPrefBranch).setIntPref(“network.proxy.type”, 0);
return;
}
proxy = document.getElementById(id).getAttribute(‘label’);
if(proxy) {
Components.classes[“@mozilla.org/preferences-service;1”]
.getService(Components.interfaces.nsIPrefBranch).setIntPref(“network.proxy.type”, 1);
var r = proxy.split(“:”);
Components.classes[“@mozilla.org/preferences-service;1”]
.getService(Components.interfaces.nsIPrefBranch).setCharPref(“network.proxy.http”, r[0]);
Components.classes[“@mozilla.org/preferences-service;1”]
.getService(Components.interfaces.nsIPrefBranch).setIntPref(“network.proxy.http_port”, parseInt(r[1]));
}
},
};
window.addEventListener(“load”, function(e) { comic.onLoad(e); }, false);
另外写了一个创建xpi安装包的bat文件
run.bat
“C:Program FilesWinRarWinRar.exe” a -afzip my_comic.xpi install.rdf chrome.manifest chrome
“C:Program FilesFirefox PlusFirefox Plus.exe” my_comic.xpi
end