2014-11-15 11:15:11 -07:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2014-12-17 13:33:53 -07:00
|
|
|
µBlock - a browser extension to block requests.
|
2014-11-15 11:15:11 -07: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-08 16:36:14 -06:00
|
|
|
Home: https://github.com/chrisaljoudi/uBlock
|
2014-11-15 11:15:11 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
// For non background pages
|
|
|
|
|
2014-11-24 15:20:21 -07:00
|
|
|
/* global self */
|
2014-11-24 08:40:05 -07:00
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-08 11:34:28 -07:00
|
|
|
(function(self) {
|
2014-11-15 11:15:11 -07:00
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-01-14 15:45:55 -07:00
|
|
|
var vAPI = self.vAPI = self.vAPI || {};
|
2015-01-01 19:14:53 -07:00
|
|
|
var chrome = self.chrome;
|
|
|
|
|
2015-04-08 16:36:14 -06:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/456
|
2015-01-01 18:58:19 -07:00
|
|
|
// Already injected?
|
2015-01-01 19:14:53 -07:00
|
|
|
if ( vAPI.vapiClientInjected ) {
|
2015-03-12 15:30:14 -06:00
|
|
|
//console.debug('vapi-client.js already injected: skipping.');
|
2015-01-01 18:58:19 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-11-24 15:20:21 -07:00
|
|
|
|
2015-02-08 11:34:28 -07:00
|
|
|
vAPI.vapiClientInjected = true;
|
|
|
|
vAPI.sessionId = String.fromCharCode(Date.now() % 25 + 97) +
|
|
|
|
Math.random().toString(36).slice(2);
|
2014-11-24 15:20:21 -07:00
|
|
|
vAPI.chrome = true;
|
2014-11-15 11:15:11 -07:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-04-07 03:22:10 -06:00
|
|
|
if (!chrome.runtime) {
|
|
|
|
// Chrome 20-21
|
|
|
|
chrome.runtime = chrome.extension;
|
|
|
|
}
|
|
|
|
else if(!chrome.runtime.onMessage) {
|
|
|
|
// Chrome 22-25
|
|
|
|
chrome.runtime.onMessage = chrome.extension.onMessage;
|
|
|
|
chrome.runtime.sendMessage = chrome.extension.sendMessage;
|
|
|
|
chrome.runtime.onConnect = chrome.extension.onConnect;
|
|
|
|
chrome.runtime.connect = chrome.extension.connect;
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-15 11:15:11 -07:00
|
|
|
var messagingConnector = function(response) {
|
|
|
|
if ( !response ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-24 08:40:05 -07:00
|
|
|
var channels = vAPI.messaging.channels;
|
2014-11-15 11:15:11 -07:00
|
|
|
var channel, listener;
|
|
|
|
|
2014-12-28 13:26:06 -07:00
|
|
|
if ( response.broadcast === true && !response.channelName ) {
|
2014-11-24 08:40:05 -07:00
|
|
|
for ( channel in channels ) {
|
|
|
|
if ( channels.hasOwnProperty(channel) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
listener = channels[channel].listener;
|
2014-11-15 11:15:11 -07:00
|
|
|
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 13:26:06 -07:00
|
|
|
channel = channels[response.channelName];
|
2014-11-15 11:15:11 -07:00
|
|
|
listener = channel && channel.listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( typeof listener === 'function' ) {
|
|
|
|
listener(response.msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-24 15:20:21 -07:00
|
|
|
vAPI.messaging = {
|
2014-11-15 11:15:11 -07:00
|
|
|
port: null,
|
|
|
|
channels: {},
|
|
|
|
listeners: {},
|
|
|
|
requestId: 1,
|
|
|
|
|
|
|
|
setup: function() {
|
2015-02-08 11:34:28 -07:00
|
|
|
this.port = chrome.runtime.connect({name: vAPI.sessionId});
|
2014-11-15 11:15:11 -07:00
|
|
|
this.port.onMessage.addListener(messagingConnector);
|
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
|
|
|
if ( this.port === null ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.port.disconnect();
|
|
|
|
this.port.onMessage.removeListener(messagingConnector);
|
|
|
|
this.port = null;
|
|
|
|
this.channels = {};
|
|
|
|
this.listeners = {};
|
|
|
|
},
|
|
|
|
|
|
|
|
channel: function(channelName, callback) {
|
|
|
|
if ( !channelName ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.channels[channelName] = {
|
2014-12-28 13:26:06 -07:00
|
|
|
channelName: channelName,
|
2014-11-15 11:15:11 -07:00
|
|
|
listener: typeof callback === 'function' ? callback : null,
|
|
|
|
send: function(message, callback) {
|
|
|
|
if ( vAPI.messaging.port === null ) {
|
|
|
|
vAPI.messaging.setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
message = {
|
2014-12-28 13:26:06 -07:00
|
|
|
channelName: this.channelName,
|
2014-11-15 11:15:11 -07:00
|
|
|
msg: message
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( callback ) {
|
|
|
|
message.requestId = vAPI.messaging.requestId++;
|
|
|
|
vAPI.messaging.listeners[message.requestId] = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
vAPI.messaging.port.postMessage(message);
|
|
|
|
},
|
|
|
|
close: function() {
|
2014-12-28 13:26:06 -07:00
|
|
|
delete vAPI.messaging.channels[this.channelName];
|
2015-04-10 12:08:43 -06:00
|
|
|
if ( Object.keys(vAPI.messaging.channels).length === 0 ) {
|
|
|
|
vAPI.messaging.close();
|
|
|
|
}
|
2014-11-15 11:15:11 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.channels[channelName];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-02-08 11:34:28 -07:00
|
|
|
})(this);
|
2014-11-15 11:15:11 -07:00
|
|
|
|
2015-02-10 14:24:04 -07:00
|
|
|
/******************************************************************************/
|