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';
|
|
|
|
|
2021-02-17 07:01:08 +01:00
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
/**
|
|
|
|
* Interceptor
|
|
|
|
*/
|
|
|
|
|
|
|
|
var interceptor = {};
|
|
|
|
|
2021-02-17 07:01:08 +01:00
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
/**
|
|
|
|
* Public Methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
|
2023-01-20 06:48:18 +01:00
|
|
|
let validCandidate, targetDetails, targetDomain, isGoogleFont, isGoogleMaterialIcons, initiatorDomain, isListed,
|
|
|
|
iframe;
|
|
|
|
|
|
|
|
iframe = '';
|
|
|
|
if (tab.url !== requestDetails.originUrl) {
|
|
|
|
console.log(`${LogString.PREFIX} ${LogString.IFRAME} ${tab.url} -> ${requestDetails.originUrl}`);
|
|
|
|
iframe = requestDetails.originUrl;
|
|
|
|
}
|
2020-02-27 13:45:29 +01:00
|
|
|
|
2022-09-18 08:23:13 +02:00
|
|
|
if (requestDetails['type'] === WebRequestType.MAIN_FRAME &&
|
2022-09-19 06:20:22 +02:00
|
|
|
helpers.checkAllowlisted(
|
|
|
|
helpers.extractDomainFromUrl(tab.url, true),
|
|
|
|
requestAnalyzer.allowlistedDomains
|
|
|
|
)) {
|
2022-09-18 08:23:13 +02:00
|
|
|
return {
|
|
|
|
'cancel': false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-30 05:06:15 +02:00
|
|
|
targetDetails = requestAnalyzer.getLocalTarget(requestDetails, tab.url);
|
2022-07-10 06:55:41 +02:00
|
|
|
if (targetDetails['result'] === 'blocked') {
|
2023-01-30 06:28:07 +01:00
|
|
|
let source, resource;
|
|
|
|
source = helpers.extractDomainFromUrl(tab.url, true);
|
|
|
|
resource = tab.url;
|
2022-07-10 06:55:41 +02:00
|
|
|
return {
|
2023-01-30 06:28:07 +01:00
|
|
|
'redirectUrl': chrome.runtime.getURL(`resources/blocked/index.html?source=${source}&resource=${resource}`)
|
2022-07-10 06:55:41 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-30 05:06:15 +02:00
|
|
|
stateManager.requests[requestDetails.requestId] = {
|
|
|
|
tabIdentifier, targetDetails
|
|
|
|
};
|
|
|
|
|
2022-11-29 07:03:15 +01:00
|
|
|
// Block POST, HEAD, PUT, DELETE, TRACE, OPTIONS
|
|
|
|
if (BlockedRequestMethods[requestDetails.method]) {
|
|
|
|
console.warn(`${LogString.PREFIX} ${LogString.NON_GET_REQUEST_BLOCKED}`);
|
2023-01-20 06:48:18 +01:00
|
|
|
log.append(tab.url, requestDetails.url, LogString.NON_GET_REQUEST_BLOCKED, true, iframe);
|
2022-11-29 07:03:15 +01:00
|
|
|
return {
|
|
|
|
'cancel': true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-09-29 07:10:37 +02:00
|
|
|
if (interceptor._isBadResource(requestDetails.url)) {
|
2021-12-04 07:46:03 +01:00
|
|
|
console.log(`${LogString.PREFIX} ${LogString.EVIL_RESOURCE_BLOCKED} ${requestDetails.url}`);
|
2023-01-20 06:48:18 +01:00
|
|
|
log.append(tab.url, requestDetails.url, '-', true, iframe);
|
2021-09-29 06:50:53 +02:00
|
|
|
return {
|
|
|
|
'cancel': true
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-04 07:46:03 +01:00
|
|
|
targetDomain = helpers.extractDomainFromUrl(requestDetails.url, true);
|
2022-01-30 09:32:53 +01:00
|
|
|
isGoogleFont = requestAnalyzer.isGoogleFont(targetDomain);
|
|
|
|
isGoogleMaterialIcons = requestAnalyzer.isGoogleMaterialIcons(requestDetails.url);
|
2021-03-12 07:22:49 +01:00
|
|
|
|
2022-01-30 09:37:42 +01:00
|
|
|
if (BrowserType.FIREFOX && isGoogleFont && !isGoogleMaterialIcons) {
|
2021-08-07 08:39:10 +02:00
|
|
|
initiatorDomain = helpers.extractDomainFromUrl(tab.url, true);
|
|
|
|
isListed = helpers.checkAllowlisted(initiatorDomain, interceptor.allowedDomainsGoogleFonts);
|
2020-08-22 14:58:57 +02:00
|
|
|
// Check if the website is allowed to load Google Fonts
|
2021-08-07 08:39:10 +02:00
|
|
|
if (interceptor.blockGoogleFonts === true && isListed === false) {
|
2023-07-11 19:21:33 +02:00
|
|
|
console.log(`${LogString.PREFIX} Google fonts blocked ${requestDetails.url}`);
|
|
|
|
log.append(tab.url, requestDetails.url, '-', true, iframe);
|
|
|
|
interceptor._handleMissingCandidate(requestDetails.url, tabIdentifier);
|
|
|
|
++stateManager.tabs[tabIdentifier].blocked;
|
2020-07-03 08:37:00 +02:00
|
|
|
return {
|
2021-11-13 07:20:06 +01:00
|
|
|
'redirectUrl': chrome.runtime.getURL('resources/google-fonts-placeholder.css')
|
2020-07-03 08:37:00 +02:00
|
|
|
};
|
2021-08-07 08:39:10 +02:00
|
|
|
} else if (interceptor.blockGoogleFonts === false || isListed === true) {
|
2020-08-23 07:37:58 +02:00
|
|
|
return {
|
|
|
|
'cancel': false
|
|
|
|
};
|
2020-07-03 08:37:00 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-23 07:37:58 +02:00
|
|
|
|
2021-12-04 07:46:03 +01:00
|
|
|
if (targetDetails['result'] === false) {
|
|
|
|
if (!IgnoredHost[targetDomain]) {
|
|
|
|
++stateManager.tabs[tabIdentifier].missing;
|
|
|
|
}
|
2021-03-13 06:55:20 +01:00
|
|
|
return interceptor._handleMissingCandidate(requestDetails.url, tabIdentifier);
|
2020-02-27 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
2022-09-18 08:23:13 +02:00
|
|
|
console.log(`${LogString.PREFIX} ${LogString.REPLACED_RESOURCE} ${targetDetails.path}`);
|
2023-01-20 06:48:18 +01:00
|
|
|
log.append(tab.url, requestDetails.url, targetDetails.path, false, iframe);
|
2022-09-18 08:23:13 +02:00
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
return {
|
2021-12-04 07:46:03 +01:00
|
|
|
'redirectUrl': chrome.runtime.getURL(targetDetails.path + fileGuard.secret)
|
2020-02-27 13:45:29 +01:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2021-02-17 07:01:08 +01:00
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
/**
|
|
|
|
* Private Methods
|
|
|
|
*/
|
|
|
|
|
2021-03-13 06:55:20 +01:00
|
|
|
interceptor._handleMissingCandidate = function (requestUrl, tabIdentifier) {
|
2023-07-11 19:21:33 +02:00
|
|
|
let requestUrlSegments, injectionCount, missingCount, blockedCount;
|
2020-02-27 13:45:29 +01:00
|
|
|
|
2021-03-13 06:55:20 +01:00
|
|
|
if (stateManager.showIconBadge === true) {
|
2021-03-14 11:03:34 +01:00
|
|
|
injectionCount = Object.keys(stateManager.tabs[tabIdentifier].injections).length || 0;
|
2023-07-13 09:07:02 +02:00
|
|
|
missingCount = stateManager.tabs[tabIdentifier].missing || 0;
|
|
|
|
blockedCount = stateManager.tabs[tabIdentifier].blocked || 0;
|
|
|
|
|
|
|
|
injectionCount = injectionCount + missingCount + blockedCount;
|
|
|
|
|
2021-03-13 06:55:20 +01:00
|
|
|
if (stateManager.changeBadgeColorMissingResources === true) {
|
2023-07-13 09:07:02 +02:00
|
|
|
if (injectionCount === 0) {
|
2021-08-11 06:29:09 +02:00
|
|
|
wrappers.setBadgeText(tabIdentifier, injectionCount);
|
2022-06-30 05:06:15 +02:00
|
|
|
wrappers.setBadgeColoring(tabIdentifier, BadgeSettingMissingResource.TYPE);
|
2021-03-13 06:55:20 +01:00
|
|
|
}
|
|
|
|
} else {
|
2021-08-11 06:29:09 +02:00
|
|
|
wrappers.setBadgeText(tabIdentifier, injectionCount);
|
2021-03-13 06:55:20 +01:00
|
|
|
}
|
2020-02-27 13:45:29 +01:00
|
|
|
}
|
|
|
|
|
2021-03-13 06:55:20 +01:00
|
|
|
if (interceptor.blockMissing === true) {
|
2020-02-27 13:45:29 +01:00
|
|
|
return {
|
2021-03-13 06:55:20 +01:00
|
|
|
'cancel': true
|
2020-02-27 13:45:29 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-02-10 06:36:01 +01:00
|
|
|
if (Setting.ALLOWED_DOMAINS_GOOGLE_FONTS in changes) {
|
|
|
|
interceptor.allowedDomainsGoogleFonts = changes.allowedDomainsGoogleFonts.newValue;
|
2020-08-22 14:58:57 +02:00
|
|
|
}
|
2020-02-27 13:45:29 +01:00
|
|
|
};
|
|
|
|
|
2021-09-29 06:50:53 +02:00
|
|
|
interceptor._isBadResource = function (requestUrl) {
|
|
|
|
requestUrl = requestUrl.replace(/(^\w+:|^)\/\//, '');
|
|
|
|
return Object.keys(BadResources).filter((path) => requestUrl.startsWith(path)).length !== 0;
|
|
|
|
};
|
2021-02-17 07:01:08 +01:00
|
|
|
|
2020-02-27 13:45:29 +01:00
|
|
|
/**
|
|
|
|
* Event Handlers
|
|
|
|
*/
|
|
|
|
|
|
|
|
chrome.storage.onChanged.addListener(interceptor._handleStorageChanged);
|