Firefox: partial vAPI.tabs implementation

pull/2/head
Deathamns 10 years ago committed by gorhill
parent 3b5826fe75
commit ac201e2f8e

@ -4,96 +4,95 @@
'use strict'; 'use strict';
let let appName = 'ublock';
appName = 'ublock', let contentBaseURI = 'chrome://' + appName + '/content/js/';
contentBaseURI = 'chrome://' + appName + '/content/js/', let listeners = {};
listeners = {},
_addMessageListener = function(id, fn) { let _addMessageListener = function(id, fn) {
_removeMessageListener(id); _removeMessageListener(id);
listeners[id] = function(msg) { listeners[id] = function(msg) {
fn(msg.data); fn(msg.data);
}; };
addMessageListener(id, listeners[id]); addMessageListener(id, listeners[id]);
}, };
_removeMessageListener = function(id) {
if (listeners[id]) { let _removeMessageListener = function(id) {
removeMessageListener(id, listeners[id]); if (listeners[id]) {
} removeMessageListener(id, listeners[id]);
}
delete listeners[id];
}; delete listeners[id];
};
addMessageListener('µBlock:broadcast', function(msg) { addMessageListener('µBlock:broadcast', function(msg) {
for (var id in listeners) { for (let id in listeners) {
listeners[id](msg); listeners[id](msg);
} }
}); });
var observer = { let initContext = function(win, sandbox) {
unload: function(e) { if (sandbox) {
Services.obs.removeObserver(observer, 'content-document-global-created'); win = Components.utils.Sandbox([win], {
observer = listeners = null; sandboxPrototype: win,
}, wantComponents: false,
onDOMReady: function(e) { wantXHRConstructor: false
var win = e.target.defaultView; });
}
if (win.location.protocol === 'chrome:' && win.location.host === appName) {
win.sendAsyncMessage = sendAsyncMessage; win.sendAsyncMessage = sendAsyncMessage;
win.addMessageListener = _addMessageListener; win.addMessageListener = _addMessageListener;
win.removeMessageListener = _removeMessageListener; win.removeMessageListener = _removeMessageListener;
}
}, return win;
observe: function(win) { };
if (!win || win.top !== content) {
return; let observer = {
} observe: function(win) {
if (!win || win.top !== content) {
// baseURI is more reliable return;
var location = Services.io.newURI( }
win.location.protocol === 'data:' ? 'data:text/plain,' : win.document.baseURI,
null, if (!(win.document instanceof win.HTMLDocument
null && (/^https?:$/.test(win.location.protocol)))) {
); return;
}
if (!(win.document instanceof win.HTMLDocument
&& (/^https?$/.test(location.scheme)))) { let lss = Services.scriptloader.loadSubScript;
return; win = initContext(win, true);
}
lss(contentBaseURI + 'vapi-client.js', win);
win = Components.utils.Sandbox([win], { lss(contentBaseURI + 'contentscript-start.js', win);
sandboxPrototype: win,
wantComponents: false, if (win.document.readyState === 'loading') {
wantXHRConstructor: false let docReady = function(e) {
}); this.removeEventListener(e.type, docReady, true);
lss(contentBaseURI + 'contentscript-end.js', win);
win.sendAsyncMessage = sendAsyncMessage; };
win.addMessageListener = _addMessageListener;
win.removeMessageListener = _removeMessageListener; win.document.addEventListener('DOMContentLoaded', docReady, true);
}
var lss = Services.scriptloader.loadSubScript; else {
lss(contentBaseURI + 'contentscript-end.js', win);
lss(contentBaseURI + 'vapi-client.js', win); }
lss(contentBaseURI + 'contentscript-start.js', win); }
if (win.document.readyState === 'loading') {
let docReady = function(e) {
this.removeEventListener(e.type, docReady, true);
lss(contentBaseURI + 'contentscript-end.js', win);
};
win.document.addEventListener('DOMContentLoaded', docReady, true);
}
else {
lss(contentBaseURI + 'contentscript-end.js', win);
}
}
}; };
Services.obs.addObserver(observer, 'content-document-global-created', false); Services.obs.addObserver(observer, 'content-document-global-created', false);
addEventListener('unload', observer.unload, false); let DOMReady = function(e) {
let win = e.target.defaultView;
// inject the message handlers for the options page
if (win.location.protocol === 'chrome:' && win.location.host === appName) {
initContext(win);
}
};
addEventListener('DOMContentLoaded', DOMReady, true);
// for the Options page addEventListener('unload', function() {
addEventListener('DOMContentLoaded', observer.onDOMReady, true); Services.obs.removeObserver(observer, 'content-document-global-created');
observer = listeners = null;
}, false);
})(); })();
Loading…
Cancel
Save