From f017311fa2c10be4af1d51aea5c9d5166fb0f217 Mon Sep 17 00:00:00 2001 From: nobody Date: Wed, 19 Aug 2020 09:58:28 +0200 Subject: [PATCH] Fixed: Blocked Google Fonts in Chromium (#80) --- core/interceptor.js | 25 +++++++------------------ core/request-analyzer.js | 18 +++--------------- pages/updates/updates.html | 1 + 3 files changed, 11 insertions(+), 33 deletions(-) diff --git a/core/interceptor.js b/core/interceptor.js index 83669dec..8686cffc 100644 --- a/core/interceptor.js +++ b/core/interceptor.js @@ -29,28 +29,25 @@ var interceptor = {}; */ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) { - let validCandidate, targetDetails, targetPath; validCandidate = requestAnalyzer.isValidCandidate(requestDetails, tab); + if (!validCandidate) { + return { + cancel: false, + }; + } + // Possible URLs of Google Fonts: https://fonts.googleapis.com/css // https://fonts.googleapis.com/css2 if (requestDetails.url.startsWith('https://fonts.googleapis.com/css')) { - if(interceptor.blockGoogleFonts === true || interceptor.blockMissing === true) { + if (interceptor.blockGoogleFonts === true || interceptor.blockMissing === true) { return { 'cancel': true }; } } - - if (!validCandidate) { - - return { - 'cancel': false - }; - } - targetDetails = requestAnalyzer.getLocalTarget(requestDetails); targetPath = targetDetails.path; @@ -72,18 +69,15 @@ interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) { */ interceptor._handleMissingCandidate = function (requestUrl, preserveUrl) { - let requestUrlSegments; if (interceptor.blockMissing === true) { - return { 'cancel': true }; } if (preserveUrl === true) { - return { 'cancel': false }; @@ -92,16 +86,13 @@ interceptor._handleMissingCandidate = function (requestUrl, preserveUrl) { requestUrlSegments = new URL(requestUrl); if (requestUrlSegments.protocol === Address.HTTP) { - requestUrlSegments.protocol = Address.HTTPS; requestUrl = requestUrlSegments.toString(); return { 'redirectUrl': requestUrl }; - } else { - return { 'cancel': false }; @@ -109,7 +100,6 @@ interceptor._handleMissingCandidate = function (requestUrl, preserveUrl) { }; interceptor._handleStorageChanged = function (changes) { - if (Setting.XHR_TEST_DOMAIN in changes) { interceptor.xhrTestDomain = changes.xhrTestDomain.newValue; } @@ -139,7 +129,6 @@ interceptor.relatedSettings.push(Setting.XHR_TEST_DOMAIN); interceptor.relatedSettings.push(Setting.BLOCK_MISSING); chrome.storage.sync.get(interceptor.relatedSettings, function (items) { - interceptor.amountInjected = items.amountInjected || 0; interceptor.xhrTestDomain = items.xhrTestDomain || Address.LOCALCDN; interceptor.blockMissing = items.blockMissing || false; diff --git a/core/request-analyzer.js b/core/request-analyzer.js index 19e4bb82..7d504f96 100644 --- a/core/request-analyzer.js +++ b/core/request-analyzer.js @@ -29,7 +29,6 @@ var requestAnalyzer = {}; */ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) { - let initiatorDomain, isWhitelisted; initiatorDomain = helpers.extractDomainFromUrl(tabDetails.url, true); @@ -45,11 +44,11 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) { } // 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)) { console.warn('[ LocalCDN ] Font Awesome is not fully supported by your browser.'); return false; - } else if (requestDetails.url === 'https://fonts.googleapis.com/icon?family=Material+Icons') { + } 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.'); return false; @@ -66,7 +65,6 @@ requestAnalyzer.isValidCandidate = function (requestDetails, tabDetails) { }; requestAnalyzer.getLocalTarget = function (requestDetails) { - let destinationUrl, destinationHost, destinationPath, hostMappings, basePath, resourceMappings; let destinationSearchString = ''; @@ -102,9 +100,7 @@ requestAnalyzer.getLocalTarget = function (requestDetails) { */ requestAnalyzer._matchBasePath = function (hostMappings, channelPath) { - for (let basePath of Object.keys(hostMappings)) { - if (channelPath.startsWith(basePath)) { return basePath; } @@ -114,7 +110,6 @@ requestAnalyzer._matchBasePath = function (hostMappings, channelPath) { }; requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channelHost, channelPath, destinationSearchString) { - let resourcePath, versionNumber, resourcePattern, filename, shorthandResource; chrome.storage.sync.get(Setting.LOGGING, function (items) { @@ -134,14 +129,11 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel } for (let resourceMold of Object.keys(resourceMappings)) { - if (resourcePattern.startsWith(resourceMold)) { - let targetPath, hostShorthands, versionDelivered, versionRequested; targetPath = resourceMappings[resourceMold].path; targetPath = targetPath.replace(Resource.VERSION_PLACEHOLDER, versionNumber); - // Replace the requested version with the latest depending on major version versionDelivered = helpers.setLastVersion(targetPath, versionNumber).toString(); targetPath = targetPath.replace(versionNumber, versionDelivered); @@ -151,19 +143,17 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel hostShorthands = shorthands[channelHost]; if (hostShorthands && hostShorthands[targetPath]) { - let shorthand = hostShorthands[targetPath]; targetPath = shorthand.path; versionDelivered = shorthand.version; - } else if (versionNumber === null) { versionDelivered = targetPath.match(Resource.VERSION_EXPRESSION).toString(); } // Get bundle name let bundle = helpers.determineBundle(channelPath); - if(bundle !== '') { + if (bundle !== '') { filename = channelPath.split('/').pop(); targetPath = ( RegExp('.*\.css$').test(filename) ) ? targetPath + filename : targetPath + filename + 'm'; } @@ -188,13 +178,11 @@ requestAnalyzer._findLocalTarget = function (resourceMappings, basePath, channel }; requestAnalyzer._applyWhitelistedDomains = function () { - chrome.storage.sync.get(Setting.WHITELISTED_DOMAINS, function (items) { requestAnalyzer.whitelistedDomains = items.whitelistedDomains || {}; }); }; requestAnalyzer._applyManipulateDOMDomains = function () { - chrome.storage.sync.get(Setting.DOMAINS_MANIPULATE_DOM, function (items) { requestAnalyzer.domainsManipulateDOM = items.domainsManipulateDOM || {}; }); diff --git a/pages/updates/updates.html b/pages/updates/updates.html index fee58110..c208e520 100644 --- a/pages/updates/updates.html +++ b/pages/updates/updates.html @@ -24,6 +24,7 @@ New in LocalCDN: