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