2014-11-24 20:00:27 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-12-17 21:33:53 +01:00
|
|
|
µBlock - a browser extension to block requests.
|
2014-11-24 20:00:27 +01:00
|
|
|
Copyright (C) 2014 The µBlock authors
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
2015-04-09 00:36:14 +02:00
|
|
|
Home: https://github.com/chrisaljoudi/uBlock
|
2014-11-24 20:00:27 +01:00
|
|
|
*/
|
|
|
|
|
2014-12-16 13:44:34 +01:00
|
|
|
/* global addMessageListener, removeMessageListener, sendAsyncMessage */
|
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
// For non background pages
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
(function(self) {
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-08 19:34:28 +01:00
|
|
|
var vAPI = self.vAPI = self.vAPI || {};
|
2014-11-24 20:00:27 +01:00
|
|
|
vAPI.firefox = true;
|
2015-02-08 19:34:28 +01:00
|
|
|
vAPI.sessionId = String.fromCharCode(Date.now() % 25 + 97) +
|
|
|
|
Math.random().toString(36).slice(2);
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var messagingConnector = function(response) {
|
|
|
|
if ( !response ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var channels = vAPI.messaging.channels;
|
|
|
|
var channel, listener;
|
|
|
|
|
2014-12-28 21:26:06 +01:00
|
|
|
if ( response.broadcast === true && !response.channelName ) {
|
2014-11-24 20:00:27 +01:00
|
|
|
for ( channel in channels ) {
|
|
|
|
if ( channels.hasOwnProperty(channel) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
listener = channels[channel].listener;
|
|
|
|
if ( typeof listener === 'function' ) {
|
|
|
|
listener(response.msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( response.requestId ) {
|
|
|
|
listener = vAPI.messaging.listeners[response.requestId];
|
|
|
|
delete vAPI.messaging.listeners[response.requestId];
|
|
|
|
delete response.requestId;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !listener ) {
|
2014-12-28 21:26:06 +01:00
|
|
|
channel = channels[response.channelName];
|
2014-11-24 20:00:27 +01:00
|
|
|
listener = channel && channel.listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( typeof listener === 'function' ) {
|
|
|
|
listener(response.msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.messaging = {
|
|
|
|
channels: {},
|
|
|
|
listeners: {},
|
|
|
|
requestId: 1,
|
|
|
|
|
|
|
|
setup: function() {
|
|
|
|
this.connector = function(msg) {
|
|
|
|
messagingConnector(JSON.parse(msg));
|
|
|
|
};
|
2015-01-08 21:18:05 +01:00
|
|
|
|
|
|
|
addMessageListener(this.connector);
|
2014-12-16 22:31:03 +01:00
|
|
|
|
|
|
|
this.channels['vAPI'] = {};
|
|
|
|
this.channels['vAPI'].listener = function(msg) {
|
2014-12-24 23:11:36 +01:00
|
|
|
if ( msg.cmd === 'injectScript' ) {
|
2014-12-16 22:31:03 +01:00
|
|
|
var details = msg.details;
|
|
|
|
|
2014-12-24 23:11:36 +01:00
|
|
|
if ( !details.allFrames && window !== window.top ) {
|
2014-12-16 22:31:03 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-27 13:31:17 +01:00
|
|
|
self.injectScript(details.file);
|
2014-12-16 22:31:03 +01:00
|
|
|
}
|
|
|
|
};
|
2014-11-24 20:00:27 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
2014-12-24 23:11:36 +01:00
|
|
|
if ( !this.connector ) {
|
|
|
|
return;
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
2014-12-24 23:11:36 +01:00
|
|
|
|
2015-01-08 21:18:05 +01:00
|
|
|
removeMessageListener();
|
2014-12-24 23:11:36 +01:00
|
|
|
this.connector = null;
|
|
|
|
this.channels = {};
|
|
|
|
this.listeners = {};
|
2014-11-24 20:00:27 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
channel: function(channelName, callback) {
|
|
|
|
if ( !channelName ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.channels[channelName] = {
|
2014-12-28 21:26:06 +01:00
|
|
|
channelName: channelName,
|
2014-11-24 20:00:27 +01:00
|
|
|
listener: typeof callback === 'function' ? callback : null,
|
|
|
|
send: function(message, callback) {
|
|
|
|
if ( !vAPI.messaging.connector ) {
|
|
|
|
vAPI.messaging.setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
message = {
|
2015-01-08 21:18:05 +01:00
|
|
|
channelName: self._sandboxId_ + '|' + this.channelName,
|
2014-11-24 20:00:27 +01:00
|
|
|
msg: message
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( callback ) {
|
|
|
|
message.requestId = vAPI.messaging.requestId++;
|
|
|
|
vAPI.messaging.listeners[message.requestId] = callback;
|
|
|
|
}
|
|
|
|
|
2014-12-07 20:51:49 +01:00
|
|
|
sendAsyncMessage('ublock:background', message);
|
2014-11-24 20:00:27 +01:00
|
|
|
},
|
|
|
|
close: function() {
|
2014-12-28 21:26:06 +01:00
|
|
|
delete vAPI.messaging.channels[this.channelName];
|
2014-11-24 20:00:27 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.channels[channelName];
|
2015-01-26 20:26:45 +01:00
|
|
|
},
|
2014-11-24 20:00:27 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
toggleListener: function({type, persisted}) {
|
|
|
|
if ( !vAPI.messaging.connector ) {
|
|
|
|
return;
|
|
|
|
}
|
2015-01-08 21:18:05 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
if ( type === 'pagehide' ) {
|
|
|
|
removeMessageListener();
|
|
|
|
return;
|
|
|
|
}
|
2015-01-09 07:58:07 +01:00
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
if ( persisted ) {
|
|
|
|
addMessageListener(vAPI.messaging.connector);
|
|
|
|
}
|
2015-01-08 21:18:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-26 20:26:45 +01:00
|
|
|
window.addEventListener('pagehide', vAPI.messaging.toggleListener, true);
|
|
|
|
window.addEventListener('pageshow', vAPI.messaging.toggleListener, true);
|
2015-01-08 21:18:05 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})(this);
|
2014-11-24 20:00:27 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|