2014-11-15 19:15:11 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
µBlock - a Chromium browser extension to block requests.
|
|
|
|
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/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
|
|
|
// For non background pages
|
|
|
|
|
2014-11-24 23:20:21 +01:00
|
|
|
/* global self */
|
2014-11-24 16:40:05 +01:00
|
|
|
|
2014-11-15 19:15:11 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
self.vAPI = self.vAPI || {};
|
2014-11-24 23:20:21 +01:00
|
|
|
|
|
|
|
var chrome = self.chrome;
|
|
|
|
var vAPI = self.vAPI;
|
|
|
|
|
|
|
|
vAPI.chrome = true;
|
2014-11-15 19:15:11 +01:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var messagingConnector = function(response) {
|
|
|
|
if ( !response ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-24 16:40:05 +01:00
|
|
|
var channels = vAPI.messaging.channels;
|
2014-11-15 19:15:11 +01:00
|
|
|
var channel, listener;
|
|
|
|
|
|
|
|
if ( response.broadcast === true ) {
|
2014-11-24 16:40:05 +01:00
|
|
|
for ( channel in channels ) {
|
|
|
|
if ( channels.hasOwnProperty(channel) === false ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
listener = channels[channel].listener;
|
2014-11-15 19:15:11 +01: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-11-24 16:40:05 +01:00
|
|
|
channel = channels[response.portName];
|
2014-11-15 19:15:11 +01:00
|
|
|
listener = channel && channel.listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( typeof listener === 'function' ) {
|
|
|
|
listener(response.msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var uniqueId = function() {
|
|
|
|
return parseInt(Math.random() * 1e10, 10).toString(36);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-24 23:20:21 +01:00
|
|
|
vAPI.messaging = {
|
2014-11-15 19:15:11 +01:00
|
|
|
port: null,
|
|
|
|
channels: {},
|
|
|
|
listeners: {},
|
|
|
|
requestId: 1,
|
|
|
|
connectorId: uniqueId(),
|
|
|
|
|
|
|
|
setup: function() {
|
|
|
|
this.port = chrome.runtime.connect({name: this.connectorId});
|
|
|
|
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] = {
|
|
|
|
portName: channelName,
|
|
|
|
listener: typeof callback === 'function' ? callback : null,
|
|
|
|
send: function(message, callback) {
|
|
|
|
if ( vAPI.messaging.port === null ) {
|
|
|
|
vAPI.messaging.setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
message = {
|
|
|
|
portName: this.portName,
|
|
|
|
msg: message
|
|
|
|
};
|
|
|
|
|
|
|
|
if ( callback ) {
|
|
|
|
message.requestId = vAPI.messaging.requestId++;
|
|
|
|
vAPI.messaging.listeners[message.requestId] = callback;
|
|
|
|
}
|
|
|
|
|
|
|
|
vAPI.messaging.port.postMessage(message);
|
|
|
|
},
|
|
|
|
close: function() {
|
|
|
|
delete vAPI.messaging.channels[this.portName];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.channels[channelName];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-11-24 23:20:21 +01:00
|
|
|
vAPI.canExecuteContentScript = function() {
|
2014-11-15 19:15:11 +01:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|