LocalCDN-Firefox-Chrome-Brave/core/interceptor.js

164 lines
4.8 KiB
JavaScript
Raw Normal View History

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) {
let validCandidate, targetDetails, targetDomain, isGoogleFont, isGoogleMaterialIcons, initiatorDomain, isListed;
2020-02-27 13:45:29 +01:00
targetDetails = requestAnalyzer.getLocalTarget(requestDetails, tab.url);
2022-07-10 06:55:41 +02:00
if (targetDetails['result'] === 'blocked') {
return {
'redirectUrl': chrome.runtime.getURL('resources/blocked/index.html')
};
}
stateManager.requests[requestDetails.requestId] = {
tabIdentifier, targetDetails
};
2020-02-27 13:45:29 +01:00
validCandidate = requestAnalyzer.isValidCandidate(requestDetails, tab);
if (!validCandidate) {
return {
'cancel': false
};
}
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}`);
2021-09-29 06:50:53 +02:00
log.append(tab.url, requestDetails.url, '-', true);
return {
'cancel': true
};
}
2021-12-04 07:46:03 +01:00
targetDomain = helpers.extractDomainFromUrl(requestDetails.url, true);
isGoogleFont = requestAnalyzer.isGoogleFont(targetDomain);
isGoogleMaterialIcons = requestAnalyzer.isGoogleMaterialIcons(requestDetails.url);
2022-01-30 09:37:42 +01:00
if (BrowserType.FIREFOX && isGoogleFont && !isGoogleMaterialIcons) {
initiatorDomain = helpers.extractDomainFromUrl(tab.url, true);
isListed = helpers.checkAllowlisted(initiatorDomain, interceptor.allowedDomainsGoogleFonts);
// Check if the website is allowed to load Google Fonts
if (interceptor.blockGoogleFonts === true && isListed === false) {
return {
2021-11-13 07:20:06 +01:00
'redirectUrl': chrome.runtime.getURL('resources/google-fonts-placeholder.css')
};
} else if (interceptor.blockGoogleFonts === false || isListed === true) {
2020-08-23 07:37:58 +02:00
return {
'cancel': false
};
}
}
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
}
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) {
let requestUrlSegments, injectionCount, missingCount;
2020-02-27 13:45:29 +01:00
2021-03-13 06:55:20 +01:00
if (stateManager.showIconBadge === true) {
injectionCount = Object.keys(stateManager.tabs[tabIdentifier].injections).length || 0;
2021-03-13 06:55:20 +01:00
if (stateManager.changeBadgeColorMissingResources === true) {
missingCount = stateManager.tabs[tabIdentifier].missing || 0;
if (missingCount > 0 && injectionCount === 0) {
wrappers.setBadgeText(tabIdentifier, injectionCount);
wrappers.setBadgeColoring(tabIdentifier, BadgeSettingMissingResource.TYPE);
2021-03-13 06:55:20 +01:00
}
} else {
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();
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;
}
if (Setting.BLOCK_GOOGLE_FONTS in changes) {
interceptor.blockGoogleFonts = changes.blockGoogleFonts.newValue;
}
2021-02-10 06:36:01 +01:00
if (Setting.ALLOWED_DOMAINS_GOOGLE_FONTS in changes) {
interceptor.allowedDomainsGoogleFonts = changes.allowedDomainsGoogleFonts.newValue;
}
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);