Browse Source

Firefox: improvements for content scripts

pull/2/head
Deathamns 10 years ago
committed by gorhill
parent
commit
5f92ce4599
  1. 36
      platform/firefox/vapi-client.js

36
platform/firefox/vapi-client.js

@ -25,7 +25,7 @@
/******************************************************************************/ /******************************************************************************/
(function() {
(function(self) {
'use strict'; 'use strict';
@ -75,23 +75,17 @@ var messagingConnector = function(response) {
/******************************************************************************/ /******************************************************************************/
var uniqueId = function() {
return Math.random().toString(36).slice(2);
};
/******************************************************************************/
vAPI.messaging = { vAPI.messaging = {
channels: {}, channels: {},
listeners: {}, listeners: {},
requestId: 1, requestId: 1,
connectorId: uniqueId(),
setup: function() { setup: function() {
this.connector = function(msg) { this.connector = function(msg) {
messagingConnector(JSON.parse(msg)); messagingConnector(JSON.parse(msg));
}; };
addMessageListener(this.connectorId, this.connector);
addMessageListener(this.connector);
this.channels['vAPI'] = {}; this.channels['vAPI'] = {};
this.channels['vAPI'].listener = function(msg) { this.channels['vAPI'].listener = function(msg) {
@ -112,7 +106,7 @@ vAPI.messaging = {
return; return;
} }
removeMessageListener(this.connectorId, this.connector);
removeMessageListener();
this.connector = null; this.connector = null;
this.channels = {}; this.channels = {};
this.listeners = {}; this.listeners = {};
@ -132,7 +126,7 @@ vAPI.messaging = {
} }
message = { message = {
channelName: vAPI.messaging.connectorId + '|' + this.channelName,
channelName: self._sandboxId_ + '|' + this.channelName,
msg: message 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() { vAPI.canExecuteContentScript = function() {
return true; return true;
}; };
/******************************************************************************/ /******************************************************************************/
})();
})(this);
/******************************************************************************/ /******************************************************************************/
Loading…
Cancel
Save