How to pack a Firefox extension from scratch -


i new firefox extensions , me pack 1 extension build , send friends test it.

my extension "block" urls. means if tries join "facebook.com" extension should redirect him "www.google.com"

the code below.

const {classes: cc, interfaces: ci, utils: cu, results: cr} = components; cu.import('resource://gre/modules/services.jsm'); var urls_block = [      //if urls contain of these elements blocked or redirected,     //  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();             (var i=0; i<urls_block.length; i++) {                 if (requesturl.indexof(urls_block[i]) > -1) {                     //httpchannel.cancel(cr.ns_binding_aborted); //this aborts load                     //can redirect next line, if don't want redirect ,                     //  block, comment line , uncomment 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() {     (var o in observers) {         observers[o].reg();     } }  function shutdown(adata, areason) {     if (areason == app_shutdown) return;      (var o in observers) {         observers[o].unreg();     } } 

big @noitidart enormous help.

so want pack code firefox extension. show me how or example?

thanks lot time helping me here.

at minimum, need create install.rdf file , chrome.manifest file. go through links, going need make choices (e.g. call extension, <em:id>, etc.).

in addition, appears making bootstrap/restartless add-on , should call file containing code included in question: bootstrap.js

.xpi file format (extension packaging):

the .xpi files used containers mozilla (firefox, thunderbird, etc.) extensions merely zip compressed archives have had file extension changed .xpi. files start in root directory of zip compressed archive (i.e. there first level directory contain files). files must either uncompressed, or compressed using "deflate" algorithm. using other compression algorithms result in .xpi file not loading , popup being shown add-on corrupt.

the contents of archive few files number of files. @ minimum, have install.rdf , chrome.manifest file. there @ least 1 additional file (if not many additional files).

my simple bootstrap/restartless extension, print button print (changes print button print instead of print preview), has 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) 

the install.rdf file print button print (all instances of printbuttonisprint should changed extension define in chrome.manifest file; of them delete instal.rdf file if wanted 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 unique extension. -->     <em:version>1.0.1</em:version>     <em:type>2</em:type>     <em:name>print button print</em:name> <!-- should unique extension. -->     <em:bootstrap>true</em:bootstrap> <!-- indicate extension restartless -->     <em:unpack>false</em:unpack>     <em:description>makes print button print page instead of presenting print preview. adds option of using shift-left-click and/or ctrl-left-click print preview (both enabled 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 (both instances of printbuttonisprint should changed extension):

content      printbuttonisprint                                         chrome/content/ skin         printbuttonisprint               classic/1.0               chrome/skin/ 

to create .xpi file use batch file, uses combination of dos , 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 old version of .xpi file. creates new .xpi file using, -1, minimal compression (speed of access more important saving space) , containing files , subdirectories ** ignoring files in xpi.ignore text file -x@xpi.ignore. ignoring files used because have other things in directory (e.g. .git directory, .bak files auto-created editor, etc.). once .xpi file created script executes pause can verify files included, there no errors, etc. instead of having window disappear , assuming fine.

my xpi.ignore file bit long, accumulates cruft various projects , 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 installing extensions (i.e. .xpi file), can simple matter of dragging , dropping onto firefox window running profile in desire installed. development/testing, can have extension in directory on local drive using firefox extension proxy file (create file named extension's <em:id> in profile's extensions directory containing 1 line complete path directory containing extension's files). depending on goal (one profile, profiles, users, os, etc.), there other options how install extensions.

this answer copied my answer here.


Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -