summaryrefslogtreecommitdiffstats
path: root/src/scripts/common.js
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/scripts/common.js63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/scripts/common.js b/src/scripts/common.js
new file mode 100644
index 0000000..bd516c8
--- /dev/null
+++ b/src/scripts/common.js
@@ -0,0 +1,63 @@
+'use strict';
+
+// ----------------- Pattern Check ------------------
+
+function checkPattern(pattern, type) {
+
+ const pat = pattern.value;
+
+ if (!pat) {
+ pattern.classList.add('invalid');
+ pattern.focus();
+ showResult(chrome.i18n.getMessage('errorEmpty'), true);
+ return;
+ }
+
+ const patternTypeSet = {
+ '1': 'wildcard',
+ '2': 'regex'
+ }
+
+ let regex;
+
+ switch (patternTypeSet[type.value]) {
+
+ // RegEx
+ case 'regex':
+ try { regex = new RegExp(pat); }
+ catch (e) {
+ pattern.classList.add('invalid');
+ showResult(e.message, true);
+ return false;
+ }
+ break;
+
+ // wildcard
+ default:
+ if (pat.includes('/')) {
+ pattern.classList.add('invalid');
+ showResult(chrome.i18n.getMessage('errorSlash'), true);
+ return false;
+ }
+
+ try { regex = new RegExp(Utils.wildcardToRegExp(pat)); }
+ catch (e) {
+ pattern.classList.add('invalid');
+ showResult(e.message, true);
+ return false;
+ }
+ }
+
+ // --- pattern is valid
+ return regex;
+}
+
+
+
+
+function showResult(text, fail) {
+
+ fail && result.classList.add('alert');
+ result.textContent = text;
+ result.classList.remove('hide');
+} \ No newline at end of file