让Firefox在启动时运行XUL类型脚本 [英] Get Firefox to run XUL type script on startup

查看:113
本文介绍了让Firefox在启动时运行XUL类型脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Firefox 17.0.1,我使用名为 KeyConfig 20110522 的附加组件来设置一些新的热键,并设置 acceltext menuitems 用于我的新密钥以及附加组件,而这些附加组件不需要这么做。

I <当Firefox启动时, 菜单项 acceltext 被设为 >,但目前我只是使用热键通过 KeyConfig 对UI执行以下代码:

  document.getElementById(tabmix-menu)
.setAttribute(acceltext,Alt + Ctrl + Shift + T);
//更多相同...

我需要一些初学者的提示:


  • 如何以与执行控制台的HTML页面相同的方式对UI执行任意代码?


  • 有没有一种偷偷摸摸的方法可以在浏览器启动时执行一堆代码,而无需深入研究XUL开发? b $ b

  • 有没有办法跟踪对UI执行的命令,这样我就可以使用命令调用,而不是在使用触发器的时候使用触发器:



  • $ document.getElementById(tabmix-menu)。click();



    这种类型的低级黑客攻击的任何其他提示也将受到欢迎。

    >你可以从一个插件对Firefox UI执行任意代码,但正如你所说,做所有的XUL相关的东西有点无聊: - )

    输入Bootstrapped 扩展!

    第1部分:

    Bootstrapped(或重新启动)扩展只需要一个install.rdf文件来标识插件,一个bootstrap.js文件来实现引导程序接口。



    • 引导式扩展: https://developer.mozilla.org/zh-CN/docs/Extensions/Bootstrapped_extensions


    • 很好的例子: http://blog.fpmurphy。 com / 2011/02 / firefox-4-restartless-add-ons.html
    • 引导程序接口可以非常简单的实现:
      $ b $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b函数关闭(数据,原因){}
      函数启动(数据,原因){/ *您的任意代码在这里! * /}

      通过将 install.rdf bootstrap.js 添加到新zip文件的顶层,并将zip文件扩展名重命名为.xpi。 第二部分:

      您的代码有特权,可以使用任何Mozilla平台API。但是,有一个时间问题。执行启动功能的那一刻是没有Chrome窗口对象存在的时间!



      如果您的代码对您有重要意义Chrome窗口,我们需要等待它出现:

        //有用的服务。 
      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);

      //获取第一个gBrowser
      var done_startup = 0;
      var windowListener;
      函数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;

      / *您的代码在这里! * /
      }

      //窗口侦听器实现
      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(把这个放在bootstrapstartup函数中)
      wmSvc.addListener(windowListener);


      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.

      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:

      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?

      • Is there a sneaky way to get a clump of code to execute on browser start-up without delving into XUL development?

      • 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:

      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 :-)

      Enter "Bootstrapped" extensions!

      Part 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.

      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.

      Part 2:

      Your code is privileged and can use any of the Mozilla platform APIs. 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!

      If it's important for your code that you have a Chrome Window, we need to wait for it to appear:

      // 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在启动时运行XUL类型脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆