2014-11-24 20:00:27 +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
|
|
|
|
*/
|
|
|
|
|
2014-12-16 13:44:34 +01:00
|
|
|
/* global addMessageListener, removeMessageListener, sendAsyncMessage */
|
|
|
|
|
2014-11-24 20:00:27 +01:00
|
|
|
// For non background pages
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
(function() {
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
self.vAPI = self.vAPI || {};
|
|
|
|
vAPI.firefox = true;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var messagingConnector = function(response) {
|
|
|
|
if ( !response ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var channels = vAPI.messaging.channels;
|
|
|
|
var channel, listener;
|
|
|
|
|
2014-12-16 22:31:03 +01:00
|
|
|
if ( response.broadcast === true && !response.portName ) {
|
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 ) {
|
|
|
|
channel = channels[response.portName];
|
|
|
|
listener = channel && channel.listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( typeof listener === 'function' ) {
|
|
|
|
listener(response.msg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
var uniqueId = function() {
|
2014-12-16 13:44:34 +01:00
|
|
|
return Math.random().toString(36).slice(2);
|
2014-11-24 20:00:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.messaging = {
|
|
|
|
channels: {},
|
|
|
|
listeners: {},
|
|
|
|
requestId: 1,
|
|
|
|
connectorId: uniqueId(),
|
|
|
|
|
|
|
|
setup: function() {
|
|
|
|
this.connector = function(msg) {
|
|
|
|
messagingConnector(JSON.parse(msg));
|
|
|
|
};
|
|
|
|
addMessageListener(this.connectorId, this.connector);
|
2014-12-16 22:31:03 +01:00
|
|
|
|
|
|
|
this.channels['vAPI'] = {};
|
|
|
|
this.channels['vAPI'].listener = function(msg) {
|
|
|
|
if (msg.cmd === 'injectScript') {
|
|
|
|
var details = msg.details;
|
|
|
|
|
|
|
|
if (!details.allFrames && window !== window.top) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.injectScript(details.file || details.code, !details.file);
|
|
|
|
}
|
|
|
|
};
|
2014-11-24 20:00:27 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
close: function() {
|
|
|
|
if (this.connector) {
|
|
|
|
removeMessageListener(this.connectorId, this.connector);
|
|
|
|
this.connector = 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.connector ) {
|
|
|
|
vAPI.messaging.setup();
|
|
|
|
}
|
|
|
|
|
|
|
|
message = {
|
|
|
|
portName: vAPI.messaging.connectorId + '|' + this.portName,
|
|
|
|
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() {
|
|
|
|
delete vAPI.messaging.channels[this.portName];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return this.channels[channelName];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
vAPI.canExecuteContentScript = function() {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|