我是Firefox扩展程序的新手,我希望您能帮我打包一个我构建的扩展程序并将其发送给一些朋友进行测试。
我的扩展即将到来"阻止"一些网址。这意味着,如果有人试图加入" facebook.com"我的分机应该将他重定向到" www.google.com"
代码如下。
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();
}
}
非常感谢@Noitidart的巨大帮助。
所以我想为Firefox扩展包装此代码。 有人可以告诉我如何做或任何一个例子吗?
非常感谢你在这里帮助我。
答案 0 :(得分:2)
至少,您需要创建一个install.rdf文件和一个chrome.manifest文件。通过这些链接,您将需要做出一些选择(例如,如何称呼您的扩展程序,<em:id>
等。)
此外,您似乎正在制作bootstrap/restartless插件,并且应该调用包含您在问题中包含的代码的文件:bootstrap.js
.xpi
file format(Extension Packaging):用作Mozilla(Firefox,Thunderbird等)扩展程序容器的.xpi
文件只是压缩文件,其文件扩展名已更改为.xpi
。文件从zip压缩存档的根目录开始(即,存在包含文件的第一级目录)。文件必须是未压缩的,或使用“Deflate”算法压缩。使用其他压缩算法将导致您的.xpi
文件无法加载,并且会显示一个弹出窗口,说明加载项已损坏。
存档的内容可能只是几个文件到任意数量的文件。您至少拥有install.rdf和chrome.manifest文件。几乎总会有至少一个附加文件(如果没有多少附加文件)。
我非常简单的Bootstrap/Restartless扩展程序Print Button is Print(更改打印按钮以打印而不是打印预览),具有以下结构:
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)
.png
文件是此扩展程序在各种分辨率下的图标。“打印”按钮的 install.rdf 文件为“打印”(PrintButtonIsPrint
的所有实例都应更改为您在 chrome.manifest <中定义的扩展程序的内容/ em> file;如果您没有选项对话框或已定义图标,则可以从 instal.rdf 文件中删除所有文件。):
<?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>
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
文件的任何旧版本。然后,它使用.xpi
创建一个新的-1
文件,最小压缩(访问速度比保存空间更重要)并包含所有文件和子目录*
*但忽略所有文件 xpi.ignore 文本文件-x@xpi.ignore
。使用忽略文件是因为我在目录中有其他内容(例如.git
目录,从编辑器自动创建的.bak
文件等)。创建.xpi
文件后,脚本将执行pause
,以便我可以验证包含哪些文件,没有错误等等,而不仅仅是让窗口消失并假设一切正常。
我的 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
.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(即.xpi
文件),可以将其拖放到运行您希望安装的配置文件的Firefox窗口上。对于development/testing,您可以使用Firefox extension proxy file将扩展名放在本地驱动器的目录中(在配置文件的扩展名中创建一个名为扩展名<em:id>
的文件包含一行的目录,其中包含包含扩展名文件的目录的完整路径。根据您的目标(一个配置文件,所有配置文件,所有用户,哪个操作系统等),还有其他选项如何install extensions。
这个答案主要是从my answer here复制的。