在 Firefox 17.0.1 中,我使用名为 KeyConfig 20110522 的插件来设置一些新的热键,并设置 menuitems
的 acceltext
code> 对于我的新 key 以及不需要这样做的附加组件。
我希望在 Firefox 启动时设置 menuitems
的 acceltext
,但目前我只是使用热键通过 KeyConfig 在 UI 上执行以下代码:
document.getElementById("tabmix-menu")
.setAttribute("acceltext","Alt+Ctrl+Shift+T");
// more of the same...
我需要一些初学者提示:
如何以与通过控制台对 HTML 页面执行相同的方式对 UI 执行任意代码?
有没有一种偷偷摸摸的方法可以让一大堆代码在浏览器启动时执行,而无需深入研究 XUL 开发?
是否有一种方法可以跟踪针对 UI 执行的命令,以便在我设置热键时可以进行命令调用,而不是使用触发器:
document.getElementById("tabmix-menu").click();
有关此类低级黑客攻击的任何其他提示也将受到欢迎。
最佳答案
您可以通过插件对 Firefox UI 执行任意代码,但正如您所说,执行所有与 XUL 相关的操作有点无聊:-)
输入“引导”扩展!
第 1 部分:
“Bootstrapped”(或无需重新启动)扩展只需要一个 install.rdf 文件来识别插件,以及一个 bootstrap.js 文件来实现 bootstrap 接口(interface)。
引导扩展:https://developer.mozilla.org/en-US/docs/Extensions/Bootstrapped_extensions
很好的例子:http://blog.fpmurphy.com/2011/02/firefox-4-restartless-add-ons.html
引导接口(interface)的实现非常简单:
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);
关于firefox - 让 Firefox 在启动时运行 XUL 类型脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13941960/