javascript: namespace pollution
I am trying to submit my addon to the mozilla site but I am getting this damn warning:
The code (in mf_options.js) is pretty simple (and i think the problem is only between the "start storage" and "end storage":
// start Storage
var url = "http://mafiaafire.com";
var ios = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
var ssm = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
.getService(Components.interfaces.nsIScriptSecurityManager);
var dsm = Components.classes["@mozilla.org/dom/storagemanager;1"]
.getService(Components.interfaces.nsIDOMStorageManager);
var uri = ios.newURI(url, "", null);
var principal = ssm.getCodebasePrincipal(uri);
var storage = dsm.getLocalStorageForPrincipal(principal, "");
// end Storage
function display_blocked_list1() {
var list = storage.getItem('domain_list_original');
if (list !== undefined) {
var strSingleLineText = list.replace(new RegExp( " ", "g" ), "<br>" );
var status = document.getElementById("div1");
status.innerHTML = strSingleLineText;
}
var list2 = storage.getItem('domain_list_redirect');
if (list2 !== undefined) {
// Strip out all line breaks.
var strSingleLineText2 = list2.replace(new RegExp( " ", "g" ), "<br>" );
var status2开发者_Go百科 = document.getElementById("div2");
status2.innerHTML = strSingleLineText2;
}
var list3 = storage.getItem('list_expiry_date');
if (list3 !== undefined) {
var dateArray = list3.split(",");
var future_date = new Date(dateArray[0],dateArray[1],dateArray[2]);
future_date.setDate(future_date.getDate()+2);
var status2 = document.getElementById("div3");
status2.innerHTML = future_date;
}
// ##################################################
}
You should definitely have a look at the link. However I also got this message and I'm fairly sure my code does not contain any (polluting) global variables.
But if this is exactly the code you use, then any function and variable you declare will be global. In its simplest case, wrap the code in an anonymous function call:
(function() {
// your code here
}());
If you need a global variable, because you have to call function from XUL elements, make sure you only have one. Create it inside the function call above with
window.YourPluginNamespace = {
// all functions or "subspaces" here
};
Wrap your code in a function envelope so your var
s are local to that function body, and explicitly attach anything you want global to the global object.
(function (global) {
// your code here
global.myGlobalVar = myVar
}(this));
The problem is that you are using too many global variables, those defined outside of a function.
Imagine this scenario: my addon, Foo, uses a variable called sheep
.
var sheep = 10;
Your addon, Bar, uses a variable also called sheep
:
var sheep = 20;
When I go to access sheep
, how can I be assured your addon hasn't modified it? This is the same reason addons use anonymous functions foo = function() {
, because they are local.
To make you global variables more local, wrap your whole script in an anonymous function:
(function() {
var sheep = 10;
}());
Now, you can do whatever you wish with sheep
and it will be local. Keep in mind, though, that you'd need some better scaffolding if you plan on making your application more complex. This method isn't completely bulletproof or scalable...
精彩评论