Skip to content

Commit

Permalink
Inject JS functions in content scope.
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
arantius committed Jan 11, 2016
1 parent 6a041ec commit 2eec9bd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 17 additions & 6 deletions content/kabl-inserter.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,39 @@ 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()) {
subObj[subName]=obj;
subObj=subObj[subName];
}

win[baseName]=obj;
window[baseName]=obj;
}
};
gKablInserter.insertStr=uneval(insert);
})();
2 changes: 2 additions & 0 deletions content/kabl-parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ var gKablRulesObj={
collapse:null,
groups:null,
injectFunctions:null,
injectFunctionsStr:'[]',

/////////////////////////////// METHODS ////////////////////////////////////

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 2eec9bd

Please sign in to comment.