diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:50:23 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 18:50:23 +0000 |
commit | 76dcdac3bd496a751312f5335cf3bbd78e9adcf6 (patch) | |
tree | 30de89024109eff10978e6b95ebd9b403a3e37b7 /src/scripts/pattern-tester.js | |
parent | Initial commit. (diff) | |
download | foxyproxy-firefox-extension-upstream.tar.xz foxyproxy-firefox-extension-upstream.zip |
Adding upstream version 7.5.1+dfsg.upstream/7.5.1+dfsgupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/scripts/pattern-tester.js')
-rw-r--r-- | src/scripts/pattern-tester.js | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/src/scripts/pattern-tester.js b/src/scripts/pattern-tester.js new file mode 100644 index 0000000..83e0a26 --- /dev/null +++ b/src/scripts/pattern-tester.js @@ -0,0 +1,77 @@ +'use strict'; + +// ----------------- Internationalization ------------------ +document.querySelectorAll('[data-i18n]').forEach(node => { + let [text, attr] = node.dataset.i18n.split('|'); + text = chrome.i18n.getMessage(text); + attr ? node[attr] = text : node.appendChild(document.createTextNode(text)); +}); +// ----------------- /Internationalization ----------------- + +// --- global +const url = document.querySelector('#url'); +const pattern = document.querySelector('#pattern'); +const type = document.querySelector('#type'); +const protocols = document.querySelector('#protocols'); +const result = document.querySelector('#result'); + + +document.querySelector('button[data-i18n="test"]').addEventListener('click', testPattern); + + +// ----- check for Edit +const pat = localStorage.getItem('pattern'); +if (pat) { + + pattern.value = pat; + type.value = localStorage.getItem('type'); + protocols.value = localStorage.getItem('protocols'); + + localStorage.removeItem('pattern'); + localStorage.removeItem('type'); + localStorage.removeItem('protocols'); +} + + +function testPattern() { + + // --- reset + url.classList.remove('invalid'); + pattern.classList.remove('invalid'); + result.classList.add('hide'); + result.classList.remove('alert'); + + // --- trim text values + [url, pattern].forEach(item => item.value = item.value.trim()); + + // --- URL check + let parsedURL; + try { parsedURL = new URL(url.value); } + catch (e) { + url.classList.add('invalid'); + showResult(e.message, true); + return; + } + + // --- protocol check + const protocolSet = { // converting to meaningful terms + '1': ['http:', 'https:'], + '2': ['http:'], + '4': ['https:'] + }; + + if (!protocolSet[protocols.value].includes(parsedURL.protocol)) { + showResult(chrome.i18n.getMessage('errorProtocol'), true); + return; + } + + + // --- pattern check + const regex = checkPattern(pattern, type); + if (!regex) { return; } + + // --- pattern on URL check (pattern is valid) + regex.test(parsedURL.host) ? showResult(chrome.i18n.getMessage('patternMatch')) : + showResult(chrome.i18n.getMessage('patternNotMatch'), true); + +} |