1
0
mirror of https://codeberg.org/nobody/LocalCDN.git synced 2025-06-05 21:49:31 +02:00

Refactor request handling (#787)

This commit is contained in:
nobody
2021-12-04 07:46:03 +01:00
parent 8f9d501c51
commit f0baf0e81b
4 changed files with 45 additions and 24 deletions

View File

@@ -31,7 +31,7 @@ var requestAnalyzer = {};
*/
requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
let initiatorDomain, isAllowlisted;
let initiatorDomain, requestedDomain, isAllowlisted;
initiatorDomain = helpers.extractDomainFromUrl(tabDetails.url, true);
@@ -39,6 +39,12 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
initiatorDomain = Address.EXAMPLE;
}
// If requested Domain not in mappings.js it is not relevant
requestedDomain = helpers.extractDomainFromUrl(requestDetails.url, true);
if (mappings['cdn'][requestedDomain] === undefined) {
return false;
}
isAllowlisted = helpers.checkAllowlisted(initiatorDomain, requestAnalyzer.allowlistedDomains);
if (isAllowlisted) {
return false;
@@ -46,11 +52,12 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
// Font Awesome injections in Chromium deactivated (https://gitlab.com/nobody42/localcdn/-/issues/67)
if (BrowserType.CHROMIUM) {
if ((/(font-awesome|fontawesome)/).test(requestDetails.url)) {
if (requestDetails.url.includes('font-awesome') || requestDetails.url.includes('fontawesome')) {
console.warn(`${LogString.PREFIX} ${LogString.FONT_AWESOME}`);
log.append(tabDetails.url, requestDetails.url, LogString.FONT_AWESOME, true);
return false;
} else if (requestDetails.url.startsWith('https://fonts.googleapis.com')) {
}
if (requestAnalyzer._isGoogleMaterialIcons(requestedDomain, requestDetails.url)) {
// also valid for Google Material icons
console.warn(`${LogString.PREFIX} ${LogString.GOOGLE_MATERIAL_ICONS}`);
log.append(tabDetails.url, requestDetails.url, LogString.GOOGLE_MATERIAL_ICONS, true);
@@ -58,7 +65,7 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
}
}
// Disable LocalCDN if website is 'yandex.com' and CDN is 'yastatic.net', because website and CDN are the same.
// Ignore requests if website is 'yandex.com' and CDN is 'yastatic.net', because website and CDN are the same.
if (tabDetails.url.includes('yandex.com') && requestDetails.url.includes('yastatic.net')) {
log.append(tabDetails.url, requestDetails.url, LogString.YANDEX, true);
return false;
@@ -68,6 +75,14 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) {
return requestDetails.method === WebRequest.GET;
};
requestAnalyzer.isGoogleMaterialIcons = function (url) {
return url.includes('Material+Icons') || url.includes('materialicons');
};
requestAnalyzer.isGoogleFont = function (domain) {
return domain.includes('fonts.googleapis.com') || domain.includes('fonts.gstatic.com');
};
requestAnalyzer.getLocalTarget = function (requestDetails, initiator) {
let destinationUrl, destinationHost, destinationPath, hostMappings, basePath,
resourceMappings, destinationSearchString;
@@ -155,7 +170,9 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
}
if (resourcePattern === undefined) {
return false;
return {
'result': false,
};
}
for (let resourceMold of Object.keys(resourceMappings)) {
@@ -166,7 +183,9 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
// Replace the requested version with the latest depending on major version
versionDelivered = targets.setLastVersion(targetPath, versionNumber);
if (versionDelivered === false) {
return false;
return {
'result': false,
};
}
versionDelivered = versionDelivered.toString();
targetPath = targetPath.replace(versionNumber, versionDelivered);
@@ -206,7 +225,9 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
console.warn(`${LogString.PREFIX} ${LogString.MISSING_RESOURCE} ${channelHost}${channelPath}`);
log.append(initiator, channelHost + channelPath, '-', true);
}
return false;
return {
'result': false,
};
};
requestAnalyzer._getPathOfBundle = function (initiator, channelHost, channelPath, targetPath, bundle) {