mirror of
https://github.com/gorhill/uMatrix.git
synced 2024-06-01 18:10:17 +12:00
Firefox: improvements for content scripts
This commit is contained in:
parent
3646ecbeb2
commit
9317615bc7
|
@ -19,51 +19,11 @@
|
|||
Home: https://github.com/gorhill/uBlock
|
||||
*/
|
||||
|
||||
/* globals addMessageListener, removeMessageListener */
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
// https://bugzil.la/673569
|
||||
|
||||
(function(frameScriptContext) {
|
||||
|
||||
'use strict';
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
let appName = Components.stack.filename.match(/:\/\/([^\/]+)/)[1];
|
||||
let listeners = {};
|
||||
|
||||
Components.utils['import'](Components.stack.filename.replace('Script', 'Module'), {});
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
frameScriptContext[appName + '_addMessageListener'] = function(id, fn) {
|
||||
frameScriptContext[appName + '_removeMessageListener'](id);
|
||||
listeners[id] = function(msg) {
|
||||
fn(msg.data);
|
||||
};
|
||||
addMessageListener(id, listeners[id]);
|
||||
};
|
||||
|
||||
frameScriptContext[appName + '_removeMessageListener'] = function(id) {
|
||||
if ( listeners[id] ) {
|
||||
removeMessageListener(id, listeners[id]);
|
||||
}
|
||||
|
||||
delete listeners[id];
|
||||
};
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
addMessageListener(appName + ':broadcast', function(msg) {
|
||||
for ( let id in listeners ) {
|
||||
listeners[id](msg);
|
||||
}
|
||||
});
|
||||
|
||||
/******************************************************************************/
|
||||
|
||||
})(this);
|
||||
Components.utils.import(
|
||||
Components.stack.filename.replace('Script', 'Module'),
|
||||
null
|
||||
);
|
||||
|
||||
/******************************************************************************/
|
||||
|
|
Loading…
Reference in a new issue