diff --git a/modules/internal/rule-generator.js b/modules/internal/rule-generator.js new file mode 100644 index 00000000..a4c86fed --- /dev/null +++ b/modules/internal/rule-generator.js @@ -0,0 +1,59 @@ +/** + * Rule Generator + * Belongs to LocalCDN (since 2020-02-26) + * + * @author nobody + * @since 2020-03-08 + * + * @license MPL 2.0 + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +'use strict'; + +/** + * Updates + */ + +var ruleGenerator = {}; + +/** + * Public Methods + */ +ruleGenerator.openRuleSet = function ({ target }) { + let urls = mappings; + let key = target.getAttribute('data-option'); + + let textArea = document.getElementById('generated-rules'); + let btnCopy = document.getElementById('button-copy-rule-set'); + + let content = ''; + + textArea.style.display = 'block'; + btnCopy.style.display = 'block'; + + for (var domain in urls) { + if (key === 'uMatrix') { + content += '* ' + domain + ' script allow' + '\n'; + content += '* ' + domain + ' css allow' + '\n'; + } else if (key === 'uBlock') { + content += '* ' + domain + ' * noop' + '\n'; + } + } + textArea.value = content.replace(/\n+$/, ''); +}; + +ruleGenerator.copyRuleSet = function () { + let textArea = document.getElementById('generated-rules'); + navigator.clipboard.writeText(textArea.value).then( + function () { + textArea.select(); + }, + function () { + alert('Rule set cannot be copied!'); + } + ); +}; diff --git a/pages/options/options.html b/pages/options/options.html index 0dcc99e8..1d0d5088 100644 --- a/pages/options/options.html +++ b/pages/options/options.html @@ -10,6 +10,7 @@ + diff --git a/pages/options/options.js b/pages/options/options.js index fdd05d8e..c496d183 100644 --- a/pages/options/options.js +++ b/pages/options/options.js @@ -134,13 +134,12 @@ options._registerOptionChangedEventListeners = function (elements) { elements.negateHtmlFilterList.addEventListener('change', options._onOptionChanged); elements.blockGoogleFonts.addEventListener('change', options._onOptionChanged); elements.selectedIcon.addEventListener('change', options._onOptionChanged); - let type = elements.ruleSets; - for (let i = 0; i < type.length; i++) { - type[i].addEventListener('change', options._openRuleSet); - } - elements.copyRuleSet.addEventListener('click', options._copyRuleSet); + elements.ruleSets[0].addEventListener('change', ruleGenerator.openRuleSet); + elements.ruleSets[1].addEventListener('change', ruleGenerator.openRuleSet); + elements.copyRuleSet.addEventListener('click', ruleGenerator.copyRuleSet); elements.internalStatistics.addEventListener('change', options._onOptionChanged); elements.allowedDomainsGoogleFonts.addEventListener('keyup', options._onOptionChanged); + elements.selectedIcon.addEventListener('change', options._onOptionChanged); }; options._registerMiscellaneousEventListeners = function () { @@ -299,12 +298,7 @@ options._onOptionChanged = function ({ target }) { } if (optionKey === Setting.SELECTED_ICON) { - wrappers.setIcon( - { - path: optionValue, - }, - 'Enabled' - ); + wrappers.setIcon({ path: optionValue }, 'Enabled'); } if (optionKey === Setting.INTERNAL_STATISTICS) { @@ -318,39 +312,6 @@ options._onOptionChanged = function ({ target }) { }); }; -options._openRuleSet = function ({ target }) { - let urls = mappings; - let optionKey = target.getAttribute('data-option'); - let textArea = document.getElementById('generated-rules'); - let btnCopy = document.getElementById('button-copy-rule-set'); - let content = ''; - - textArea.style.display = 'block'; - btnCopy.style.display = 'block'; - - for (var domain in urls) { - if (optionKey === 'uMatrix') { - content += '* ' + domain + ' script allow' + '\n'; - content += '* ' + domain + ' css allow' + '\n'; - } else if (optionKey === 'uBlock') { - content += '* ' + domain + ' * noop' + '\n'; - } - } - textArea.value = content.replace(/\n+$/, ''); -}; - -options._copyRuleSet = function () { - let textArea = document.getElementById('generated-rules'); - navigator.clipboard.writeText(textArea.value).then( - function () { - textArea.select(); - }, - function () { - alert('Rule set cannot be copied!'); - } - ); -}; - options._onClickHTMLFilterWarning = function () { chrome.tabs.create({ url: 'https://codeberg.org/nobody/LocalCDN/wiki/Blank-websites-or-weird-characters', diff --git a/pages/updates/updates.html b/pages/updates/updates.html index 91e67046..f031e858 100644 --- a/pages/updates/updates.html +++ b/pages/updates/updates.html @@ -8,6 +8,7 @@ + diff --git a/pages/updates/updates.js b/pages/updates/updates.js index 10e92ef0..8e2a0bdc 100644 --- a/pages/updates/updates.js +++ b/pages/updates/updates.js @@ -24,40 +24,6 @@ var updates = {}; /** * Private Methods */ -updates._openRuleSet = function ({ target }) { - let urls = mappings; - let updateKey = target.getAttribute('data-option'); - - let textArea = document.getElementById('generated-rules'); - let btnCopy = document.getElementById('button-copy-rule-set'); - - let content = ''; - - textArea.style.display = 'block'; - btnCopy.style.display = 'block'; - - for (var domain in urls) { - if (updateKey === 'uMatrix') { - content += '* ' + domain + ' script allow' + '\n'; - content += '* ' + domain + ' css allow' + '\n'; - } else if (updateKey === 'uBlock') { - content += '* ' + domain + ' * noop' + '\n'; - } - } - textArea.value = content.replace(/\n+$/, ''); -}; - -updates._copyRuleSet = function () { - let textArea = document.getElementById('generated-rules'); - navigator.clipboard.writeText(textArea.value).then( - function () { - textArea.select(); - }, - function () { - alert('Rule set cannot be copied!'); - } - ); -}; updates._openHistoryReleaseNotes = function () { let container = document.getElementById('history-release-notes'); @@ -82,10 +48,10 @@ updates._onDocumentLoaded = function () { }; for (let i = 0; i < updateElements.ruleSets.length; i++) { - updateElements.ruleSets[i].addEventListener('change', updates._openRuleSet); + updateElements.ruleSets[i].addEventListener('change', ruleGenerator.openRuleSet); } - updateElements.copyRuleSet.addEventListener('click', updates._copyRuleSet); + updateElements.copyRuleSet.addEventListener('click', ruleGenerator.copyRuleSet); document.getElementById('history').addEventListener('click', updates._openHistoryReleaseNotes);