[英]Get Firefox to run XUL type script on startup
在Firefox 17.0.1中,我使用一个名为KeyConfig 20110522的附加组件来设置一些新的热键,并为我的新键以及无需这样做的附加组件设置menuitems
的acceltext
。
我希望 在Firefox启动时设置menuitems
的acceltext
,但是目前我只是使用热键通过KeyConfig对UI执行以下代码:
document.getElementById("tabmix-menu")
.setAttribute("acceltext","Alt+Ctrl+Shift+T");
// more of the same...
我需要几个初学者提示:
如何对UI执行任意代码的方式与通过控制台对HTML页面执行的方式相同?
是否有一种偷偷摸摸的方式来获取一堆代码来执行浏览器启动而不深入研究XUL开发?
有没有办法跟踪针对UI执行的命令,这样当我设置热键时,我可以获取命令调用而不是使用触发器:
document.getElementById("tabmix-menu").click();
关于此类低级别黑客攻击的任何其他提示也将受到欢迎。
你可以从一个插件对Firefox UI执行任意代码,但正如你所说,做所有与XUL相关的东西有点无聊:-)
输入“Bootstrapped”扩展!
第1部分:
“Bootstrapped”(或重新启动)扩展只需要一个install.rdf文件来识别插件,并需要一个bootstrap.js文件来实现bootstrap接口。
Bootstrapped Extension: https : //developer.mozilla.org/en-US/docs/Extensions/Bootstrapped_extensions
很好的例子: http : //blog.fpmurphy.com/2011/02/firefox-4-restartless-add-ons.html
引导接口可以非常简单地实现:
function install() {}
function uninstall() {}
function shutdown(data, reason) {}
function startup(data, reason) { /* YOUR ARBITRARY CODE HERE! */ }
您可以通过将install.rdf
和bootstrap.js
放入新zip文件的顶层来编译扩展,并将zip文件扩展名重命名为.xpi。
第2部分:
您的代码具有特权,可以使用任何Mozilla平台API。 然而,存在时间问题。 “启动”功能执行的时刻是尚未存在Chrome窗口对象的时刻!
如果您的代码对您有Chrome窗口很重要,我们需要等待它出现:
// useful services.
Cu.import("resource://gre/modules/Services.jsm");
var loader = Cc["@mozilla.org/moz/jssubscript-loader;1"]
.getService(Ci.mozIJSSubScriptLoader);
var wmSvc = Cc["@mozilla.org/appshell/window-mediator;1"]
.getService(Ci.nsIWindowMediator);
var logSvc = Cc["@mozilla.org/consoleservice;1"]
.getService(Ci.nsIConsoleService);
// get the first gBrowser
var done_startup = 0;
var windowListener;
function do_startup(win) {
if (done_startup) return;
done_startup = 1;
wmSvc.removeListener(windowListener);
var browserEnum = wmSvc.getEnumerator("navigator:browser");
var browserWin = browserEnum.getNext();
var tabbrowser = browserWin.gBrowser;
/* your code goes here! */
}
// window listener implementation
windowListener = {
onWindowTitleChange: function(aWindow, aTitle) {},
onCloseWindow: function(aWindow) {},
onOpenWindow: function(aWindow) {
var win = aWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowInternal || Ci.nsIDOMWindow);
win.addEventListener("load", function(aEvent) {
win.removeEventListener("load", arguments.callee, false);
if (aEvent.originalTarget.nodeName != "#document") return;
do_startup();
}
};
// CODE ENTRY POINT (put this in bootstrap "startup" function)
wmSvc.addListener(windowListener);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.