Hello!
On Habré there are a lot of articles on the topic of writing extensions for Mozilla Firefox.
Using the search, you can find information, for example:
here ,
here ,
here or even
here .
But I have not yet found (if there is, then - sorry) articles about valid extensions.
')
At the same time, the Developer Extensions Validator has existed for quite a long time:
here at the bottom of the page, you need a login to enter .
I will not copy the information given in the articles above, with your permission.
Instead, I will try to describe the general principles for creating a “valid” supplement.
And I’ll also give you a couple of examples and add links.
- Never, use eval () . Although everyone knows this rule, but here it is all the more fraught.
In addition, I think that few always remember that eval () is a “closure”.
Everything that was declared in its "scope" (scope) becomes available to the code that was contained in eval (). Including this is fraught with so many problems. Especially if you prefer to give auxiliary variables names like "i", "str", and the like.
- When using window.setTimeout (), always write like this:
window.setTimeout( function (){<br> [ ]<br> }, ); <br><br> * This source code was highlighted with Source Code Highlighter .
First, it is valid, and secondly, it is valid, and third, too.
What to do if you need to pass variables?
Use the scope to close!
( function ( [ , Timeout ] ){
window.setTimeout( function (){
[ ]
}, );
}( [ , "" Timeout ] ));
* This source code was highlighted with Source Code Highlighter .
There is, of course, quite the “right” option - use, for example, this:
Components.classes[ '@mozilla.org/timer;1' ].createInstance(Components.interfaces.nsITimer); <br><br> * This source code was highlighted with Source Code Highlighter .
But this is “quite” right.
- Never, ever, ever get into Global Scrope for an overlay.
That is this:
< toolbarbutton id ="helloButton" label ="&helloworld;" oncommand ="showHello();" /> <br><br> * This source code was highlighted with Source Code Highlighter .
Should be avoided.
What is wrong here?
Not so that there is a global function "showHello ()".
Yes, everyone knows that it is “in the sandbox” and not literally “available” from
Global Overlay for Firefox Skin.
But this does not prevent it from remaining global!
At least in the framework of the project ...
So, her “something” or “somebody” can “accidentally” ...
What to do?
But no one has yet canceled " addEventListener "
That is, do this:
<toolbarbutton id= "helloButton" label= "&helloworld;" />
var gid = function (id){ return window. document .getElementById(id); }
gid( 'helloButton' ).addEventListener( 'command' , function (evt){ our_event_handler(evt); }, false );
* This source code was highlighted with Source Code Highlighter .
Yes, it is a "dreary", but validation is more expensive!
What else can you think of to make your life harder:
- For your XUL overlay, it is advisable to create only one (there should be only one, as in “Gorce”) a JS file, which will be “all your code”.
Moreover, the entire code of this file will be inside the circuit, like this:
( function (){
var e;
try {
[ ]
} catch (e){ window.alert(e); }
})();
* This source code was highlighted with Source Code Highlighter .
- Do you want the code to be “scattered” in “a bunch of small files in a bunch of directories?
This is also possible!
var JS_Loader = function ( path_to_file, object_where_file_will_be_loaded ){
Components.classes[ '@mozilla.org/moz/jssubscript-loader;1' ]
.getService(Components.interfaces.mozIJSSubScriptLoader)
.loadSubScript( path_to_file , object_where_file_will_be_loaded ); }
}
var our_scope_object = {};
// USING:
JS_Loader( 'some_path to chrome:// or resource://' , our_scope_object);
/*
, "" 'some_path' our_scope_object.
, 'some_path' this our_scope_object
*/
* This source code was highlighted with Source Code Highlighter .
If this option is not satisfactory due to the well-known "glitches", and, by the way, due to the fact that the Warning fails during validation, then you should do it "completely correctly" through:
Components.utils.import( path_to_file , object_where_file_will_be_loaded )
* This source code was highlighted with Source Code Highlighter .
Although, there will be a little bit other "troubles".
- In general, it is possible, of course, to remove all actions for “hanging” “ addEventListener ” for controls into a separate function ... But then you need to remember that there are “incidents” if you try to register events for non-existent controls.
Among other things, this will help us !:
window.addEventListener( "aftercustomization" , function (evt){
[ , <br> , toolbar ]
}, false );
* This source code was highlighted with Source Code Highlighter .
Finally, let a couple of "pleasant" little things:
Work with properties "extensions".var ADDON_ID = '[ em:id install.rdf ]' ;
var EXTENSION = {};
if (Application.extensions){ EXTENSION = Application.extensions.get(ADDON_ID); }
else { Application.getExtensions( function (extensions){ EXTENSION = extensions.get(ADDON_ID); }); }
return {
ext : function (){ return EXTENSION; }
, ver : function (){ return EXTENSION.version; }
}
* This source code was highlighted with Source Code Highlighter .
File system extension path:var addonLocation = '' ;
Components.utils.import( "resource://gre/modules/AddonManager.jsm" );
AddonManager.getAddonByID( '[ em:id install.rdf ]' , function (addon) {
addonLocation = addon.getResourceURI( "" ).QueryInterface(Components.interfaces.nsIFileURL).file;
} );
* This source code was highlighted with Source Code Highlighter .
Twinkler ...var blinker = function (cnt){
for ( var i = 1; i < cnt; i++){
( function (ist){
// window.alert('' + ist + ' ' + (ist % 2));
window.setTimeout( function (){ show_status((ist % 2)); }, ist*450 );
})(i);
}
}
/*
show_status, , 1 0
"" , - ""
*/
* This source code was highlighted with Source Code Highlighter .
In conclusion, I would like to give a couple of references:
Does not need comments:
addons.mozilla.org/en-US/developersMozilla Developer Network:
developer.mozilla.org/en-USWe look through "this" with care .:
XUL Specification:
developer.mozilla.org/en/XULdeveloper.mozilla.org/en/JavaScriptdeveloper.mozilla.org/en/DOMdeveloper.mozilla.org/en/XUL_Overlaysdeveloper.mozilla.org/en/XUL_controlsAnd this is sure to review !:
developer.mozilla.org/en/Setting_up_extension_development_environmentdeveloper.mozilla.org/en/Extension_Frequently_Asked_Questionsdeveloper.mozilla.org/en/Creating_toolbar_buttonsdeveloper.mozilla.org/en/XUL_School/Adding_Toolbars_and_Toolbar_Buttonsdeveloper.mozilla.org/en/XUL_School/Adding_Events_and_Commandsdeveloper.mozilla.org/en/Addons/Add-on_Manager/Code_Samplesdeveloper.mozilla.org/en/chrome_registrationdeveloper.mozilla.org/en/XPCOM_Interface_Reference/mozIJSSubScriptLoaderAnd about this sometime, probably, we will remember ...:
developer.mozilla.org/en/Code_snippetsdeveloper.mozilla.org/en/XUL_School/Handling_Preferencesdeveloper.mozilla.org/en/Security_best_practices_in_extensions