如何从头开始打包Firefox扩展 [英] How to pack a Firefox extension from scratch

查看:207
本文介绍了如何从头开始打包Firefox扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Firefox扩展的新手,我希望您能帮我打包一个扩展,并将其发送给一些朋友进行测试。

我的扩展即将阻止一些网址。这意味着,如果有人试图加入facebook.com,我的扩展名应该将他重定向到www.google.com。

下面的代码位于
$ b $

  const {类:Cc,接口:Ci,utils:Cu,结果:Cr} =组件; 
Cu.import('resource://gre/modules/Services.jsm');
var urls_block = [
//如果URL包含任何这些元素,它们将被阻止或重定向,
//您根据观察者行中的代码17选择
'www。 facebook.com',
'www.apple.com'
];
var redir_obj = {
'www.facebook.com':'http://www.google.com/',
'www.apple.com':'http:// www.samsung.com

var observers = {
'http-on-modify-request':{
观察:函数(aSubject,aTopic,aData){
console.info('http-on-modify-request:aSubject ='
+ aSubject +'| aTopic ='+ aTopic +'| aData ='+ aData);
var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
var requestUrl = httpChannel.URI.spec.toLowerCase();
for(var i = 0; i< urls_block.length; i ++){
if(requestUrl.indexOf(urls_block [i])> -1){
//httpChannel.cancel (Cr.NS_BINDING_ABORTED); //这个放弃加载
//可以重定向到这下一行,如果不想重定向和
//只是阻塞,那么注释这一行然后取消注释上面的行:
httpChannel.redirectTo(Services.io.newURI(redir_obj [urls_block [i]],
null,null));
break;


$ b reg:function(){
Services.obs.addObserver(observers ['http-on-modify-request'],
'http-on-modify-request',false);

unreg:function(){
Services.obs.removeObserver(observers ['http-on-modify-request'],
'http-on-modify-请求');
}
}
};
函数install(){}
函数uninstall(){}
函数startup(){
(观察者中的var o){
observers [o]。 REG();



函数shutdown(aData,aReason){
if(aReason == APP_SHUTDOWN)return;

(观察者中的var o){
observers [o] .unreg();




$ b非常感谢@Noitidart的巨大帮助。 / p>

所以我想打包这个Firefox扩展的代码。
有人可以告诉我怎么做或者做任何例子吗?

非常感谢您的帮助。

解决方案

至少,您需要创建一个 install.rdf 文件和 chrome.manifest 文件。通过这些链接,你将需要做出一些选择(例如,什么叫你的扩展,< em:id> 等)。 >

另外,看起来你正在做一个 bootstrap / restartless 附加组件,并且应该调用包含问题中包含的代码的文件: bootstrap.js mozilla.org/en-US/docs/XPIrel =nofollow noreferrer> .xpi 文件格式(

用作Mozilla(Firefox,Thunderbird等)扩展的容器的$ c> .xpi 文件只是zip文件的扩展压缩文件n改为 .xpi 。这些文件从zip压缩归档的根目录开始(即有第一级目录来包含这些文件)。这些文件必须是未压缩的,或者使用Deflate算法进行压缩。使用其他压缩算法将导致您的 .xpi 文件无法加载,并显示一个弹出窗口,显示加载项已损坏。



存档的内容可能只是几个文件到任意数量的文件。至少,您有一个 install.rdf / a>和 chrome.manifest 文件。几乎总会有至少一个额外的文件(如果没有多少额外的文件)。

我非常简单的 Bootstrap / Restartless 扩展,打印按钮打印(更改打印按钮打印而不是打印预览),具有以下结构:

 存档包含:
bootstrap.js
chrome /
chrome / content /
chrome / content / options.xul
chrome / skin /
chrome / skin / printer-typeC128.png
chrome / skin / printer-typeC32.png
chrome / skin / printer-typeC48 .png
chrome / skin / printer-typeC64.png
chrome.manifest
install.rdf
license.txt
共有12个条目(42360个字节)




  • 需要 install.rdf chrome.manifest 文件。

  • 档案 bootstrap。 js Bootstrap / Restartless 扩展。它包含在扩展程序安装,删除,启用,禁用或Firefox启动或关闭时运行的代码。这个扩展很简单,所有的JavaScript代码都包含在 bootstrap.js 中。

  • 有一个文件 chrome / content / options.xul ,它是选项对话框

  • license.txt 只是说明该扩展程序是在 Mozilla Public License,v2.0
    $ b $ 打印按钮的 install.rdf 文件是Print(应该更改 PrintButtonIsPrint 的所有实例到你在你的 chrome.manifest 文件中定义的扩展名;如果你想要,你可以从 instal.rdf 文件中删除所有这些文件选项对话框,或定义的图标(尚未)):

     <?xml version =1.0?> 
    xmlns:em =http://www.mozilla。组织/ 2004 / EM-RDF#>
    <描述about =urn:mozilla:install-manifest>
    < em:id> PrintButtonIsPrint@makyen.foo< / em:id> <! - 您的分机必须是唯一的。 - >
    < em:version> 1.0.1< / em:version>
    < em:type> 2< / em:type>
    < em:name>打印按钮是打印< / em:name> <! - 对您的扩展应该是唯一的。 - >
    < em:bootstrap> true< / em:bootstrap> <! - 表示该分机是无重启的 - >
    < em:解包> false< / em:解包>
    < em:description>使打印按钮打印页面,而不是显示打印预览。添加使用Shift +左键单击和/或Ctrl +左键单击打印预览的选项(默认情况下都启用)。< / em:description>
    < em:creator> Makyen< / em:creator>
    <! - 没有。
    < em:aboutURL> chrome://PrintButtonIsPrint/content/about.xul< / em:aboutURL>
    - >
    < em:optionsURL> chrome://PrintButtonIsPrint/content/options.xul< / em:optionsURL>
    < em:iconURL> chrome://PrintButtonIsPrint/skin/printer-typeC48.png< / em:iconURL>
    < em:icon64URL> chrome://PrintButtonIsPrint/skin/printer-typeC64.png< / em:icon64URL>
    <! - Firefox - >
    < em:targetApplication>
    <说明>
    < em:id> {ec8030f7-c20a-464f-9b0e-13a3a9e97384}< / em:id>
    < em:minVersion> 29.0< / em:minVersion>
    < em:maxVersion> 37。*< / em:maxVersion>
    < /说明>
    < / em:targetApplication>
    < /说明>
    < / RDF>

    chrome.manifest (两个 PrintButtonIsPrint 应该改成你的扩展名):

      content PrintButtonIsPrint chrome / content / 
    skin PrintButtonIsPrint classic / 1.0 chrome / skin /

    创建 .xpi文件我使用一个批处理文件,它使用了DOS和Unix / Linux的组合(实际上是 Cygwin )命令:

    mkxpi.bat

      rm -f PrintButtonIsPrint@makyen.foo.xpi 
    zip -1 -r PrintButtonIsPrint@makyen.foo.xpi * -x@xpi.ignore
    pause

    这将删除旧版本的 .xpi 文件。然后使用 -1 创建一个新的 .xpi 文件,最小化压缩(访问速度比保存更重要包含所有文件和子目录 * *,但忽略 xpi.ignore 文本文件中的所有文件 -x @ xpi.ignore 。忽略文件是因为我在目录中有其他的东西(例如 .git 目录, .bak 自动创建的文件来自编辑等)。一旦创建了 .xpi 文件,脚本将执行 pause ,这样我就可以验证包含哪些文件,没有错误等,而不是只是让窗口消失,并假设一切正常。
    $ b

    My xpi.ignore 文件有点长,因为它从各种项目中积累,并很少清理:

      *。com 
    * .class
    * .dll
    * .exe
    * .o
    * .so
    * .7z
    * .dmg
    * .gz
    * .iso
    * .jar
    * .rar
    * .tar
    * .zip
    * .log
    * .sql
    * .sqlite
    * .svg
    * /。DS_Store
    * /。DS_Store?
    * / ._ *
    ._ *
    * /。Spotlight-V100
    .Spotlight-V100
    * /。
    .Trashes
    * / ehthumbs.db
    * / Thumbs.db
    * .ORIG
    * .bak
    * OLD *
    OLD / *
    * /旧/ *
    * .OLD
    * .OLD [0-9]
    * /旧/ *
    * /旧[0-9] / *
    * .unknown
    * .unknown [0-9]
    * .updated
    * .updated [0-9]
    * / Copy *
    * / OLD
    * / OLD *
    * / OLD [0-9]
    * / OLD [0-9] [0-9]
    * / test / *
    * /不在xpi / *
    * / tmp
    * .tmp
    * / foo
    * .foo
    * checkpoint
    .git
    * /.git
    .gitignore
    * /。gitignore
    xpi.ignore
    mkclean.bat
    mkclean.bat.DONTRUN
    mkxpi.bat
    * .xpi
    * / devtools-toolbox-window.ico
    * / devtools-webconsole.ico
    * / JSConsoleWindow.ico
    * / main-window.ico
    * / places.ico
    * / viewSource.ico



    安装扩展



    安装扩展(即 .xpi 文件),可以简单地将它拖放到运行您希望安装的配置文件的Firefox窗口中。对于开发/测试,您可以将扩展名放在本地驱动器的目录中通过使用 Firefox扩展代理文件(创建一个名为作为配置文件的 extensions 目录中的< em:id> 目录,包含一行以及包含扩展名文件的目录的完整路径)。根据您的目标是什么(一个配置文件,所有配置文件,所有用户,哪个操作系统等),还有其他的选择,如何 install extensions

    这个答案主要是从我的答案在这里


    I am new to Firefox extensions and i would like you to help me pack one extension that i build and send it to some friends to test it.

    My Extension is about to "block" some URLs. Which means that if someone tries to join "facebook.com" my extension should redirect him to "www.google.com"

    the code is below.

    const {classes: Cc, interfaces: Ci, utils: Cu, results: Cr} = Components;
    Cu.import('resource://gre/modules/Services.jsm');
    var urls_block = [ 
        //If URLs contain any of these elements they will be blocked or redirected,
        //  your choice based on code in observer line 17
        'www.facebook.com',
         'www.apple.com'
    ];
    var redir_obj = {
        'www.facebook.com': 'http://www.google.com/',
        'www.apple.com': 'http://www.samsung.com'
    }
    var observers = {
        'http-on-modify-request': {
            observe: function (aSubject, aTopic, aData) {
                console.info('http-on-modify-request: aSubject = ' 
                              + aSubject + ' | aTopic = ' + aTopic + ' | aData = ' + aData);
                var httpChannel = aSubject.QueryInterface(Ci.nsIHttpChannel);
                var requestUrl = httpChannel.URI.spec.toLowerCase();
                for (var i=0; i<urls_block.length; i++) {
                    if (requestUrl.indexOf(urls_block[i]) > -1) {
                        //httpChannel.cancel(Cr.NS_BINDING_ABORTED); //this aborts the load
                        //Can redirect with this next line, if don't want to redirect and
                        //  just block, then comment this line and uncomment the line above:
                        httpChannel.redirectTo(Services.io.newURI(redir_obj[urls_block[i]],
                                                   null, null));
                        break;
                    }
                }
            },
            reg: function () {
                Services.obs.addObserver(observers['http-on-modify-request'],
                                               'http-on-modify-request', false);
            },
            unreg: function () {
                Services.obs.removeObserver(observers['http-on-modify-request'], 
                                               'http-on-modify-request');
            }
        }
    };
    function install() {}
    function uninstall() {}
    function startup() {
        for (var o in observers) {
            observers[o].reg();
        }
    }
    
    function shutdown(aData, aReason) {
        if (aReason == APP_SHUTDOWN) return;
    
        for (var o in observers) {
            observers[o].unreg();
        }
    }
    

    Big thanks to @Noitidart for his enormous help.

    So i want to pack this code for Firefox Extension. Could someone show me how to do it or any example?

    Thanks a lot for your time helping me here.

    解决方案

    At a minimum, you will need to create an install.rdf file and a chrome.manifest file. Go through those links, you are going to need to make some choices (e.g. what to call your extension, an <em:id>, etc.).

    In addition, it appears that you are making a bootstrap/restartless add-on and should call the file containing the code you included in the question: bootstrap.js

    .xpi file format (Extension Packaging):

    The .xpi files that are used as containers for Mozilla (Firefox, Thunderbird, etc.) extensions are merely zip compressed archives that have had the file extension changed to .xpi. The files start in the root directory of the zip compressed archive (i.e. there is first level directory to contain the files). The files must either be uncompressed, or compressed using the "Deflate" algorithm. Using other compression algorithms will result in your .xpi file not loading and a popup being shown that the add-on is corrupt.

    The contents of the archive could be only a few files to any number of files. At a minimum, you have an install.rdf and a chrome.manifest file. There will almost always be at least one additional file (if not many additional files).

    My very simple Bootstrap/Restartless extension, Print Button is Print (changes the print button to print instead of print preview), has the following structure:

    Archive contains:
      bootstrap.js
      chrome/
      chrome/content/
      chrome/content/options.xul
      chrome/skin/
      chrome/skin/printer-typeC128.png
      chrome/skin/printer-typeC32.png
      chrome/skin/printer-typeC48.png
      chrome/skin/printer-typeC64.png
      chrome.manifest
      install.rdf
      license.txt
    Total 12 entries (42360 bytes)
    

    • There are the required install.rdf and chrome.manifest files.
    • The file bootstrap.js is required for Bootstrap/Restartless extensions. It contains the code that is run when the extension is installed, removed, enabled, disabled, or upon Firefox startup or shutdown. This extension is simple enough such that all the JavaScript code is contained in bootstrap.js.
    • There is a file chrome/content/options.xul which is a XUL definition of the options dialog.
    • The license.txt just explains that the extension was released under the Mozilla Public License, v2.0.
    • The .png files are the icon for this extension at various resolutions.

    The install.rdf file for Print Button is Print is (all instances of PrintButtonIsPrint should be changed to something for your extension which you define in your chrome.manifest file; All of them you could just delete from the instal.rdf file if you wanted to as you have no options dialog, or icons defined (yet).):

    <?xml version="1.0"?>
    <RDF xmlns="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
         xmlns:em="http://www.mozilla.org/2004/em-rdf#">
        <Description about="urn:mozilla:install-manifest">
        <em:id>PrintButtonIsPrint@makyen.foo</em:id> <!-- MUST be unique to your extension. -->
        <em:version>1.0.1</em:version>
        <em:type>2</em:type>
        <em:name>Print Button is Print</em:name> <!-- Should be unique to your extension. -->
        <em:bootstrap>true</em:bootstrap> <!-- Indicate that the extension is restartless -->
        <em:unpack>false</em:unpack>
        <em:description>Makes the Print Button print the page instead of presenting a print preview. Adds the option of using shift-left-click and/or ctrl-left-click for Print Preview (both enabled by default).</em:description>
        <em:creator>Makyen</em:creator>
        <!-- No about.
        <em:aboutURL>chrome://PrintButtonIsPrint/content/about.xul</em:aboutURL>
        -->
        <em:optionsURL>chrome://PrintButtonIsPrint/content/options.xul</em:optionsURL>
        <em:iconURL>chrome://PrintButtonIsPrint/skin/printer-typeC48.png</em:iconURL> 
        <em:icon64URL>chrome://PrintButtonIsPrint/skin/printer-typeC64.png</em:icon64URL> 
    <!--Firefox-->
        <em:targetApplication>
            <Description>
                <em:id>{ec8030f7-c20a-464f-9b0e-13a3a9e97384}</em:id>
                <em:minVersion>29.0</em:minVersion>
                <em:maxVersion>37.*</em:maxVersion>
            </Description>
        </em:targetApplication>
        </Description>
    </RDF>
    

    The chrome.manifest is (both instances of PrintButtonIsPrint should be changed to something for your extension):

    content      PrintButtonIsPrint                                         chrome/content/
    skin         PrintButtonIsPrint               classic/1.0               chrome/skin/
    

    To create the .xpi file I use a batch file, which uses a combination of DOS and Unix/Linux (actually Cygwin) commands:

    mkxpi.bat:

    rm -f PrintButtonIsPrint@makyen.foo.xpi
    zip -1 -r PrintButtonIsPrint@makyen.foo.xpi * -x@xpi.ignore
    pause
    

    This removes any old version of the .xpi file. It then creates a new .xpi file using, -1, minimal compression (speed of access is more important than saving space) and containing all files and subdirectories ** but ignoring all the files in the xpi.ignore text file -x@xpi.ignore. Ignoring files is used because I have other things in the directory (e.g. .git directory, .bak files auto-created from editor, etc.). Once the .xpi file is created the script executes pause so that I can verify which files were included, that there were no errors, etc. instead of just having the window disappear and assuming that everything is fine.

    My xpi.ignore file is a bit long, as it accumulates cruft from various projects and is rarely cleaned out:

    *.com
    *.class
    *.dll
    *.exe
    *.o
    *.so
    *.7z
    *.dmg
    *.gz
    *.iso
    *.jar
    *.rar
    *.tar
    *.zip
    *.log
    *.sql
    *.sqlite
    *.svg
    */.DS_Store
    */.DS_Store?
    */._*
    ._*
    */.Spotlight-V100
    .Spotlight-V100
    */.Trashes
    .Trashes
    */ehthumbs.db
    */Thumbs.db
    *.ORIG
    *.bak
    *OLD*
    OLD/*
    */OLD/*
    *.OLD
    *.OLD[0-9]
    */OLD/*
    */OLD[0-9]/*
    *.unknown
    *.unknown[0-9]
    *.updated
    *.updated[0-9]
    */Copy *
    */OLD
    */OLD*
    */OLD[0-9]
    */OLD[0-9][0-9]
    */test/*
    */not in xpi/*
    */tmp
    *.tmp
    */foo
    *.foo
    *checkpoint
    .git
    */.git
    .gitignore
    */.gitignore
    xpi.ignore
    mkclean.bat
    mkclean.bat.DONTRUN
    mkxpi.bat
    *.xpi
    */devtools-toolbox-window.ico
    */devtools-webconsole.ico
    */JSConsoleWindow.ico
    */main-window.ico
    */places.ico
    */viewSource.ico
    

    Installing extensions:

    As to installing extensions (i.e. the .xpi file), it can be a simple matter of dragging and dropping it onto a Firefox window running the profile in which you desire it installed. For development/testing, you can have the extension be in a directory on your local drive by using a Firefox extension proxy file (create a file named as the extension's <em:id> in the profile's extensions directory containing one line with the complete path to the directory containing the extension's files). Depending on what your goal is (one profile, all profiles, all users, which OS, etc.), there are other options as to how to install extensions.

    This answer was mostly copied from my answer here.

    这篇关于如何从头开始打包Firefox扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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