1
0
Fork 0
mirror of https://github.com/gorhill/uMatrix.git synced 2024-06-02 02:14:52 +12:00
uMatrix/src/js/messaging.js

961 lines
27 KiB
JavaScript
Raw Normal View History

2014-10-18 08:01:09 +13:00
/*******************************************************************************
2017-11-22 04:36:08 +13:00
uMatrix - a Chromium browser extension to black/white list requests.
2018-01-07 12:00:28 +13:00
Copyright (C) 2014-2018 Raymond Hill
2014-10-18 08:01:09 +13:00
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/uMatrix
*/
'use strict';
2014-10-18 08:01:09 +13:00
/******************************************************************************/
2015-04-12 09:15:57 +12:00
/******************************************************************************/
// Default handler
2014-10-18 08:01:09 +13:00
2015-04-12 09:15:57 +12:00
(function() {
var µm = µMatrix;
2014-10-18 08:01:09 +13:00
/******************************************************************************/
2015-04-12 09:15:57 +12:00
// Default is for commonly used message.
function onMessage(request, sender, callback) {
// Async
switch ( request.what ) {
case 'getAssetContent':
µm.assets.get(request.url, { dontCache: true }, callback);
return;
case 'selectHostsFiles':
µm.selectHostsFiles(request, callback);
return;
2015-04-12 09:15:57 +12:00
default:
break;
}
// Sync
var response;
switch ( request.what ) {
case 'forceReloadTab':
2017-11-20 12:18:02 +13:00
µm.forceReload(request.tabId, request.bypassCache);
2015-04-12 09:15:57 +12:00
break;
2015-05-02 11:27:43 +12:00
case 'forceUpdateAssets':
µm.scheduleAssetUpdater(0);
µm.assets.updateStart({ delay: 2000 });
2015-05-02 11:27:43 +12:00
break;
2015-04-12 09:15:57 +12:00
case 'getUserSettings':
response = {
userSettings: µm.userSettings,
matrixSwitches: {
'https-strict': µm.pMatrix.evaluateSwitch('https-strict', '*') === 1,
'referrer-spoof': µm.pMatrix.evaluateSwitch('referrer-spoof', '*') === 1,
'noscript-spoof': µm.pMatrix.evaluateSwitch('noscript-spoof', '*') === 1
}
};
2015-04-12 09:15:57 +12:00
break;
2014-10-18 08:01:09 +13:00
2015-04-12 09:15:57 +12:00
case 'gotoExtensionURL':
µm.gotoExtensionURL(request);
2015-04-12 09:15:57 +12:00
break;
case 'gotoURL':
µm.gotoURL(request);
2015-04-12 09:15:57 +12:00
break;
2015-05-03 05:30:29 +12:00
case 'mustBlock':
response = µm.mustBlock(
request.scope,
request.hostname,
request.type
);
break;
2018-01-07 12:00:28 +13:00
case 'readRawSettings':
response = µm.stringFromRawSettings();
break;
2015-04-12 09:15:57 +12:00
case 'reloadHostsFiles':
2015-05-02 11:27:43 +12:00
µm.reloadHostsFiles();
break;
case 'setMatrixSwitch':
µm.tMatrix.setSwitch(request.switchName, '*', request.state);
if ( µm.pMatrix.setSwitch(request.switchName, '*', request.state) ) {
µm.saveMatrix();
}
break;
2015-04-12 09:15:57 +12:00
case 'userSettings':
if ( request.hasOwnProperty('value') === false ) {
request.value = undefined;
}
2015-04-12 09:15:57 +12:00
response = µm.changeUserSettings(request.name, request.value);
break;
2018-01-07 12:00:28 +13:00
case 'writeRawSettings':
µm.rawSettingsFromString(request.content);
break;
2015-04-12 09:15:57 +12:00
default:
return vAPI.messaging.UNHANDLED;
}
callback(response);
}
/******************************************************************************/
vAPI.messaging.setup(onMessage);
/******************************************************************************/
})();
/******************************************************************************/
2014-10-18 08:01:09 +13:00
/******************************************************************************/
2015-04-12 09:15:57 +12:00
(function() {
// popup.js
2014-10-18 08:01:09 +13:00
var µm = µMatrix;
/******************************************************************************/
2015-04-12 09:15:57 +12:00
// Constructor is faster than object literal
var RowSnapshot = function(srcHostname, desHostname, desDomain) {
this.domain = desDomain;
this.temporary = µm.tMatrix.evaluateRowZXY(srcHostname, desHostname);
this.permanent = µm.pMatrix.evaluateRowZXY(srcHostname, desHostname);
this.counts = RowSnapshot.counts.slice();
this.totals = RowSnapshot.counts.slice();
2014-10-18 08:01:09 +13:00
};
2015-04-12 09:15:57 +12:00
RowSnapshot.counts = (function() {
2017-11-29 06:33:22 +13:00
var aa = [];
for ( var i = 0, n = µm.Matrix.columnHeaderIndices.size; i < n; i++ ) {
2015-04-12 09:15:57 +12:00
aa[i] = 0;
}
return aa;
})();
2014-10-18 08:01:09 +13:00
/******************************************************************************/
var matrixSnapshot = function(pageStore, details) {
2015-04-12 09:15:57 +12:00
var µmuser = µm.userSettings;
2017-11-29 06:33:22 +13:00
var headerIndices = µm.Matrix.columnHeaderIndices;
2015-04-12 09:15:57 +12:00
var r = {
appVersion: vAPI.app.version,
blockedCount: pageStore.requestStats.blocked.all,
2017-11-30 08:40:18 +13:00
collapseAllDomains: µmuser.popupCollapseAllDomains,
collapseBlacklistedDomains: µmuser.popupCollapseBlacklistedDomains,
diff: [],
domain: pageStore.pageDomain,
has3pReferrer: pageStore.has3pReferrer,
hasMixedContent: pageStore.hasMixedContent,
hasNoscriptTags: pageStore.hasNoscriptTags,
hasWebWorkers: pageStore.hasWebWorkers,
2017-11-29 06:33:22 +13:00
headerIndices: Array.from(headerIndices),
hostname: pageStore.pageHostname,
mtxContentModified: pageStore.mtxContentModifiedTime !== details.mtxContentModifiedTime,
mtxCountModified: pageStore.mtxCountModifiedTime !== details.mtxCountModifiedTime,
mtxContentModifiedTime: pageStore.mtxContentModifiedTime,
mtxCountModifiedTime: pageStore.mtxCountModifiedTime,
pMatrixModified: µm.pMatrix.modifiedTime !== details.pMatrixModifiedTime,
pMatrixModifiedTime: µm.pMatrix.modifiedTime,
2015-04-12 09:15:57 +12:00
pSwitches: {},
rows: {},
rowCount: 0,
scope: '*',
tabId: pageStore.tabId,
tMatrixModified: µm.tMatrix.modifiedTime !== details.tMatrixModifiedTime,
tMatrixModifiedTime: µm.tMatrix.modifiedTime,
tSwitches: {},
url: pageStore.pageUrl,
2015-04-12 09:15:57 +12:00
userSettings: {
colorBlindFriendly: µmuser.colorBlindFriendly,
displayTextSize: µmuser.displayTextSize,
popupScopeLevel: µmuser.popupScopeLevel
}
};
2017-11-22 04:36:08 +13:00
if ( typeof details.scope === 'string' ) {
r.scope = details.scope;
} else if ( µmuser.popupScopeLevel === 'site' ) {
2015-04-12 09:15:57 +12:00
r.scope = r.hostname;
} else if ( µmuser.popupScopeLevel === 'domain' ) {
r.scope = r.domain;
}
2017-11-29 06:33:22 +13:00
for ( var switchName of µm.Matrix.switchNames ) {
2015-04-12 09:15:57 +12:00
r.tSwitches[switchName] = µm.tMatrix.evaluateSwitchZ(switchName, r.scope);
r.pSwitches[switchName] = µm.pMatrix.evaluateSwitchZ(switchName, r.scope);
}
// These rows always exist
r.rows['*'] = new RowSnapshot(r.scope, '*', '*');
r.rows['1st-party'] = new RowSnapshot(r.scope, '1st-party', '1st-party');
r.rowCount += 1;
var µmuri = µm.URI;
var reqType, reqHostname, reqDomain;
2015-04-12 09:15:57 +12:00
var desHostname;
var row, typeIndex;
2017-11-29 06:33:22 +13:00
var anyIndex = headerIndices.get('*');
var pos, count;
2015-04-12 09:15:57 +12:00
for ( var entry of pageStore.hostnameTypeCells ) {
pos = entry[0].indexOf(' ');
reqHostname = entry[0].slice(0, pos);
reqType = entry[0].slice(pos + 1);
2015-04-12 09:15:57 +12:00
// rhill 2013-10-23: hostname can be empty if the request is a data url
// https://github.com/gorhill/httpswitchboard/issues/26
if ( reqHostname === '' ) {
reqHostname = pageStore.pageHostname;
2014-10-18 08:01:09 +13:00
}
2015-04-12 09:15:57 +12:00
reqDomain = µmuri.domainFromHostname(reqHostname) || reqHostname;
// We want rows of self and ancestors
desHostname = reqHostname;
for (;;) {
2015-04-12 09:15:57 +12:00
// If row exists, ancestors exist
if ( r.rows.hasOwnProperty(desHostname) !== false ) { break; }
2015-04-12 09:15:57 +12:00
r.rows[desHostname] = new RowSnapshot(r.scope, desHostname, reqDomain);
r.rowCount += 1;
if ( desHostname === reqDomain ) { break; }
2015-04-12 09:15:57 +12:00
pos = desHostname.indexOf('.');
if ( pos === -1 ) { break; }
2015-04-12 09:15:57 +12:00
desHostname = desHostname.slice(pos + 1);
}
count = entry[1].size;
2017-11-29 06:33:22 +13:00
typeIndex = headerIndices.get(reqType);
2015-04-12 09:15:57 +12:00
row = r.rows[reqHostname];
row.counts[typeIndex] += count;
row.counts[anyIndex] += count;
2015-04-12 09:15:57 +12:00
row = r.rows[reqDomain];
row.totals[typeIndex] += count;
row.totals[anyIndex] += count;
2015-04-12 09:15:57 +12:00
row = r.rows['*'];
row.totals[typeIndex] += count;
row.totals[anyIndex] += count;
2014-10-18 08:01:09 +13:00
}
2015-04-12 09:15:57 +12:00
r.diff = µm.tMatrix.diff(µm.pMatrix, r.hostname, Object.keys(r.rows));
2015-04-12 09:15:57 +12:00
return r;
2014-10-18 08:01:09 +13:00
};
/******************************************************************************/
2015-04-12 09:15:57 +12:00
var matrixSnapshotFromTabId = function(details, callback) {
var matrixSnapshotIf = function(tabId, details) {
var pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore === null ) {
callback('ENOTFOUND');
return;
}
// First verify whether we must return data or not.
if (
µm.tMatrix.modifiedTime === details.tMatrixModifiedTime &&
µm.pMatrix.modifiedTime === details.pMatrixModifiedTime &&
pageStore.mtxContentModifiedTime === details.mtxContentModifiedTime &&
pageStore.mtxCountModifiedTime === details.mtxCountModifiedTime
) {
callback('ENOCHANGE');
return ;
}
callback(matrixSnapshot(pageStore, details));
};
// Specific tab id requested?
2015-04-14 07:05:53 +12:00
if ( details.tabId ) {
matrixSnapshotIf(details.tabId, details);
2015-04-12 09:15:57 +12:00
return;
}
2014-10-18 08:01:09 +13:00
// Fall back to currently active tab
var onTabReady = function(tab) {
2017-11-25 11:22:42 +13:00
if ( tab instanceof Object === false ) {
callback('ENOTFOUND');
return;
}
// Allow examination of behind-the-scene requests
var tabId = tab.url.lastIndexOf(vAPI.getURL('dashboard.html'), 0) !== 0 ?
tab.id :
vAPI.noTabId;
matrixSnapshotIf(tabId, details);
};
vAPI.tabs.get(null, onTabReady);
2015-04-12 09:15:57 +12:00
};
/******************************************************************************/
var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
case 'matrixSnapshot':
matrixSnapshotFromTabId(request, callback);
return;
default:
break;
}
// Sync
var response;
switch ( request.what ) {
case 'toggleMatrixSwitch':
µm.tMatrix.setSwitchZ(
request.switchName,
request.srcHostname,
µm.tMatrix.evaluateSwitchZ(request.switchName, request.srcHostname) === false
);
break;
case 'blacklistMatrixCell':
µm.tMatrix.blacklistCell(
request.srcHostname,
request.desHostname,
request.type
);
break;
case 'whitelistMatrixCell':
µm.tMatrix.whitelistCell(
request.srcHostname,
request.desHostname,
request.type
);
break;
case 'graylistMatrixCell':
µm.tMatrix.graylistCell(
request.srcHostname,
request.desHostname,
request.type
);
break;
case 'applyDiffToPermanentMatrix': // aka "persist"
if ( µm.pMatrix.applyDiff(request.diff, µm.tMatrix) ) {
µm.saveMatrix();
2014-10-18 08:01:09 +13:00
}
2015-04-12 09:15:57 +12:00
break;
case 'applyDiffToTemporaryMatrix': // aka "revert"
µm.tMatrix.applyDiff(request.diff, µm.pMatrix);
break;
case 'revertTemporaryMatrix':
µm.tMatrix.assign(µm.pMatrix);
break;
default:
return vAPI.messaging.UNHANDLED;
2014-10-18 08:01:09 +13:00
}
2015-04-12 09:15:57 +12:00
callback(response);
2014-10-18 08:01:09 +13:00
};
2015-04-12 09:15:57 +12:00
vAPI.messaging.listen('popup.js', onMessage);
})();
/******************************************************************************/
/******************************************************************************/
// content scripts
(function() {
var µm = µMatrix;
2014-10-18 08:01:09 +13:00
/******************************************************************************/
var contentScriptSummaryHandler = function(tabId, pageStore, details) {
2017-12-12 02:31:09 +13:00
if ( pageStore === null ) { return; }
2015-05-16 15:05:37 +12:00
var pageHostname = pageStore.pageHostname;
var µmuri = µm.URI.set(details.documentURI);
2015-04-12 09:15:57 +12:00
var frameURL = µmuri.normalizedURI();
var blocked = details.blocked;
if ( blocked === undefined ) {
blocked = µm.mustBlock(pageHostname, µmuri.hostname, 'script');
}
2015-04-12 09:15:57 +12:00
// https://github.com/gorhill/httpswitchboard/issues/333
// Look-up here whether inline scripting is blocked for the frame.
var url = frameURL + '{inline_script}';
pageStore.recordRequest('script', url, blocked);
µm.logger.writeOne(tabId, 'net', pageHostname, url, 'script', blocked);
2015-05-27 23:51:48 +12:00
// https://github.com/gorhill/uMatrix/issues/225
// A good place to force an update of the page title, as at this point
// the DOM has been loaded.
µm.updateTitle(tabId);
2014-10-18 08:01:09 +13:00
};
/******************************************************************************/
2017-12-10 04:45:37 +13:00
var contentScriptLocalStorageHandler = function(tabId, originURL) {
var tabContext = µm.tabContextManager.lookup(tabId);
if ( tabContext === null ) { return; }
var blocked = µm.mustBlock(
tabContext.rootHostname,
2017-12-10 04:45:37 +13:00
µm.URI.hostnameFromURI(originURL),
'cookie'
2015-04-12 09:15:57 +12:00
);
var pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore !== null ) {
2017-12-10 04:45:37 +13:00
var requestURL = originURL + '/{localStorage}';
pageStore.recordRequest('cookie', requestURL, blocked);
µm.logger.writeOne(tabId, 'net', tabContext.rootHostname, requestURL, 'cookie', blocked);
}
var removeStorage = blocked && µm.userSettings.deleteLocalStorage;
if ( removeStorage ) {
2015-04-12 09:15:57 +12:00
µm.localStorageRemovedCounter++;
}
return removeStorage;
2015-04-12 09:15:57 +12:00
};
2014-10-18 08:01:09 +13:00
2015-04-12 09:15:57 +12:00
/******************************************************************************/
// Evaluate many URLs against the matrix.
var lookupBlockedCollapsibles = function(tabId, requests) {
2018-01-07 12:00:28 +13:00
if ( placeholdersReadTime < µm.rawSettingsWriteTime ) {
placeholders = undefined;
}
if ( placeholders === undefined ) {
placeholders = {
frame: µm.rawSettings.framePlaceholder,
image: µm.rawSettings.imagePlaceholder
};
if ( placeholders.frame ) {
placeholders.frameDocument =
µm.rawSettings.framePlaceholderDocument.replace(
'{{bg}}',
µm.rawSettings.framePlaceholderBackground !== 'default' ?
µm.rawSettings.framePlaceholderBackground :
µm.rawSettings.placeholderBackground
);
}
if ( placeholders.image ) {
placeholders.imageBorder =
µm.rawSettings.imagePlaceholderBorder !== 'default' ?
µm.rawSettings.imagePlaceholderBorder :
µm.rawSettings.placeholderBorder;
placeholders.imageBackground =
µm.rawSettings.imagePlaceholderBackground !== 'default' ?
µm.rawSettings.imagePlaceholderBackground :
µm.rawSettings.placeholderBackground;
}
placeholdersReadTime = Date.now();
}
2015-05-03 23:58:10 +12:00
var response = {
blockedResources: [],
hash: requests.hash,
id: requests.id,
placeholders: placeholders
2015-05-03 23:58:10 +12:00
};
var tabContext = µm.tabContextManager.lookup(tabId);
if ( tabContext === null ) {
2015-05-03 23:58:10 +12:00
return response;
}
2015-05-15 13:04:49 +12:00
var pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore !== null ) {
pageStore.lookupBlockedCollapsibles(requests, response);
}
2015-05-03 23:58:10 +12:00
return response;
};
2018-01-07 12:00:28 +13:00
var placeholders,
placeholdersReadTime = 0;
2015-05-03 23:58:10 +12:00
/******************************************************************************/
2015-04-12 09:15:57 +12:00
var onMessage = function(request, sender, callback) {
2014-10-18 08:01:09 +13:00
// Async
switch ( request.what ) {
2015-04-12 09:15:57 +12:00
default:
break;
}
var tabId = sender && sender.tab ? sender.tab.id || 0 : 0,
tabContext = µm.tabContextManager.lookup(tabId),
2018-01-01 13:25:50 +13:00
rootHostname = tabContext && tabContext.rootHostname,
pageStore = µm.pageStoreFromTabId(tabId);
2015-04-12 09:15:57 +12:00
// Sync
var response;
2014-10-18 08:01:09 +13:00
2015-04-12 09:15:57 +12:00
switch ( request.what ) {
case 'contentScriptHasLocalStorage':
2017-12-10 04:45:37 +13:00
response = contentScriptLocalStorageHandler(tabId, request.originURL);
break;
case 'contentScriptSummary':
contentScriptSummaryHandler(tabId, request);
break;
case 'lookupBlockedCollapsibles':
response = lookupBlockedCollapsibles(tabId, request);
break;
case 'mustRenderNoscriptTags?':
if ( tabContext === null ) { break; }
response =
2018-01-01 13:25:50 +13:00
µm.tMatrix.mustBlock(rootHostname, rootHostname, 'script') &&
µm.tMatrix.evaluateSwitchZ('noscript-spoof', rootHostname);
if ( pageStore !== null ) {
pageStore.hasNoscriptTags = true;
}
break;
case 'securityPolicyViolation':
if ( request.directive === 'worker-src' ) {
var url = µm.URI.hostnameFromURI(request.blockedURI) !== '' ?
request.blockedURI :
request.documentURI;
if ( pageStore !== null ) {
pageStore.hasWebWorkers = true;
pageStore.recordRequest('script', url, true);
}
if ( tabContext !== null ) {
µm.logger.writeOne(tabId, 'net', rootHostname, url, 'worker', request.blocked);
}
} else if ( request.directive === 'script-src' ) {
contentScriptSummaryHandler(tabId, pageStore, request);
}
break;
case 'shutdown?':
if ( tabContext !== null ) {
2018-01-01 13:25:50 +13:00
response = µm.tMatrix.evaluateSwitchZ('matrix-off', rootHostname);
}
break;
2015-04-12 09:15:57 +12:00
default:
return vAPI.messaging.UNHANDLED;
}
callback(response);
};
vAPI.messaging.listen('contentscript.js', onMessage);
2015-04-12 09:15:57 +12:00
/******************************************************************************/
})();
/******************************************************************************/
/******************************************************************************/
// cloud-ui.js
(function() {
/******************************************************************************/
var µm = µMatrix;
/******************************************************************************/
var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
case 'cloudGetOptions':
vAPI.cloud.getOptions(function(options) {
options.enabled = µm.userSettings.cloudStorageEnabled === true;
callback(options);
});
return;
case 'cloudSetOptions':
vAPI.cloud.setOptions(request.options, callback);
return;
case 'cloudPull':
return vAPI.cloud.pull(request.datakey, callback);
case 'cloudPush':
return vAPI.cloud.push(request.datakey, request.data, callback);
default:
break;
}
// Sync
var response;
switch ( request.what ) {
// For when cloud storage is disabled.
case 'cloudPull':
// fallthrough
case 'cloudPush':
break;
default:
return vAPI.messaging.UNHANDLED;
}
callback(response);
};
/******************************************************************************/
vAPI.messaging.listen('cloud-ui.js', onMessage);
})();
/******************************************************************************/
/******************************************************************************/
2015-04-12 09:15:57 +12:00
// user-rules.js
(function() {
var µm = µMatrix;
/******************************************************************************/
var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
switch ( request.what ) {
case 'getUserRules':
response = {
temporaryRules: µm.tMatrix.toString(),
permanentRules: µm.pMatrix.toString()
};
break;
case 'setUserRules':
if ( typeof request.temporaryRules === 'string' ) {
µm.tMatrix.fromString(request.temporaryRules);
}
if ( typeof request.permanentRules === 'string' ) {
µm.pMatrix.fromString(request.permanentRules);
µm.saveMatrix();
}
response = {
temporaryRules: µm.tMatrix.toString(),
permanentRules: µm.pMatrix.toString()
};
break;
default:
return vAPI.messaging.UNHANDLED;
}
callback(response);
};
vAPI.messaging.listen('user-rules.js', onMessage);
})();
2014-10-18 08:01:09 +13:00
/******************************************************************************/
2015-04-12 09:15:57 +12:00
/******************************************************************************/
// hosts-files.js
2014-10-18 08:01:09 +13:00
2015-04-12 09:15:57 +12:00
(function() {
2014-10-18 08:01:09 +13:00
2015-04-12 09:15:57 +12:00
var µm = µMatrix;
/******************************************************************************/
2015-05-02 11:27:43 +12:00
var prepEntries = function(entries) {
var µmuri = µm.URI;
var entry;
for ( var k in entries ) {
if ( entries.hasOwnProperty(k) === false ) {
continue;
}
entry = entries[k];
if ( typeof entry.homeURL === 'string' ) {
entry.homeHostname = µmuri.hostnameFromURI(entry.homeURL);
entry.homeDomain = µmuri.domainFromHostname(entry.homeHostname);
}
}
};
/******************************************************************************/
2015-04-12 09:15:57 +12:00
var getLists = function(callback) {
var r = {
2015-05-02 11:27:43 +12:00
autoUpdate: µm.userSettings.autoUpdate,
2015-04-12 09:15:57 +12:00
available: null,
cache: null,
current: µm.liveHostsFiles,
2015-05-02 11:27:43 +12:00
blockedHostnameCount: µm.ubiquitousBlacklist.count
2015-04-12 09:15:57 +12:00
};
var onMetadataReady = function(entries) {
r.cache = entries;
2015-05-02 11:27:43 +12:00
prepEntries(r.cache);
2015-04-12 09:15:57 +12:00
callback(r);
2014-10-18 08:01:09 +13:00
};
2015-04-12 09:15:57 +12:00
var onAvailableHostsFilesReady = function(lists) {
r.available = lists;
2015-05-02 11:27:43 +12:00
prepEntries(r.available);
2015-04-12 09:15:57 +12:00
µm.assets.metadata(onMetadataReady);
};
µm.getAvailableHostsFiles(onAvailableHostsFilesReady);
};
2014-10-18 08:01:09 +13:00
2015-04-12 09:15:57 +12:00
/******************************************************************************/
var onMessage = function(request, sender, callback) {
var µm = µMatrix;
// Async
switch ( request.what ) {
case 'getLists':
return getLists(callback);
default:
break;
2014-10-18 08:01:09 +13:00
}
2015-04-12 09:15:57 +12:00
// Sync
var response;
switch ( request.what ) {
case 'purgeCache':
µm.assets.purge(request.assetKey);
µm.assets.remove('compiled/' + request.assetKey);
break;
case 'purgeAllCaches':
if ( request.hard ) {
µm.assets.remove(/./);
} else {
µm.assets.purge(/./, 'public_suffix_list.dat');
}
2015-04-12 09:15:57 +12:00
break;
default:
return vAPI.messaging.UNHANDLED;
}
callback(response);
2014-10-18 08:01:09 +13:00
};
2015-04-12 09:15:57 +12:00
vAPI.messaging.listen('hosts-files.js', onMessage);
})();
/******************************************************************************/
2014-10-18 08:01:09 +13:00
/******************************************************************************/
2015-04-12 09:15:57 +12:00
// about.js
(function() {
var µm = µMatrix;
/******************************************************************************/
var restoreUserData = function(userData) {
2018-01-07 12:00:28 +13:00
var countdown = 4;
2015-04-12 09:15:57 +12:00
var onCountdown = function() {
countdown -= 1;
if ( countdown === 0 ) {
vAPI.app.restart();
}
};
var onAllRemoved = function() {
2018-01-07 12:00:28 +13:00
vAPI.storage.set(userData.settings, onCountdown);
vAPI.storage.set({ userMatrix: userData.rules }, onCountdown);
vAPI.storage.set({ liveHostsFiles: userData.hostsFiles }, onCountdown);
if ( userData.rawSettings instanceof Object ) {
µMatrix.saveRawSettings(userData.rawSettings, onCountdown);
}
2015-04-12 09:15:57 +12:00
};
// If we are going to restore all, might as well wipe out clean local
// storage
µm.XAL.keyvalRemoveAll(onAllRemoved);
};
/******************************************************************************/
var resetUserData = function() {
var onAllRemoved = function() {
vAPI.app.restart();
};
µm.XAL.keyvalRemoveAll(onAllRemoved);
};
2014-10-18 08:01:09 +13:00
2015-04-12 09:15:57 +12:00
/******************************************************************************/
var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
switch ( request.what ) {
case 'getAllUserData':
response = {
2015-05-12 07:44:11 +12:00
app: vAPI.app.name,
2015-04-12 09:15:57 +12:00
version: vAPI.app.version,
when: Date.now(),
settings: µm.userSettings,
rules: µm.pMatrix.toString(),
2018-01-07 12:00:28 +13:00
hostsFiles: µm.liveHostsFiles,
rawSettings: µm.rawSettings
2015-04-12 09:15:57 +12:00
};
break;
case 'getSomeStats':
response = {
version: vAPI.app.version,
storageUsed: µm.storageUsed
};
break;
case 'restoreAllUserData':
restoreUserData(request.userData);
break;
case 'resetAllUserData':
resetUserData();
break;
default:
return vAPI.messaging.UNHANDLED;
}
callback(response);
2014-10-18 08:01:09 +13:00
};
2015-04-12 09:15:57 +12:00
vAPI.messaging.listen('about.js', onMessage);
/******************************************************************************/
2014-10-18 08:01:09 +13:00
/******************************************************************************/
// logger-ui.js
(function() {
/******************************************************************************/
var µm = µMatrix,
loggerURL = vAPI.getURL('logger-ui.html');
/******************************************************************************/
var onMessage = function(request, sender, callback) {
// Async
switch ( request.what ) {
default:
break;
}
// Sync
var response;
switch ( request.what ) {
case 'readMany':
if (
µm.logger.ownerId !== undefined &&
request.ownerId !== µm.logger.ownerId
) {
response = { unavailable: true };
break;
}
var tabIds = {};
for ( var tabId in µm.pageStores ) {
var pageStore = µm.pageStoreFromTabId(tabId);
if ( pageStore === null ) { continue; }
if ( pageStore.rawUrl.startsWith(loggerURL) ) { continue; }
tabIds[tabId] = pageStore.title || pageStore.rawUrl;
}
response = {
colorBlind: false,
entries: µm.logger.readAll(request.ownerId),
maxLoggedRequests: µm.userSettings.maxLoggedRequests,
noTabId: vAPI.noTabId,
tabIds: tabIds,
tabIdsToken: µm.pageStoresToken
};
break;
case 'releaseView':
if ( request.ownerId === µm.logger.ownerId ) {
µm.logger.ownerId = undefined;
}
break;
default:
return vAPI.messaging.UNHANDLED;
}
callback(response);
};
vAPI.messaging.listen('logger-ui.js', onMessage);
/******************************************************************************/
})();
/******************************************************************************/
/******************************************************************************/
2014-10-18 08:01:09 +13:00
})();
/******************************************************************************/