2014-11-24 12:00:27 -07:00
|
|
|
/* globals Services, sendAsyncMessage, addMessageListener, removeMessageListener, content */
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2014-12-02 00:35:25 -07:00
|
|
|
let appName = 'ublock';
|
|
|
|
let contentBaseURI = 'chrome://' + appName + '/content/js/';
|
|
|
|
let listeners = {};
|
|
|
|
|
|
|
|
let _addMessageListener = function(id, fn) {
|
|
|
|
_removeMessageListener(id);
|
|
|
|
listeners[id] = function(msg) {
|
|
|
|
fn(msg.data);
|
|
|
|
};
|
|
|
|
addMessageListener(id, listeners[id]);
|
|
|
|
};
|
|
|
|
|
|
|
|
let _removeMessageListener = function(id) {
|
|
|
|
if (listeners[id]) {
|
|
|
|
removeMessageListener(id, listeners[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
delete listeners[id];
|
|
|
|
};
|
2014-11-24 12:00:27 -07:00
|
|
|
|
|
|
|
addMessageListener('µBlock:broadcast', function(msg) {
|
2014-12-02 00:35:25 -07:00
|
|
|
for (let id in listeners) {
|
|
|
|
listeners[id](msg);
|
|
|
|
}
|
2014-11-24 12:00:27 -07:00
|
|
|
});
|
|
|
|
|
2014-12-02 00:35:25 -07:00
|
|
|
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);
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2014-11-24 12:47:32 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
Services.obs.addObserver(observer, 'content-document-global-created', false);
|
|
|
|
|
2014-12-02 00:35:25 -07:00
|
|
|
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);
|
2014-11-24 12:47:32 -07:00
|
|
|
|
2014-12-02 00:35:25 -07:00
|
|
|
addEventListener('unload', function() {
|
|
|
|
Services.obs.removeObserver(observer, 'content-document-global-created');
|
|
|
|
observer = listeners = null;
|
|
|
|
}, false);
|
2014-11-24 12:47:32 -07:00
|
|
|
|
2014-11-24 12:00:27 -07:00
|
|
|
})();
|