1
0
Fork 0
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:
Deathamns 2015-01-08 21:18:05 +01:00 committed by gorhill
parent 1a1d1b1c4b
commit 5f92ce4599

View file

@ -25,7 +25,7 @@
/******************************************************************************/
(function() {
(function(self) {
'use strict';
@ -75,23 +75,17 @@ var messagingConnector = function(response) {
/******************************************************************************/
var uniqueId = function() {
return Math.random().toString(36).slice(2);
};
/******************************************************************************/
vAPI.messaging = {
channels: {},
listeners: {},
requestId: 1,
connectorId: uniqueId(),
setup: function() {
this.connector = function(msg) {
messagingConnector(JSON.parse(msg));
};
addMessageListener(this.connectorId, this.connector);
addMessageListener(this.connector);
this.channels['vAPI'] = {};
this.channels['vAPI'].listener = function(msg) {
@ -112,7 +106,7 @@ vAPI.messaging = {
return;
}
removeMessageListener(this.connectorId, this.connector);
removeMessageListener();
this.connector = null;
this.channels = {};
this.listeners = {};
@ -132,7 +126,7 @@ vAPI.messaging = {
}
message = {
channelName: vAPI.messaging.connectorId + '|' + this.channelName,
channelName: self._sandboxId_ + '|' + this.channelName,
msg: message
};
@ -154,12 +148,30 @@ vAPI.messaging = {
/******************************************************************************/
var toggleListener = function({type, persisted}) {
if ( !persisted || !vAPI.messaging.connector ) {
return;
}
if ( type === 'pagehide' ) {
removeMessageListener();
}
else {
addMessageListener(vAPI.messaging.connector);
}
};
window.addEventListener('pagehide', toggleListener, true);
window.addEventListener('pageshow', toggleListener, true);
/******************************************************************************/
vAPI.canExecuteContentScript = function() {
return true;
};
/******************************************************************************/
})();
})(this);
/******************************************************************************/