Refactor request-analyzer.js
This commit is contained in:
parent
0d8547970e
commit
5f16f4de7a
|
@ -32,6 +32,7 @@
|
|||
"BadgeSettingHTMLFilter": true,
|
||||
"BadgeSettingMissingResource": true,
|
||||
"BadResources": true,
|
||||
"LogString": true,
|
||||
|
||||
"fileGuard": true,
|
||||
"files": true,
|
||||
|
|
|
@ -413,6 +413,15 @@ const BadResources = {
|
|||
'cdnjs.cloudflare.com/ajax/libs/ClientJS/': true,
|
||||
};
|
||||
|
||||
const LogString = {
|
||||
'PREFIX': '[ LocalCDN ]',
|
||||
'FONT_AWESOME': 'Font Awesome is not fully supported by your browser.',
|
||||
'GOOGLE_MATERIAL_ICONS': 'Google Material Icons are not fully supported by your browser.',
|
||||
'YANDEX': 'Workaround. Disable LocalCDN if website and CDN are the same',
|
||||
'REPLACED_RESOURCE': 'Replaced resource:',
|
||||
'MISSING_RESOURCE': 'Missing resource:',
|
||||
};
|
||||
|
||||
// Supported charsets for TextDecoder()
|
||||
// https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder/TextDecoder
|
||||
const EncodingTypes = {
|
||||
|
|
|
@ -47,20 +47,20 @@ 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)) {
|
||||
console.warn('[ LocalCDN ] Font Awesome is not fully supported by your browser.');
|
||||
log.append(tabDetails.url, requestDetails.url, 'Font Awesome is not fully supported by your browser', true);
|
||||
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')) {
|
||||
// also valid for Google Material icons
|
||||
console.warn('[ LocalCDN ] Google Material Icons are not fully supported by your browser.');
|
||||
log.append(tabDetails.url, requestDetails.url, 'Google Material Icons are not fully supported by your browser', true);
|
||||
console.warn(`${LogString.PREFIX} ${LogString.GOOGLE_MATERIAL_ICONS}`);
|
||||
log.append(tabDetails.url, requestDetails.url, LogString.GOOGLE_MATERIAL_ICONS, true);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Disable LocalCDN 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, 'Workaround. Disable LocalCDN if website and CDN are the same', true);
|
||||
log.append(tabDetails.url, requestDetails.url, LogString.YANDEX, true);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -98,7 +98,13 @@ requestAnalyzer.getLocalTarget = function (requestDetails, initiator) {
|
|||
|
||||
// Return either the local target's path or false.
|
||||
// eslint-disable-next-line max-len
|
||||
return requestAnalyzer._findLocalTarget(resourceMappings, basePath, destinationHost, destinationPath, destinationSearchString, initiator);
|
||||
return requestAnalyzer._findLocalTarget(
|
||||
resourceMappings,
|
||||
basePath,
|
||||
destinationHost,
|
||||
destinationPath,
|
||||
destinationSearchString,
|
||||
initiator);
|
||||
};
|
||||
|
||||
|
||||
|
@ -130,12 +136,10 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
|
|||
versionNumber = resourcePath.match(Resource.VERSION_EXPRESSION);
|
||||
|
||||
// Handle weird version expressions
|
||||
if (!versionNumber) {
|
||||
if (Resource.SINGLE_NUMBER_EXPRESSION.test(channelPath)) {
|
||||
if (!versionNumber && Resource.SINGLE_NUMBER_EXPRESSION.test(channelPath)) {
|
||||
versionNumber = channelPath.match(/\d/);
|
||||
resourcePattern = resourcePath.replace(versionNumber, Resource.VERSION_PLACEHOLDER);
|
||||
versionNumber = [`${versionNumber}.0`];
|
||||
}
|
||||
} else {
|
||||
resourcePattern = resourcePath.replace(versionNumber, Resource.VERSION_PLACEHOLDER);
|
||||
}
|
||||
|
@ -143,7 +147,7 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
|
|||
shorthandResource = shorthands.specialFiles(channelHost, channelPath, destinationSearchString);
|
||||
if (shorthandResource) {
|
||||
if (requestAnalyzer.logging) {
|
||||
console.log(`[ LocalCDN ] Replaced resource: ${shorthandResource.path}`);
|
||||
console.log(`${LogString.PREFIX} ${LogString.REPLACED_RESOURCE} ${shorthandResource.path}`);
|
||||
log.append(initiator, channelHost + channelPath, shorthandResource.path, false);
|
||||
}
|
||||
return shorthandResource;
|
||||
|
@ -154,7 +158,9 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
|
|||
}
|
||||
|
||||
for (let resourceMold of Object.keys(resourceMappings)) {
|
||||
if (resourcePattern.startsWith(resourceMold)) {
|
||||
if (!resourcePattern.startsWith(resourceMold)) {
|
||||
continue;
|
||||
}
|
||||
let targetPath, versionDelivered, versionRequested, bundle;
|
||||
targetPath = resourceMappings[resourceMold].path;
|
||||
targetPath = targetPath.replace(Resource.VERSION_PLACEHOLDER, versionNumber);
|
||||
|
@ -176,24 +182,14 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
|
|||
// Get bundle name
|
||||
bundle = targets.determineBundle(targetPath);
|
||||
if (bundle !== '') {
|
||||
filename = channelPath.split('/').pop();
|
||||
if (bundle === 'MathJax (Bundle)' && filename !== 'MathJax.js') {
|
||||
filename = channelPath.replace(Resource.MATHJAX, '');
|
||||
if (!MathJaxFiles[filename]) {
|
||||
console.warn(`[ LocalCDN ] Missing resource: ${channelHost + channelPath}`);
|
||||
log.append(initiator, channelHost + channelPath, '-', true);
|
||||
targetPath = requestAnalyzer._getPathOfBundle(initiator, channelPath, targetPath, bundle);
|
||||
}
|
||||
if (targetPath === false) {
|
||||
break;
|
||||
}
|
||||
if (filename === 'config/TeX-AMS_HTML.js') {
|
||||
filename = 'config/TeX-AMS_HTML-full.js';
|
||||
}
|
||||
}
|
||||
targetPath = (filename.endsWith('.js')) ? `${targetPath + filename}m` : targetPath + filename;
|
||||
targetPath = helpers.formatFilename(targetPath);
|
||||
}
|
||||
|
||||
if (requestAnalyzer.logging) {
|
||||
console.log(`[ LocalCDN ] Replaced resource: ${targetPath}`);
|
||||
console.log(`${LogString.PREFIX} ${LogString.REPLACED_RESOURCE} ${targetPath}`);
|
||||
log.append(initiator, channelHost + channelPath, targetPath, false);
|
||||
}
|
||||
// Prepare and return a local target.
|
||||
|
@ -205,15 +201,30 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel
|
|||
'bundle': bundle
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (requestAnalyzer.logging && !IgnoredHost[channelHost]) {
|
||||
console.warn(`[ LocalCDN ] Missing resource: ${channelHost}${channelPath}`);
|
||||
console.warn(`${LogString.PREFIX} ${LogString.MISSING_RESOURCE} ${channelHost}${channelPath}`);
|
||||
log.append(initiator, channelHost + channelPath, '-', true);
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
requestAnalyzer._getPathOfBundle = function (initiator, channelPath, targetPath, bundle) {
|
||||
let filename = channelPath.split('/').pop();
|
||||
if (bundle === 'MathJax (Bundle)' && filename !== 'MathJax.js') {
|
||||
filename = channelPath.replace(Resource.MATHJAX, '');
|
||||
if (!MathJaxFiles[filename]) {
|
||||
console.warn(`${LogString.PREFIX} ${LogString.MISSING_RESOURCE} ${channelHost + channelPath}`);
|
||||
log.append(initiator, channelHost + channelPath, '-', true);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return helpers.formatFilename(
|
||||
filename.endsWith('.js')
|
||||
? `${targetPath + filename}m`
|
||||
: targetPath + filename);
|
||||
}
|
||||
|
||||
requestAnalyzer._applyAllowlistedDomains = function () {
|
||||
storageManager.type.get(Setting.ALLOWLISTED_DOMAINS, function (items) {
|
||||
requestAnalyzer.allowlistedDomains = items.allowlistedDomains || {};
|
||||
|
|
Loading…
Reference in New Issue