Skip to content
This repository has been archived by the owner on Oct 12, 2021. It is now read-only.

Commit

Permalink
support nested properties in web-content.inject()
Browse files Browse the repository at this point in the history
  • Loading branch information
lloyd committed May 19, 2011
1 parent 6f43b10 commit 9cf3513
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions modules/lib/web-content.js
Expand Up @@ -293,13 +293,25 @@ exports.scrollTop = function(frame) {
};

/**
* inject a function into a web content window
* Inject a function or property into a web content window.
* @params {IFrameNode} frame An iframe dom node.
* @params {string} attachPoint the property of `window.` to which this function shall be
* attached.
* @params {function} callback The function that will be invoked when content in the
* iframe invokes this function.
* attached. This string *may* include dots '.', in which case it will be interpreted as
* a nested property
* @params {object} obj A property to attach to the specified attach point, a function or object
* may be used.
*/
exports.inject = function(frame, attach, func) {
frame.contentWindow.wrappedJSObject[attach] = func;
var w = frame.contentWindow.wrappedJSObject;
var ar = attach.split(".");
while (ar.length) {
var x = ar.shift();
if (ar.length) {
if (!(w.hasOwnProperty(x))) w[x] = {};
w = w[x];
} else {
w[x] = func;
}
}
w[attach] = func;
};

0 comments on commit 9cf3513

Please sign in to comment.