Skip to content

Instantly share code, notes, and snippets.

@CatTail
Last active December 26, 2015 23:18
Show Gist options
  • Select an option

  • Save CatTail/7229216 to your computer and use it in GitHub Desktop.

Select an option

Save CatTail/7229216 to your computer and use it in GitHub Desktop.
Using functional way to grab all windows in a window.
var map = Array.prototype.map.call
, reduce = Array.prototype.reduce.call
, filter = Array.prototype.filter.call
, concat = Array.prototype.concat.call;
/**
* Recursively get all windows of current window.
* @param {Window} win Current window.
* @param {Function} f Extra filter to throw away undesired frame.
* @param {boolean} recursion Whether invocation is in recursion.
* @returns {Array.<Window>}
*/
var getWindows = function (win, f, recursion) {
// when in recursion, filter is already calculated.
f = recursion ? f : _.and(f, function (w) {
try {
// simple invoke to see whether we can access sub window.
return !!(w.location.href || true);
} catch (err) {
return false;
}
});
return _.compose(
// including current window in recursion
_.partialRight(goog.array.reduce, _.lock(goog.array.concat, 2), recursion ? [win] : []),
_.partialRight(goog.array.map, _.lock(_.partialRight(dom.getWindows, f, true), 1)),
_.partialRight(goog.array.filter, f)
)(win.frames);
};
// refer to http://lodash.com/docs and http://docs.closure-library.googlecode.com/git/namespace_goog_functions.html to see how `_` implemented.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment