From 2eec9bde4980749dc286eba90fc8adf4399d11aa Mon Sep 17 00:00:00 2001 From: Anthony Lieuallen Date: Mon, 11 Jan 2016 16:04:21 -0500 Subject: [PATCH] Inject JS functions in content scope. Fixes #18 --- content/kabl-inserter.js | 23 +++++++++++++++++------ content/kabl-parse.js | 2 ++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/content/kabl-inserter.js b/content/kabl-inserter.js index 08d5529..ba83096 100644 --- a/content/kabl-inserter.js +++ b/content/kabl-inserter.js @@ -57,21 +57,30 @@ gKablInserter.observe=function(aSubject, aTopic, aData) { if (!gKablPrefs.enabled) return; if (0 == gKablRulesObj.injectFunctions.length) return; - var win = aSubject; - // xpcnativewrapper = no expando, so unwrap - win=win.wrappedJSObject || win; + var sandbox = Components.utils.Sandbox(aSubject, { + 'sameZoneAs': aSubject, + 'sandboxPrototype': aSubject, + 'wantXrays': false, + }); + Components.utils.evalInSandbox( + gKablInserter.insertStr + + ';insert('+gKablRulesObj.injectFunctionsStr+');', + sandbox); +}; +(function() { +function insert(aFuncs) { var obj=function(){return arguments.callee;}; obj.__noSuchMethod__=obj; obj.toString=function(){return '';}; - for (var i=0, func=null; func=gKablRulesObj.injectFunctions[i]; i++) { + for (var i=0, func=null; func=aFuncs[i]; i++) { var subObj=obj; var name=func.split('.'); var baseName=name.shift(), subName; // Don't overwrite, if the page already has this object. - if ('undefined'!=typeof win[baseName]) return; + if ('undefined'!=typeof window[baseName]) continue; // Create properties, as necessary. while (subName=name.shift()) { @@ -79,6 +88,8 @@ gKablInserter.observe=function(aSubject, aTopic, aData) { subObj=subObj[subName]; } - win[baseName]=obj; + window[baseName]=obj; } }; +gKablInserter.insertStr=uneval(insert); +})(); diff --git a/content/kabl-parse.js b/content/kabl-parse.js index 96427f0..457d5a2 100644 --- a/content/kabl-parse.js +++ b/content/kabl-parse.js @@ -124,6 +124,7 @@ var gKablRulesObj={ collapse:null, groups:null, injectFunctions:null, + injectFunctionsStr:'[]', /////////////////////////////// METHODS //////////////////////////////////// @@ -267,6 +268,7 @@ var gKablRulesObj={ this.injectFunctions.push( // strip off the quote marks tok2.val.substr(0, tok2.val.length-1).substr(1)); + this.injectFunctionsStr=JSON.stringify(this.injectFunctions); break; } break;