go-style switch-case

This commit is contained in:
gorhill 2015-05-19 13:59:53 -04:00
parent 9dc9116fa9
commit 6c3217d9af
1 changed files with 141 additions and 141 deletions

View File

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
µBlock - a browser extension to block requests. uBlock - a browser extension to block requests.
Copyright (C) 2014 Raymond Hill Copyright (C) 2014-2015 Raymond Hill
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -66,7 +66,7 @@ var onMessage = function(request, sender, callback) {
/* falls through */ /* falls through */
case 'cosmeticFiltersActivated': case 'cosmeticFiltersActivated':
// Net-based cosmetic filters are of no interest for logging purpose. // Net-based cosmetic filters are of no interest for logging purpose.
if ( µb.logger.isObserved() && request.type !== 'net' ) { if ( µb.logger.isEnabled() && request.type !== 'net' ) {
µb.logCosmeticFilters(tabId); µb.logCosmeticFilters(tabId);
} }
break; break;
@ -402,8 +402,8 @@ var µb = µBlock;
var onMessage = function(request, sender, callback) { var onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
default: default:
break; break;
} }
// Sync // Sync
@ -415,14 +415,14 @@ var onMessage = function(request, sender, callback) {
} }
switch ( request.what ) { switch ( request.what ) {
case 'retrieveDomainCosmeticSelectors': case 'retrieveDomainCosmeticSelectors':
if ( pageStore && pageStore.getSpecificCosmeticFilteringSwitch() ) { if ( pageStore && pageStore.getSpecificCosmeticFilteringSwitch() ) {
response = µb.cosmeticFilteringEngine.retrieveDomainSelectors(request); response = µb.cosmeticFilteringEngine.retrieveDomainSelectors(request);
} }
break; break;
default: default:
return vAPI.messaging.UNHANDLED; return vAPI.messaging.UNHANDLED;
} }
callback(response); callback(response);
@ -555,7 +555,7 @@ var µb = µBlock;
/******************************************************************************/ /******************************************************************************/
var logCosmeticFilters = function(tabId, details) { var logCosmeticFilters = function(tabId, details) {
if ( µb.logger.isObserved() === false ) { if ( µb.logger.isEnabled() === false ) {
return; return;
} }
@ -631,60 +631,60 @@ var µb = µBlock;
var onMessage = function(request, sender, callback) { var onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
case 'elementPickerArguments': case 'elementPickerArguments':
var xhr = new XMLHttpRequest(); var xhr = new XMLHttpRequest();
xhr.open('GET', 'epicker.html', true); xhr.open('GET', 'epicker.html', true);
xhr.overrideMimeType('text/html;charset=utf-8'); xhr.overrideMimeType('text/html;charset=utf-8');
xhr.responseType = 'text'; xhr.responseType = 'text';
xhr.onload = function() { xhr.onload = function() {
this.onload = null; this.onload = null;
var i18n = { var i18n = {
bidi_dir: document.body.getAttribute('dir'), bidi_dir: document.body.getAttribute('dir'),
create: vAPI.i18n('pickerCreate'), create: vAPI.i18n('pickerCreate'),
pick: vAPI.i18n('pickerPick'), pick: vAPI.i18n('pickerPick'),
quit: vAPI.i18n('pickerQuit'), quit: vAPI.i18n('pickerQuit'),
netFilters: vAPI.i18n('pickerNetFilters'), netFilters: vAPI.i18n('pickerNetFilters'),
cosmeticFilters: vAPI.i18n('pickerCosmeticFilters'), cosmeticFilters: vAPI.i18n('pickerCosmeticFilters'),
cosmeticFiltersHint: vAPI.i18n('pickerCosmeticFiltersHint') cosmeticFiltersHint: vAPI.i18n('pickerCosmeticFiltersHint')
}; };
var reStrings = /\{\{(\w+)\}\}/g; var reStrings = /\{\{(\w+)\}\}/g;
var replacer = function(a0, string) { var replacer = function(a0, string) {
return i18n[string]; return i18n[string];
};
callback({
frameContent: this.responseText.replace(reStrings, replacer),
target: µb.contextMenuTarget,
clientX: µb.contextMenuClientX,
clientY: µb.contextMenuClientY,
eprom: µb.epickerEprom
});
µb.contextMenuTarget = '';
µb.contextMenuClientX = -1;
µb.contextMenuClientY = -1;
}; };
xhr.send();
return;
default: callback({
break; frameContent: this.responseText.replace(reStrings, replacer),
target: µb.contextMenuTarget,
clientX: µb.contextMenuClientX,
clientY: µb.contextMenuClientY,
eprom: µb.epickerEprom
});
µb.contextMenuTarget = '';
µb.contextMenuClientX = -1;
µb.contextMenuClientY = -1;
};
xhr.send();
return;
default:
break;
} }
// Sync // Sync
var response; var response;
switch ( request.what ) { switch ( request.what ) {
case 'createUserFilter': case 'createUserFilter':
µb.appendUserFilters(request.filters); µb.appendUserFilters(request.filters);
break; break;
case 'elementPickerEprom': case 'elementPickerEprom':
µb.epickerEprom = request; µb.epickerEprom = request;
break; break;
default: default:
return vAPI.messaging.UNHANDLED; return vAPI.messaging.UNHANDLED;
} }
callback(response); callback(response);
@ -760,26 +760,26 @@ var getLists = function(callback) {
var onMessage = function(request, sender, callback) { var onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
case 'getLists': case 'getLists':
return getLists(callback); return getLists(callback);
case 'purgeAllCaches': case 'purgeAllCaches':
return µb.assets.purgeAll(callback); return µb.assets.purgeAll(callback);
default: default:
break; break;
} }
// Sync // Sync
var response; var response;
switch ( request.what ) { switch ( request.what ) {
case 'purgeCache': case 'purgeCache':
µb.assets.purge(request.path); µb.assets.purge(request.path);
break; break;
default: default:
return vAPI.messaging.UNHANDLED; return vAPI.messaging.UNHANDLED;
} }
callback(response); callback(response);
@ -809,22 +809,22 @@ var µb = µBlock;
var onMessage = function(request, sender, callback) { var onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
case 'readUserFilters': case 'readUserFilters':
return µb.assets.get(µb.userFiltersPath, callback); return µb.assets.get(µb.userFiltersPath, callback);
case 'writeUserFilters': case 'writeUserFilters':
return µb.assets.put(µb.userFiltersPath, request.content, callback); return µb.assets.put(µb.userFiltersPath, request.content, callback);
default: default:
break; break;
} }
// Sync // Sync
var response; var response;
switch ( request.what ) { switch ( request.what ) {
default: default:
return vAPI.messaging.UNHANDLED; return vAPI.messaging.UNHANDLED;
} }
callback(response); callback(response);
@ -960,25 +960,25 @@ var µb = µBlock;
var onMessage = function(request, sender, callback) { var onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
default: default:
break; break;
} }
// Sync // Sync
var response; var response;
switch ( request.what ) { switch ( request.what ) {
case 'getWhitelist': case 'getWhitelist':
response = µb.stringFromWhitelist(µb.netWhitelist); response = µb.stringFromWhitelist(µb.netWhitelist);
break; break;
case 'setWhitelist': case 'setWhitelist':
µb.netWhitelist = µb.whitelistFromString(request.whitelist); µb.netWhitelist = µb.whitelistFromString(request.whitelist);
µb.saveWhitelist(); µb.saveWhitelist();
break; break;
default: default:
return vAPI.messaging.UNHANDLED; return vAPI.messaging.UNHANDLED;
} }
callback(response); callback(response);
@ -1123,30 +1123,30 @@ var resetUserData = function() {
var onMessage = function(request, sender, callback) { var onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
case 'backupUserData': case 'backupUserData':
return backupUserData(callback); return backupUserData(callback);
case 'getLocalData': case 'getLocalData':
return getLocalData(callback); return getLocalData(callback);
default: default:
break; break;
} }
// Sync // Sync
var response; var response;
switch ( request.what ) { switch ( request.what ) {
case 'restoreUserData': case 'restoreUserData':
restoreUserData(request); restoreUserData(request);
break; break;
case 'resetUserData': case 'resetUserData':
resetUserData(); resetUserData();
break; break;
default: default:
return vAPI.messaging.UNHANDLED; return vAPI.messaging.UNHANDLED;
} }
callback(response); callback(response);
@ -1176,39 +1176,39 @@ var µb = µBlock;
var onMessage = function(request, sender, callback) { var onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
default: default:
break; break;
} }
// Sync // Sync
var response; var response;
switch ( request.what ) { switch ( request.what ) {
case 'readAll': case 'readAll':
var tabIds = {}, pageStore; var tabIds = {}, pageStore;
var loggerURL = vAPI.getURL('logger-ui.html'); var loggerURL = vAPI.getURL('logger-ui.html');
for ( var tabId in µb.pageStores ) { for ( var tabId in µb.pageStores ) {
pageStore = µb.pageStoreFromTabId(tabId); pageStore = µb.pageStoreFromTabId(tabId);
if ( pageStore === null ) { if ( pageStore === null ) {
continue; continue;
}
if ( pageStore.rawURL.lastIndexOf(loggerURL, 0) === 0 ) {
continue;
}
tabIds[tabId] = pageStore.title;
} }
response = { if ( pageStore.rawURL.lastIndexOf(loggerURL, 0) === 0 ) {
colorBlind: µb.userSettings.colorBlindFriendly, continue;
entries: µb.logger.readAll(), }
maxEntries: µb.userSettings.requestLogMaxEntries, tabIds[tabId] = pageStore.title;
noTabId: vAPI.noTabId, }
tabIds: tabIds, response = {
tabIdsToken: µb.pageStoresToken colorBlind: µb.userSettings.colorBlindFriendly,
}; entries: µb.logger.readAll(),
break; maxEntries: µb.userSettings.requestLogMaxEntries,
noTabId: vAPI.noTabId,
tabIds: tabIds,
tabIdsToken: µb.pageStoresToken
};
break;
default: default:
return vAPI.messaging.UNHANDLED; return vAPI.messaging.UNHANDLED;
} }
callback(response); callback(response);
@ -1244,15 +1244,15 @@ var onMessage = function(request, sender, callback) {
var response; var response;
switch ( request.what ) { switch ( request.what ) {
case 'subscriberData': case 'subscriberData':
response = { response = {
confirmStr: vAPI.i18n('subscriberConfirm'), confirmStr: vAPI.i18n('subscriberConfirm'),
externalLists: µBlock.userSettings.externalLists externalLists: µBlock.userSettings.externalLists
}; };
break; break;
default: default:
return vAPI.messaging.UNHANDLED; return vAPI.messaging.UNHANDLED;
} }
callback(response); callback(response);
@ -1278,20 +1278,20 @@ vAPI.messaging.listen('subscriber.js', onMessage);
var onMessage = function(request, sender, callback) { var onMessage = function(request, sender, callback) {
// Async // Async
switch ( request.what ) { switch ( request.what ) {
default: default:
break; break;
} }
// Sync // Sync
var response; var response;
switch ( request.what ) { switch ( request.what ) {
case 'temporarilyWhitelistDocument': case 'temporarilyWhitelistDocument':
µBlock.webRequest.temporarilyWhitelistDocument(request.hostname); µBlock.webRequest.temporarilyWhitelistDocument(request.hostname);
break; break;
default: default:
return vAPI.messaging.UNHANDLED; return vAPI.messaging.UNHANDLED;
} }
callback(response); callback(response);