Secured getWrapped from weird errors (Unexpected object).

Fixes #133
This commit is contained in:
kkapsner 2017-10-10 21:11:05 +02:00
parent c5872ef5a1
commit 68fb7730b9
1 changed files with 40 additions and 26 deletions

View File

@ -2,33 +2,47 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
window.scope = {};
function require(module){
const require = function(){
"use strict";
if (module.startsWith("./")){
var scopeName = module.substr(2).replace(/\..+/, "");
return window.scope[scopeName];
}
else if (module === "sdk/getWrapped"){
return function getWrapped(obj){
return obj.wrappedJSObject || obj;
};
}
else if (module === "sdk/simple-prefs"){
return {
prefs: settings,
on: function(key, callback){
browser.storage.onChanged.addListener(function(changes, area){
if (area === "local"){
if (changes.hasOwnProperty(key)){
callback(changes[key].newValue);
window.scope = {};
const scope = window.scope;
function require(module){
if (module.startsWith("./")){
var scopeName = module.substr(2).replace(/\..+/, "");
return scope[scopeName];
}
else if (module === "sdk/getWrapped"){
return function getWrapped(obj){
if (!obj){
return obj;
}
var wrapped;
try {
wrapped = obj.wrappedJSObject || obj;
}
catch (e){
require("./logging").error("getWrapped failed for", obj, ":", e);
wrapped = obj;
}
return wrapped;
};
}
else if (module === "sdk/simple-prefs"){
return {
prefs: settings,
on: function(key, callback){
browser.storage.onChanged.addListener(function(changes, area){
if (area === "local"){
if (changes.hasOwnProperty(key)){
callback(changes[key].newValue);
}
}
}
});
}
};
});
}
};
}
throw new ReferenceError("Unable to get non relative module " + module + "!");
}
throw new ReferenceError("Unable to get non relative module " + module + "!");
}
window.scope.require = require;
return require;
}();