我正在创建一个firefox插件,在单击工具栏按钮时会在新选项卡中打开网站。 当我单击工具栏按钮时,我没有收到回复。我的代码中是否有任何遗漏?
第1部分:
var app = function () {
var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
return {
init : function () {
gBrowser.addEventListener("load", function () {
var toRun = prefManager.getBoolPref("extensions.app.autorun");
if (autoRun) {
app.run();
}
}, false);
},
run : function () {
var head = content.document.getElementsByTagName("head")[0],
style = content.document.getElementById("app-style"),
allLinks = content.document.getElementsByTagName("a"),
foundLinks = 0;
if (!style) {
style = content.document.createElement("link");
style.id = "app-style";
style.type = "text/css";
style.rel = "stylesheet";
style.href = "chrome://app/skin/skin.css";
head.appendChild(style);
}
},
click : function () {
gBrowser.addEventListener("click", function () {
gBrowser.addTab("https://www.google.com");
}, false);
},
}
};
第2部分:
function installButton(toolbarId, id, afterId) {
if (!document.getElementById(id))
var toolbar = document.getElementById(toolbarId);
// If no afterId is given, then append the item to the toolbar
var before = null;
if (afterId) {
var elem = document.getElementById(afterId);
if (elem && elem.parentNode == toolbar)
before = elem.nextElementSibling;
}
toolbar.insertItem(id, before);
toolbar.setAttribute("currentset", toolbar.currentSet);
document.persist(toolbar.id, "currentset");
if (toolbarId == "addon-bar")
toolbar.collapsed = false;
}
};
Application.getExtensions(function (extensions) {
var extension = extensions.get("app@app.com");
installButton("nav-bar", "app-toolbar-button");
});
window.addEventListener("load", app.init, false);
window.addEventListener("click", app.click, false);
答案 0 :(得分:1)
我认为你应该使用正常方式(这些是我常规插件的摘录):
1)chrome.manifest文件
# ... here is packages definitions ...
# Overlays for Mozilla Firefox
overlay chrome://browser/content/browser.xul chrome://xpcomviewer/content/overlay-firefox.xul
# This is needed for toolbar
style chrome://global/content/customizeToolbar.xul chrome://xpcomviewer/skin/overlays.css
# ...
2)content / overlay.xul
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet href="chrome://xpcomviewer/skin/overlays.css" type="text/css"?>
<!DOCTYPE overlay [
<!ENTITY % brandDTD SYSTEM "../locale/en-US/brand.dtd">
%brandDTD;
<!ENTITY % overlayDTD SYSTEM "../locale/en-US/overlays.dtd">
%overlayDTD;
]>
<overlay id="eXpcomViewerBrowserOverlay"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<script type="application/javascript"
src="chrome://xpcomviewer/content/overlays.js"></script>
<!-- Toolbar palette overlay -->
<toolbarpalette id="BrowserToolbarPalette">
<toolbarbutton id="xpcomviewer-toolbarbutton"
class="toolbarbutton-1"
oncommand="xpcomviewer.overlay.open();"
label="&xpcomviewerCmd.label;"
accesskey="&xpcomviewerCmd.key;"
tooltiptext="&xpcomviewerCmd.tip;"/>
</toolbarpalette>
</overlay>
3)content / overlay.js
/* Copyright (c) 2007-2012 ondrejd.info
See the file LICENSE.txt for licensing information. */
/*
#include "global.js"
*/
// Namespace
if(typeof(xpcomviewer) == "undefined") var xpcomviewer = {};
if(typeof(xpcomviewer.overlay) == "undefined") xpcomviewer.overlay = {};
// Implementation of xpcomviewer.overlay
(function() {
/** @type String */
const kFIREFOX_ID = "{ec8030f7-c20a-464f-9b0e-13a3a9e97384}";
/** @type String */
const kSEAMONKEY_ID = "{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}";
/** @type String */
const kTHUNDERBIRD_ID = "{3550f703-e582-4d05-9a08-453d09bdfdc6}";
/** @type String */
const kWIN_URI = "chrome://xpcomviewer/content/xpcomviewer.xul";
/** @type Components.interfaces.nsIPrefBranch */
const kPREF_SRV = Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefService).
getBranch("extensions.xpcomviewer.");
/**
* Opens XPCOMViewer.
*
* @returns {void}
*/
this.open = function()
{
let useTab = kPREF_SRV.getBoolPref("useTab");
if(useTab === true) {
this.openInTab();
} else {
this.openInWindow();
}
}; // end open()
/**
* Opens XPCOMViewer in a standalone window.
*
* @returns {void}
*/
this.openInWindow = function()
{
let win = window.open(
kWIN_URI,
"xpcomviewer",
"chrome,extrachrome,menubar,toolbar,resizable,all"
);
win.focus();
}; // end openInWindow()
/**
* Opens XPCOMViewer in a new tab if is running on Firefox, SeaMonkey or
* Thunderbird. Otherwise opens in a new window.
*
* @returns {void}
*/
this.openInTab = function()
{
let appInfo = Components.classes["@mozilla.org/xre/app-info;1"].
getService(Components.interfaces.nsIXULAppInfo);
if(appInfo.ID == kFIREFOX_ID || appInfo.ID == kSEAMONKEY_ID) {
xpcomviewer.global.openUrlInInternalBrowser(kWIN_URI);
} else if(appInfo.ID == kTHUNDERBIRD_ID) {
xpcomviewer.global.openUrlInContentTab(kWIN_URI);
} else {
this.openInWindow();
}
}; // end openInTab()
}).apply(xpcomviewer.overlay);
4)skin / overlay.css
@import url("chrome://global/skin/");
@namespace url("http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul");
/* Copyright (c) 2005-2012 Ondrej Donek.
* See the file LICENSE.txt for licensing information. */
.icon16 {
list-style-image: url("chrome://xpcomviewer/skin/icons16.png");
-moz-image-region: rect(16px 16px 32px 0px);
}
#xpcomviewer-toolbarbutton {
list-style-image: url("chrome://xpcomviewer/skin/icon22.png");
}
toolbar[iconsize="small"] #xpcomviewer-toolbarbutton {
list-style-image: url("chrome://xpcomviewer/skin/icons16.png");
-moz-image-region: rect(16px 16px 32px 0px);
}
#mini-xpcomviewer {
list-style-image: url("chrome://xpcomviewer/skin/icons16.png");
-moz-image-region: rect(16px 16px 32px 0px);
}