2020-02-27 13:45:29 +01:00
|
|
|
/**
|
|
|
|
* Interceptor
|
2020-06-30 18:41:58 +02:00
|
|
|
* Belongs to LocalCDN (since 2020-02-26)
|
|
|
|
* (Origin: Decentraleyes)
|
2020-02-27 13:45:29 +01:00
|
|
|
*
|
|
|
|
* @author Thomas Rientjes
|
|
|
|
* @since 2016-04-06
|
2020-04-14 07:43:25 +02:00
|
|
|
*
|
2020-06-30 18:41:58 +02:00
|
|
|
* @author nobody
|
2020-04-14 07:43:25 +02:00
|
|
|
* @since 2020-02-26
|
|
|
|
*
|
2020-02-27 13:45:29 +01:00
|
|
|
* @license MPL 2.0
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Interceptor
|
|
|
|
*/
|
|
|
|
|
|
|
|
var interceptor = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Public Methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
|
2020-07-04 07:34:31 +02:00
|
|
|
let validCandidate, targetDetails, targetPath;
|
2020-02-27 13:45:29 +01:00
|
|
|
|
|
|
|
validCandidate = requestAnalyzer.isValidCandidate(requestDetails, tab);
|
|
|
|
|
2020-08-19 09:58:28 +02:00
|
|
|
if (!validCandidate) {
|
|
|
|
return {
|
2020-08-22 14:58:57 +02:00
|
|
|
'cancel': false
|
2020-08-19 09:58:28 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-08-22 14:58:57 +02:00
|
|
|
targetDetails = requestAnalyzer.getLocalTarget(requestDetails);
|
|
|
|
targetPath = targetDetails.path;
|
|
|
|
|
2020-08-23 07:37:58 +02:00
|
|
|
if (Regex.GOOGLE_FONTS.test(requestDetails.url)) {
|
2020-08-22 14:58:57 +02:00
|
|
|
let initiatorDomain = helpers.extractDomainFromUrl(tab.url, true);
|
|
|
|
// Check if the website is allowed to load Google Fonts
|
|
|
|
if (interceptor.blockGoogleFonts === true && !requestAnalyzer.domainsGoogleFonts[initiatorDomain]) {
|
2020-07-03 08:37:00 +02:00
|
|
|
return {
|
2020-08-03 13:28:52 +02:00
|
|
|
'cancel': true
|
2020-07-03 08:37:00 +02:00
|
|
|
};
|
2020-08-23 07:37:58 +02:00
|
|
|
} else if (interceptor.blockGoogleFonts === false || requestAnalyzer.domainsGoogleFonts[initiatorDomain]) {
|
|
|
|
return {
|
|
|
|
'cancel': false
|
|
|
|
};
|
2020-07-03 08:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-23 07:37:58 +02:00
|
|
|
|
2020-03-28 19:19:18 +01:00
|
|
|
if (!targetDetails) {
|
2020-02-27 13:45:29 +01:00
|
|
|
return interceptor._handleMissingCandidate(requestDetails.url);
|
|
|
|
}
|
|
|
|
|
|
|
|
stateManager.requests[requestDetails.requestId] = {
|
|
|
|
tabIdentifier, targetDetails
|
|
|
|
};
|
|
|
|
|
|
|
|
return {
|
|
|
|
'redirectUrl': chrome.extension.getURL(targetPath + fileGuard.secret)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Private Methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
interceptor._handleMissingCandidate = function (requestUrl, preserveUrl) {
|
|
|
|
let requestUrlSegments;
|
|
|
|
|
|
|
|
if (interceptor.blockMissing === true) {
|
|
|
|
return {
|
|
|
|
'cancel': true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preserveUrl === true) {
|
|
|
|
return {
|
|
|
|
'cancel': false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
requestUrlSegments = new URL(requestUrl);
|
|
|
|
|
|
|
|
if (requestUrlSegments.protocol === Address.HTTP) {
|
|
|
|
requestUrlSegments.protocol = Address.HTTPS;
|
|
|
|
requestUrl = requestUrlSegments.toString();
|
|
|
|
|
2020-03-28 11:17:02 +01:00
|
|
|
return {
|
|
|
|
'redirectUrl': requestUrl
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
'cancel': false
|
|
|
|
};
|
|
|
|
}
|
2020-02-27 13:45:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
interceptor._handleStorageChanged = function (changes) {
|
|
|
|
if (Setting.XHR_TEST_DOMAIN in changes) {
|
|
|
|
interceptor.xhrTestDomain = changes.xhrTestDomain.newValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Setting.BLOCK_MISSING in changes) {
|
|
|
|
interceptor.blockMissing = changes.blockMissing.newValue;
|
|
|
|
}
|
2020-07-03 08:37:00 +02:00
|
|
|
|
|
|
|
if (Setting.BLOCK_GOOGLE_FONTS in changes) {
|
|
|
|
interceptor.blockGoogleFonts = changes.blockGoogleFonts.newValue;
|
|
|
|
}
|
2020-08-22 14:58:57 +02:00
|
|
|
|
|
|
|
if (Setting.BLOCK_GOOGLE_FONTS in changes) {
|
|
|
|
interceptor.blockGoogleFonts = changes.blockGoogleFonts.newValue;
|
|
|
|
}
|
2020-02-27 13:45:29 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializations
|
|
|
|
*/
|
|
|
|
|
2020-07-04 07:38:59 +02:00
|
|
|
interceptor.xhrTestDomain = Address.LOCALCDN;
|
2020-02-27 13:45:29 +01:00
|
|
|
interceptor.blockMissing = false;
|
2020-07-03 08:37:00 +02:00
|
|
|
interceptor.blockGoogleFonts = true;
|
2020-08-22 14:58:57 +02:00
|
|
|
interceptor.allowedDomainsGoogleFonts = {};
|
2020-02-27 13:45:29 +01:00
|
|
|
|
|
|
|
interceptor.relatedSettings = [];
|
|
|
|
|
|
|
|
interceptor.relatedSettings.push(Setting.AMOUNT_INJECTED);
|
|
|
|
interceptor.relatedSettings.push(Setting.XHR_TEST_DOMAIN);
|
|
|
|
interceptor.relatedSettings.push(Setting.BLOCK_MISSING);
|
2020-08-22 14:58:57 +02:00
|
|
|
interceptor.relatedSettings.push(Setting.ALLOWED_DOMAINS_GOOGLE_FONTS);
|
2020-02-27 13:45:29 +01:00
|
|
|
|
2020-08-30 18:56:36 +02:00
|
|
|
storageManager.type.get(interceptor.relatedSettings, function (items) {
|
2020-09-08 20:50:54 +02:00
|
|
|
storageManager.amountInjected = items.amountInjected || 0;
|
2020-07-04 07:38:59 +02:00
|
|
|
interceptor.xhrTestDomain = items.xhrTestDomain || Address.LOCALCDN;
|
2020-02-27 13:45:29 +01:00
|
|
|
interceptor.blockMissing = items.blockMissing || false;
|
2020-07-03 08:37:00 +02:00
|
|
|
interceptor.blockGoogleFonts = items.blockGoogleFonts || true;
|
2020-08-22 14:58:57 +02:00
|
|
|
interceptor.allowedDomainsGoogleFonts = items.allowedDomainsGoogleFonts || {};
|
2020-02-27 13:45:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event Handlers
|
|
|
|
*/
|
|
|
|
|
|
|
|
chrome.storage.onChanged.addListener(interceptor._handleStorageChanged);
|