简体   繁体   English

让Firefox在启动时运行XUL类型的脚本

[英]Get Firefox to run XUL type script on startup

With Firefox 17.0.1 I am using an add-on called KeyConfig 20110522 to set some new hot keys and also set the acceltext of menuitems for my new keys as well as for add-ons that do not bother to do so. 在Firefox 17.0.1中,我使用一个名为KeyConfig 20110522的附加组件来设置一些新的热键,并为我的新键以及无需这样做的附加组件设置menuitemsacceltext

I want the acceltext of the menuitems to be set when Firefox starts , but currently I am just using a hot key to execute the following code against the UI via KeyConfig : 希望 在Firefox启动时设置menuitemsacceltext ,但是目前我只是使用热键通过KeyConfig对UI执行以下代码:

document.getElementById("tabmix-menu")
  .setAttribute("acceltext","Alt+Ctrl+Shift+T");
// more of the same...

I need a couple of beginners tips: 我需要几个初学者提示:

  • How can I execute arbitrary code against the UI in the same way as I execute against an HTML page via the console? 如何对UI执行任意代码的方式与通过控制台对HTML页面执行的方式相同?

  • Is there a sneaky way to get a clump of code to execute on browser start-up without delving into XUL development? 是否有一种偷偷摸摸的方式来获取一堆代码来执行浏览器启动而不深入研究XUL开发?

  • Is there a way to trace commands executed against the UI so I can get at command calls instead of using triggers when I set my hot keys like so: 有没有办法跟踪针对UI执行的命令,这样当我设置热键时,我可以获取命令调用而不是使用触发器:

document.getElementById("tabmix-menu").click();

Any other tips on this type of low-level hacking would also be welcome. 关于此类低级别黑客攻击的任何其他提示也将受到欢迎。

You can execute arbitrary code against the Firefox UI from an addon, but as you say, doing all the XUL related stuff is a bit boring :-) 你可以从一个插件对Firefox UI执行任意代码,但正如你所说,做所有与XUL相关的东西有点无聊:-)

Enter "Bootstrapped" extensions! 输入“Bootstrapped”扩展!

Part 1: 第1部分:

A "Bootstrapped" (or re-startless) extension needs only an install.rdf file to identify the addon, and a bootstrap.js file to implement the bootstrap interface. “Bootstrapped”(或重新启动)扩展只需要一个install.rdf文件来识别插件,并需要一个bootstrap.js文件来实现bootstrap接口。

The bootstrap interface can be implemented very simply: 引导接口可以非常简单地实现:

function install() {}
function uninstall() {}
function shutdown(data, reason) {}
function startup(data, reason) { /* YOUR ARBITRARY CODE HERE! */ }

You compile the extension by putting install.rdf and bootstrap.js into the top-level of a new zip file, and rename the zip file extension to .xpi. 您可以通过将install.rdfbootstrap.js放入新zip文件的顶层来编译扩展,并将zip文件扩展名重命名为.xpi。

Part 2: 第2部分:

Your code is privileged and can use any of the Mozilla platform APIs. 您的代码具有特权,可以使用任何Mozilla平台API。 There is however an issue of timing. 然而,存在时间问题。 The moment-in-time at which the "startup" function is executed is one at which no Chrome window objects exist yet! “启动”功能执行的时刻是尚未存在Chrome窗口对象的时刻!

If it's important for your code that you have a Chrome Window, we need to wait for it to appear: 如果您的代码对您有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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM