1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-27 13:42:25 +01:00

Add util.iterFrames.

This commit is contained in:
Doug Kearns
2015-07-06 23:55:51 +10:00
parent 58faec7655
commit 85eede1ee2
2 changed files with 24 additions and 19 deletions

View File

@@ -363,17 +363,13 @@ var Buffer = Module("Buffer", {
/**
* Returns a list of all frames in the given window or current buffer.
*/
allFrames: function allFrames(win, focusedFirst) {
let frames = [];
(function rec(frame) {
if (true || frame.document.body instanceof Ci.nsIDOMHTMLBodyElement)
frames.push(frame);
Array.forEach(frame.frames, rec);
})(win || this.win);
allFrames: function allFrames(win=this.win, focusedFirst) {
let frames = iter(util.iterFrames(win)).toArray();
if (focusedFirst)
return frames.filter(f => f === this.focusedFrame).concat(
frames.filter(f => f !== this.focusedFrame));
return frames;
},
@@ -2337,7 +2333,7 @@ var Buffer = Module("Buffer", {
if (count >= 1 || !elem || !events.isContentNode(elem)) {
let xpath = ["frame", "iframe", "input", "xul:textbox", "textarea[not(@disabled) and not(@readonly)]"];
let frames = buffer.allFrames(null, true);
let frames = buffer.allFrames(undefined, true);
let elements = Ary.flatten(frames.map(win => [m for (m of DOM.XPath(xpath, win.document))]))
.filter(function (elem) {

View File

@@ -1704,6 +1704,19 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
}
},
/**
* Iterates, depth-first, over the given window and all of its descendant
* frames.
*
* @param {nsIDOMWindow} win The root window.
* @returns {Generator(nsIDOMWindow)}
*/
iterFrames: function* iterFrames(win) {
yield win;
for (let i = 0; i < win.frames.length; i++)
yield* iterFrames(win.frames[i]);
},
/**
* Returns a list of all domains and subdomains of documents in the
* given window and all of its descendant frames.
@@ -1712,16 +1725,14 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
* @returns {[string]} The visible domains.
*/
visibleHosts: function visibleHosts(win) {
let res = [],
seen = new RealSet;
(function rec(frame) {
let res = [], seen = new RealSet;
for (let frame of this.iterFrames(win)) {
try {
if (frame.location.hostname)
res = res.concat(util.subdomains(frame.location.hostname));
}
catch (e) {}
Array.forEach(frame.frames, rec);
})(win);
}
return res.filter(h => !seen.add(h));
},
@@ -1733,15 +1744,13 @@ var Util = Module("Util", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]),
* @returns {[nsIURI]} The visible URIs.
*/
visibleURIs: function visibleURIs(win) {
let res = [],
seen = new RealSet;
(function rec(frame) {
let res = [], seen = new RealSet;
for (let frame of this.iterFrames(win)) {
try {
res = res.concat(util.newURI(frame.location.href));
res.push(util.newURI(frame.location.href));
}
catch (e) {}
Array.forEach(frame.frames, rec);
})(win);
}
return res.filter(h => !seen.add(h.spec));
},