/
browser.es
89 lines (79 loc) · 2.56 KB
/
browser.es
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import CliqzHumanWeb from 'human-web/human-web';
Cu.import("resource://gre/modules/Services.jsm");
export class Window {
constructor(window) {
this.window = window;
}
get id() {
const util = this.window.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindowUtils);
return util.outerWindowID;
}
}
export let currentURL = CliqzHumanWeb.currentURL;
export function contextFromEvent() {
return CliqzHumanWeb.contextFromEvent;
}
export function mapWindows(callback) {
const enumerator = Services.wm.getEnumerator('navigator:browser');
const results = []
while (enumerator.hasMoreElements()) {
try {
var win = enumerator.getNext();
results.push(callback(win));
} catch(e) {}
}
return results;
}
export function forEachWindow(callback) {
mapWindows(callback);
}
/** Returns true if the give windowID represents an open browser tab's windowID.
*/
export function isWindowActive(windowID) {
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var browserEnumerator = wm.getEnumerator("navigator:browser");
// ensure an integer as getBrowserForOuterWindowID() is type sensitive
var int_id = parseInt(windowID);
if(int_id <= 0) return false;
while (browserEnumerator.hasMoreElements()) {
var browserWin = browserEnumerator.getNext();
var tabbrowser = browserWin.gBrowser;
// check if tab is open in this window
// on FF>=39 wm.getOuterWindowWithId() behaves differently to on FF<=38 for closed tabs so we first try
// gBrowser.getBrowserForOuterWindowID which works on FF>=39, and fall back to wm.getOuterWindowWithId()
// for older versions.
try {
var win = tabbrowser.getBrowserForOuterWindowID(int_id)
// check for http URI.
if (win !== undefined) {
return win.currentURI && (win.currentURI.schemeIs('http') || win.currentURI.schemeIs('https'))
}
} catch(e) {
let tabwindow;
try {
tabwindow = wm.getOuterWindowWithId(int_id);
} catch(e) {
// if getOuterWindowWithId randomly fails, keep the tab
return true;
}
if(tabwindow == null) {
return false;
} else {
try {
// check for http URI.
if (tabwindow.document.documentURI.substring(0, 4) === 'http') {
let contents = tabwindow.content || tabwindow._content;
return true;
} else {
return false;
}
} catch(ee) {
return false;
}
}
}
}
return false;
}