Firefox: blocking and content scripts for e10s

pull/2/head
Deathamns 10 years ago committed by gorhill
parent 05f174ab97
commit 41f3579461

@ -1,22 +1,20 @@
/* globals Services, sendAsyncMessage, addMessageListener, removeMessageListener, content */ /* globals Services, sendAsyncMessage, addMessageListener, removeMessageListener */
(function() {
'use strict'; 'use strict';
let appName = 'ublock'; let appName = 'ublock';
let contentBaseURI = 'chrome://' + appName + '/content/js/';
let listeners = {}; let listeners = {};
let frameModule = Components.utils['import']('chrome://' + appName + '/content/frameModule.js', {});
let _addMessageListener = function(id, fn) { this.ublock_addMessageListener = function(id, fn) {
_removeMessageListener(id); ublock_removeMessageListener(id);
listeners[id] = function(msg) { listeners[id] = function(msg) {
fn(msg.data); fn(msg.data);
}; };
addMessageListener(id, listeners[id]); addMessageListener(id, listeners[id]);
}; };
let _removeMessageListener = function(id) { this.ublock_removeMessageListener = function(id) {
if (listeners[id]) { if (listeners[id]) {
removeMessageListener(id, listeners[id]); removeMessageListener(id, listeners[id]);
} }
@ -29,72 +27,3 @@ addMessageListener(appName + ':broadcast', function(msg) {
listeners[id](msg); listeners[id](msg);
} }
}); });
let initContext = function(win, sandbox) {
if (sandbox) {
win = Components.utils.Sandbox([win], {
sandboxPrototype: win,
wantComponents: false,
wantXHRConstructor: false
});
}
win.sendAsyncMessage = sendAsyncMessage;
win.addMessageListener = _addMessageListener;
win.removeMessageListener = _removeMessageListener;
return win;
};
let observer = {
observe: function(win) {
if (!win || win.top !== content) {
return;
}
if (!(win.document instanceof win.HTMLDocument
&& (/^https?:$/.test(win.location.protocol)))) {
return;
}
let lss = Services.scriptloader.loadSubScript;
win = initContext(win, true);
lss(contentBaseURI + 'vapi-client.js', win);
lss(contentBaseURI + 'contentscript-start.js', win);
let readyState = win.document.readyState;
if (readyState === "interactive" || readyState === "complete") {
lss(contentBaseURI + 'contentscript-end.js', win);
}
else {
let docReady = function(e) {
this.removeEventListener(e.type, docReady, true);
lss(contentBaseURI + 'contentscript-end.js', win);
};
win.document.addEventListener('DOMContentLoaded', docReady, true);
}
}
};
Services.obs.addObserver(observer, 'content-document-global-created', 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);
addEventListener('unload', function() {
Services.obs.removeObserver(observer, 'content-document-global-created');
observer = listeners = null;
}, false);
})();
Loading…
Cancel
Save