1
0
mirror of https://codeberg.org/nobody/LocalCDN.git synced 2024-12-22 16:12:13 +01:00

Separated rule generator

This commit is contained in:
nobody 2020-08-29 09:56:11 +02:00
parent 96fd0399ad
commit 5d05066e76
No known key found for this signature in database
GPG Key ID: 8F6DE3D614FCFD7A
5 changed files with 68 additions and 80 deletions

View File

@ -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!');
}
);
};

View File

@ -10,6 +10,7 @@
<body>
<script src="../../core/constants.js"></script>
<script src="../../modules/internal/helpers.js"></script>
<script src="../../modules/internal/rule-generator.js"></script>
<script src="../../modules/internal/wrappers.js"></script>
<script defer src="../../core/resources.js"></script>
<script defer src="../../core/mappings.js"></script>

View File

@ -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',

View File

@ -8,6 +8,7 @@
<link rel="stylesheet" type="text/css" href="../base.css">
<link rel="stylesheet" type="text/css" href="../updates/updates.css">
<script src="../../core/constants.js"></script>
<script src="../../modules/internal/rule-generator.js"></script>
<script defer src="../../core/resources.js"></script>
<script defer src="../../core/mappings.js"></script>
<script src="updates.js"></script>

View File

@ -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);