uBlock/js/messaging-handlers.js

512 lines
12 KiB
JavaScript
Raw Normal View History

2014-06-24 00:42:43 +02:00
/*******************************************************************************
µBlock - a Chromium browser extension to block requests.
Copyright (C) 2014 Raymond Hill
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-08-02 17:40:27 +02:00
/* global chrome, µBlock, YaMD5 */
2014-06-24 00:42:43 +02:00
/******************************************************************************/
(function() {
// popup.js
/******************************************************************************/
2014-07-02 18:02:29 +02:00
var getStats = function(request) {
2014-06-24 00:42:43 +02:00
var µb = µBlock;
var r = {
globalBlockedRequestCount: µb.localSettings.blockedRequestCount,
globalAllowedRequestCount: µb.localSettings.allowedRequestCount,
tabId: request.tabId,
pageURL: '',
pageBlockedRequestCount: 0,
pageAllowedRequestCount: 0,
netFilteringSwitch: false,
2014-07-02 18:02:29 +02:00
cosmeticFilteringSwitch: false,
2014-07-07 01:14:32 +02:00
logBlockedRequests: µb.userSettings.logBlockedRequests,
logAllowedRequests: µb.userSettings.logAllowedRequests
2014-06-24 00:42:43 +02:00
};
var pageStore = µb.pageStoreFromTabId(request.tabId);
if ( pageStore ) {
r.pageURL = pageStore.pageURL;
r.pageHostname = pageStore.pageHostname;
r.pageBlockedRequestCount = pageStore.perLoadBlockedRequestCount;
r.pageAllowedRequestCount = pageStore.perLoadAllowedRequestCount;
2014-08-02 17:40:27 +02:00
r.netFilteringSwitch = pageStore.getNetFilteringSwitch();
2014-06-24 00:42:43 +02:00
}
return r;
};
/******************************************************************************/
var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
switch ( request.what ) {
case 'stats':
response = getStats(request);
break;
case 'toggleNetFiltering':
µBlock.toggleNetFilteringSwitch(
2014-08-02 17:40:27 +02:00
request.url,
request.scope,
2014-06-24 00:42:43 +02:00
request.state
);
2014-07-07 01:14:32 +02:00
µBlock.updateBadgeAsync(request.tabId);
2014-06-24 00:42:43 +02:00
break;
2014-07-13 02:32:44 +02:00
case 'gotoPick':
chrome.tabs.executeScript(request.tabId, { file: 'js/element-picker.js' });
break;
2014-06-24 00:42:43 +02:00
default:
return µBlock.messaging.defaultHandler(request, sender, callback);
}
callback(response);
};
µBlock.messaging.listen('popup.js', onMessage);
})();
/******************************************************************************/
2014-06-27 23:06:42 +02:00
// contentscript-start.js
2014-06-24 00:42:43 +02:00
(function() {
2014-08-02 17:40:27 +02:00
var µb = µBlock;
2014-06-24 00:42:43 +02:00
var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
2014-06-24 01:23:36 +02:00
var pageStore;
if ( sender && sender.tab ) {
2014-08-02 17:40:27 +02:00
pageStore = µb.pageStoreFromTabId(sender.tab.id);
2014-06-24 01:23:36 +02:00
}
2014-06-24 00:42:43 +02:00
switch ( request.what ) {
case 'retrieveDomainCosmeticSelectors':
2014-08-02 17:40:27 +02:00
if ( pageStore && pageStore.getNetFilteringSwitch() ) {
2014-09-08 23:46:58 +02:00
response = µb.cosmeticFilteringEngine.retrieveDomainSelectors(request);
2014-08-02 17:40:27 +02:00
}
2014-06-24 00:42:43 +02:00
break;
2014-06-27 23:06:42 +02:00
default:
2014-08-02 17:40:27 +02:00
return µb.messaging.defaultHandler(request, sender, callback);
2014-06-27 23:06:42 +02:00
}
callback(response);
};
2014-08-02 17:40:27 +02:00
µb.messaging.listen('contentscript-start.js', onMessage);
2014-06-27 23:06:42 +02:00
})();
/******************************************************************************/
// contentscript-end.js
(function() {
2014-08-02 17:40:27 +02:00
var µb = µBlock;
2014-06-27 23:06:42 +02:00
var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
var pageStore;
if ( sender && sender.tab ) {
2014-08-02 17:40:27 +02:00
pageStore = µb.pageStoreFromTabId(sender.tab.id);
2014-06-27 23:06:42 +02:00
}
switch ( request.what ) {
2014-06-24 00:42:43 +02:00
case 'retrieveGenericCosmeticSelectors':
2014-08-02 17:40:27 +02:00
if ( pageStore && pageStore.getNetFilteringSwitch() ) {
2014-09-08 23:46:58 +02:00
response = µb.cosmeticFilteringEngine.retrieveGenericSelectors(request);
2014-08-02 17:40:27 +02:00
}
2014-06-24 00:42:43 +02:00
break;
case 'injectedSelectors':
2014-09-08 23:46:58 +02:00
µb.cosmeticFilteringEngine.addToSelectorCache(request);
2014-06-27 23:06:42 +02:00
break;
2014-08-15 16:34:13 +02:00
case 'blockedRequests':
response = {
collapse: µb.userSettings.collapseBlocked,
blockedRequests: pageStore ? pageStore.blockedRequests : {}
};
break;
2014-06-29 18:38:18 +02:00
// Check a single request
case 'blockedRequest':
response = {
2014-08-02 17:40:27 +02:00
collapse: µb.userSettings.collapseBlocked,
2014-06-29 18:38:18 +02:00
blocked: pageStore && pageStore.blockedRequests[request.url]
2014-07-02 18:02:29 +02:00
};
2014-06-29 18:38:18 +02:00
break;
2014-06-24 00:42:43 +02:00
default:
2014-08-02 17:40:27 +02:00
return µb.messaging.defaultHandler(request, sender, callback);
2014-06-24 00:42:43 +02:00
}
callback(response);
};
2014-08-02 17:40:27 +02:00
µb.messaging.listen('contentscript-end.js', onMessage);
2014-07-13 02:32:44 +02:00
})();
/******************************************************************************/
// element-picker.js
(function() {
var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
switch ( request.what ) {
case 'i18n':
response = {
create: chrome.i18n.getMessage('pickerCreate'),
pick: chrome.i18n.getMessage('pickerPick'),
quit: chrome.i18n.getMessage('pickerQuit'),
netFilters: chrome.i18n.getMessage('pickerNetFilters'),
cosmeticFilters: chrome.i18n.getMessage('pickerCosmeticFilters')
};
break;
case 'createUserFilter':
µBlock.appendUserFilters(request.filters);
break;
default:
return µBlock.messaging.defaultHandler(request, sender, callback);
}
callback(response);
};
µBlock.messaging.listen('element-picker.js', onMessage);
2014-06-24 00:42:43 +02:00
})();
/******************************************************************************/
// 3p-filters.js
(function() {
2014-07-25 22:12:20 +02:00
var getLists = function(callback) {
var µb = µBlock;
2014-07-26 22:10:20 +02:00
var r = {
available: null,
current: µb.remoteBlacklists,
cosmetic: µb.userSettings.parseAllABPHideFilters,
2014-09-08 23:46:58 +02:00
netFilterCount: µb.netFilteringEngine.getFilterCount(),
cosmeticFilterCount: µb.cosmeticFilteringEngine.getFilterCount(),
autoUpdate: µb.userSettings.autoUpdate,
2014-08-20 16:26:57 +02:00
userFiltersPath: µb.userFiltersPath,
2014-07-26 22:10:20 +02:00
cache: null
};
var onMetadataReady = function(entries) {
2014-07-26 22:10:20 +02:00
r.cache = entries;
if ( r.available ) {
callback(r);
}
};
var onLists = function(lists) {
r.available = lists;
if ( r.cache ) {
callback(r);
}
2014-07-25 22:12:20 +02:00
};
2014-07-26 22:10:20 +02:00
µb.getAvailableLists(onLists);
µb.assets.metadata(onMetadataReady);
2014-07-25 22:12:20 +02:00
};
/******************************************************************************/
2014-06-24 00:42:43 +02:00
var onMessage = function(request, sender, callback) {
var µb = µBlock;
// Async
switch ( request.what ) {
2014-07-25 22:12:20 +02:00
case 'getLists':
return getLists(callback);
2014-06-24 00:42:43 +02:00
case 'readUserUbiquitousBlockRules':
return µb.assets.get(µb.userFiltersPath, callback);
case 'writeUserUbiquitousBlockRules':
return µb.assets.put(µb.userFiltersPath, request.content, callback);
default:
break;
}
// Sync
var response;
switch ( request.what ) {
2014-07-26 22:10:20 +02:00
case 'purgeCache':
µb.assets.purge(request.path);
break;
2014-06-24 00:42:43 +02:00
default:
return µb.messaging.defaultHandler(request, sender, callback);
}
callback(response);
};
µBlock.messaging.listen('3p-filters.js', onMessage);
})();
/******************************************************************************/
// 1p-filters.js
(function() {
var onMessage = function(request, sender, callback) {
var µb = µBlock;
// Async
switch ( request.what ) {
case 'readUserFilters':
return µb.assets.get(µb.userFiltersPath, callback);
case 'writeUserFilters':
return µb.assets.put(µb.userFiltersPath, request.content, callback);
default:
break;
}
// Sync
var response;
switch ( request.what ) {
default:
return µb.messaging.defaultHandler(request, sender, callback);
}
callback(response);
};
µBlock.messaging.listen('1p-filters.js', onMessage);
})();
/******************************************************************************/
2014-07-17 16:52:43 +02:00
// whitelist.js
(function() {
var onMessage = function(request, sender, callback) {
var µb = µBlock;
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
switch ( request.what ) {
case 'getWhitelist':
2014-08-02 17:40:27 +02:00
response = µb.stringFromWhitelist(µb.netWhitelist);
2014-07-17 16:52:43 +02:00
break;
case 'setWhitelist':
2014-08-02 17:40:27 +02:00
µb.netWhitelist = µb.whitelistFromString(request.whitelist);
2014-07-17 16:52:43 +02:00
µb.saveWhitelist();
break;
default:
return µb.messaging.defaultHandler(request, sender, callback);
}
callback(response);
};
µBlock.messaging.listen('whitelist.js', onMessage);
})();
/******************************************************************************/
2014-07-02 18:02:29 +02:00
// stats.js
(function() {
var getPageDetails = function(µb, tabId) {
var r = {
2014-07-07 01:14:32 +02:00
blockedRequests: [],
allowedRequests: [],
2014-07-02 18:02:29 +02:00
hash: ''
};
var pageStore = µb.pageStores[tabId];
if ( !pageStore ) {
return r;
}
2014-07-07 01:14:32 +02:00
var prepareRequests = function(requests, hasher) {
2014-08-27 20:01:38 +02:00
var µburi = µb.URI;
2014-07-07 01:14:32 +02:00
var r = [];
2014-08-27 20:01:38 +02:00
var details, pos, hostname, domain;
2014-07-07 01:14:32 +02:00
for ( var requestURL in requests ) {
if ( requests.hasOwnProperty(requestURL) === false ) {
continue;
}
details = requests[requestURL];
if ( typeof details !== 'string' ) {
continue;
}
hasher.appendStr(requestURL);
hasher.appendStr(details);
pos = details.indexOf('\t');
2014-08-27 20:01:38 +02:00
hostname = µburi.hostnameFromURI(requestURL);
domain = µburi.domainFromHostname(hostname);
if ( domain === '' ) {
domain = hostname;
}
2014-07-07 01:14:32 +02:00
r.push({
type: details.slice(0, pos),
2014-08-27 20:01:38 +02:00
domain: domain,
2014-07-07 01:14:32 +02:00
url: requestURL,
reason: details.slice(pos + 1)
});
2014-07-02 18:02:29 +02:00
}
2014-07-07 01:14:32 +02:00
return r;
2014-07-07 03:52:16 +02:00
};
2014-07-07 01:14:32 +02:00
var hasher = new YaMD5();
if ( µb.userSettings.logBlockedRequests ) {
r.blockedRequests = prepareRequests(pageStore.blockedRequests, hasher);
}
if ( µb.userSettings.logAllowedRequests ) {
r.allowedRequests = prepareRequests(pageStore.allowedRequests, hasher);
2014-07-02 18:02:29 +02:00
}
r.hash = hasher.end();
return r;
};
var onMessage = function(request, sender, callback) {
var µb = µBlock;
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
switch ( request.what ) {
case 'getPageSelectors':
response = Object.keys(µb.pageStores);
break;
case 'getPageDetails':
response = getPageDetails(µb, request.tabId);
break;
default:
return µb.messaging.defaultHandler(request, sender, callback);
}
callback(response);
};
µBlock.messaging.listen('stats.js', onMessage);
})();
/******************************************************************************/
2014-06-24 00:42:43 +02:00
// about.js
(function() {
var onMessage = function(request, sender, callback) {
var µb = µBlock;
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
switch ( request.what ) {
default:
return µb.messaging.defaultHandler(request, sender, callback);
}
callback(response);
};
µBlock.messaging.listen('about.js', onMessage);
})();
// https://www.youtube.com/watch?v=3_WcygKJP1k
2014-06-24 00:42:43 +02:00
/******************************************************************************/