diff --git a/core/messenger.js b/core/messenger.js index e959f9a8..f8df2283 100644 --- a/core/messenger.js +++ b/core/messenger.js @@ -32,89 +32,79 @@ var messenger = {}; messenger._handleMessageReceived = function (message, sender, sendResponse) { - let topic, value; + let topic, value, popup; topic = message.topic; value = message.value; + popup = {}; - if (topic === 'tab:fetch-injections') { + switch (topic) { - sendResponse({'value': stateManager.tabs[value].injections}); - return MessageResponse.SYNCHRONOUS; - } + case 'tab:fetch-injections': + sendResponse({'value': stateManager.tabs[value].injections}); + return MessageResponse.SYNCHRONOUS; - if (topic === 'domain:fetch-is-allowlisted') { - let allowlistRecord = helpers.checkAllowlisted(value); - sendResponse({'value': Boolean(allowlistRecord)}); + case 'tab:inject': + chrome.tabs.executeScript(value, { + 'code': `window.addEventListener('load', () => { + document.getElementById('domain').value = '${message.url}'; + });`, + 'runAt': 'document_start' + }); + break; - return MessageResponse.SYNCHRONOUS; - } + case 'domain:fetch-is-allowlisted': + sendResponse({'value': Boolean(helpers.checkAllowlisted(value))}); + return MessageResponse.SYNCHRONOUS; - if (topic === 'allowlist:add-domain') { + case 'domain:fetch-is-manipulateDOM': + sendResponse({'value': Boolean(requestAnalyzer.domainsManipulateDOM[value])}); + return MessageResponse.SYNCHRONOUS; - stateManager.addDomainToAllowlist(value).then(function () { - sendResponse({'value': true}); - }); + case 'allowlist:add-domain': + stateManager.addDomainToAllowlist(value).then(function () { + sendResponse({'value': true}); + }); + return MessageResponse.ASYNCHRONOUS; - return MessageResponse.ASYNCHRONOUS; - } + case 'allowlist:remove-domain': + stateManager.removeDomainFromAllowlist(value).then(function () { + sendResponse({'value': true}); + }); + return MessageResponse.ASYNCHRONOUS; - if (topic === 'allowlist:remove-domain') { + case 'manipulateDOM:add-domain': + stateManager.addDomainToManipulateDOMlist(value).then(function () { + sendResponse({'value': true}); + }); + return MessageResponse.ASYNCHRONOUS; - stateManager.removeDomainFromAllowlist(value).then(function () { - sendResponse({'value': true}); - }); + case 'manipulateDOM:remove-domain': + stateManager.removeDomainFromManipulateDOMlist(value).then(function () { + sendResponse({'value': true}); + }); + return MessageResponse.ASYNCHRONOUS; - return MessageResponse.ASYNCHRONOUS; - } + case 'statistic:delete': + storageManager.statistics = {}; + break; - if (topic === 'domain:fetch-is-manipulateDOM') { + case 'logs:get': + sendResponse({'logs': log.data}); + return MessageResponse.SYNCHRONOUS; - let manipulateDOMRecord = requestAnalyzer.domainsManipulateDOM[value]; - sendResponse({'value': Boolean(manipulateDOMRecord)}); + case 'logs:delete': + log.data = []; + break; - return MessageResponse.SYNCHRONOUS; - } - - if (topic === 'manipulateDOM:add-domain') { - - stateManager.addDomainToManipulateDOMlist(value).then(function () { - sendResponse({'value': true}); - }); - - return MessageResponse.ASYNCHRONOUS; - } - - if (topic === 'manipulateDOM:remove-domain') { - - stateManager.removeDomainFromManipulateDOMlist(value).then(function () { - sendResponse({'value': true}); - }); - - return MessageResponse.ASYNCHRONOUS; - } - - if (topic === 'deleteStatistic') { - storageManager.statistics = {}; - } - - if (topic === 'injection') { - - chrome.tabs.executeScript(value, { - 'code': `window.addEventListener('load', () => { - document.getElementById('domain').value = '${message.url}'; - });`, - 'runAt': 'document_start' - }); - } - - if (topic === 'logs:get') { - sendResponse({'logs': log.data}); - return MessageResponse.SYNCHRONOUS; - } - - if (topic === 'logs:delete') { - log.data = []; + case 'popup:get-data': + popup.amountInjected = storageManager.amountInjected; + popup.internalStatistics = stateManager.internalStatistics; + popup.negateHtmlFilterList = stateManager.getInvertOption; + popup.loggingStatus = stateManager.logging; + popup.hideDonationButton = stateManager.hideDonationButton; + sendResponse({'data': popup}); + return MessageResponse.ASYNCHRONOUS; } }; diff --git a/core/state-manager.js b/core/state-manager.js index f081bd16..50480a97 100644 --- a/core/state-manager.js +++ b/core/state-manager.js @@ -206,6 +206,8 @@ stateManager._handleStorageChanged = function (changes) { stateManager.hideDonationButton = changes.hideDonationButton.newValue; } else if (Setting.CHANGE_BADGE_COLOR_MISSING_RESOURCES in changes) { stateManager.changeBadgeColorMissingResources = changes.changeBadgeColorMissingResources.newValue; + } else if (Setting.LOGGING in changes) { + stateManager.logging = changes.enableLogging.newValue; } }; @@ -256,6 +258,7 @@ stateManager.selectedIcon = 'Default'; stateManager.internalStatistics = false; stateManager.hideDonationButton = false; stateManager.changeBadgeColorMissingResources = false; +stateManager.logging = false; for (let mapping in mappings.cdn) { let supportedHost = Address.ANY_PROTOCOL + mapping + Address.ANY_PATH; @@ -268,8 +271,13 @@ chrome.tabs.query({}, function (tabs) { storageManager.type.get([ Setting.SHOW_ICON_BADGE, + Setting.NEGATE_HTML_FILTER_LIST, Setting.SELECTED_ICON, - Setting.CHANGE_BADGE_COLOR_MISSING_RESOURCES + Setting.INTERNAL_STATISTICS, + Setting.HIDE_DONATION_BUTTON, + Setting.CHANGE_BADGE_COLOR_MISSING_RESOURCES, + Setting.LOGGING, + Setting.AMOUNT_INJECTED ], function (items) { if (items.showIconBadge === undefined) { items.showIconBadge = true; @@ -278,8 +286,13 @@ storageManager.type.get([ stateManager.selectedIcon = 'Default'; } stateManager.showIconBadge = items.showIconBadge; + stateManager.getInvertOption = items.negateHtmlFilterList; stateManager.selectedIcon = items.selectedIcon; + stateManager.internalStatistics = items.internalStatistics; + stateManager.hideDonationButton = items.hideDonationButton; stateManager.changeBadgeColorMissingResources = items.changeBadgeColorMissingResources; + stateManager.logging = items.enableLogging; + stateManager.amountInjected = items.amountInjected; }); chrome.storage.local.get([Setting.INTERNAL_STATISTICS], function (items) { diff --git a/modules/internal/helpers.js b/modules/internal/helpers.js index 5a2d3748..bc574bb6 100644 --- a/modules/internal/helpers.js +++ b/modules/internal/helpers.js @@ -187,6 +187,28 @@ helpers.determineScriptDirection = function (language) { return scriptDirection; }; +helpers.determineActiveTab = function () { + return new Promise((resolve) => { + let opt = { + 'active': true, + 'currentWindow': true + }; + + chrome.tabs.query(opt, function (tabs) { + if (tabs[0]) { + resolve(tabs[0]); + } else { + opt = {'active': true}; + + chrome.tabs.query(opt, function (tabs) { + resolve(tabs[0]); + }); + } + }); + }); +}; + + helpers.formatNumber = function (number) { if (typeof number === 'number') { return number.toLocaleString(); diff --git a/pages/options/options.html b/pages/options/options.html index bc536152..7361b132 100644 --- a/pages/options/options.html +++ b/pages/options/options.html @@ -176,7 +176,7 @@ Do not apply HTML filter to these domains: Apply HTML filter to these domains: - + Enter the domains to be handled or ignored by the HTML filter. One domain per line. diff --git a/pages/popup/popup.html b/pages/popup/popup.html index fa01ba74..8770de7f 100644 --- a/pages/popup/popup.html +++ b/pages/popup/popup.html @@ -20,7 +20,7 @@ Donate - + @@ -44,7 +44,7 @@ - Filter HTML source code + Filter HTML source code @@ -55,18 +55,18 @@ - +
Donate