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

150 lines
3.9 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, targetPath, targetDetailURL;
2020-02-27 13:45:29 +01:00
validCandidate = requestAnalyzer.isValidCandidate(requestDetails, tab);
if (!validCandidate) {
return {
'cancel': false
};
}
targetDetails = requestAnalyzer.getLocalTarget(requestDetails, tab.url);
targetPath = targetDetails.path;
2020-08-23 07:37:58 +02:00
if (Regex.GOOGLE_FONTS.test(requestDetails.url)) {
let initiatorDomain, isListed;
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 {
2020-08-03 13:28:52 +02:00
'cancel': true
};
} 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
targetDetailURL = helpers.extractDomainFromUrl(requestDetails.url, true);
if (targetDetails === false && !IgnoredHost[targetDetailURL]) {
2021-03-13 07:41:17 +01:00
++stateManager.tabs[tabIdentifier].missing;
}
2020-03-28 19:19:18 +01:00
if (!targetDetails) {
2021-03-13 06:55:20 +01:00
return interceptor._handleMissingCandidate(requestDetails.url, tabIdentifier);
2020-02-27 13:45:29 +01:00
}
stateManager.requests[requestDetails.requestId] = {
tabIdentifier, targetDetails
};
return {
2021-04-11 08:03:42 +02:00
'redirectUrl': chrome.runtime.getURL(targetPath + 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);
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-02-17 07:01:08 +01:00
2020-02-27 13:45:29 +01:00
/**
* Event Handlers
*/
chrome.storage.onChanged.addListener(interceptor._handleStorageChanged);