From 6023bda460a3bee4b00a321c5adaf1573e26adfb Mon Sep 17 00:00:00 2001 From: MrYuto <75252297+MrYuto@users.noreply.github.com> Date: Tue, 1 Mar 2022 11:57:04 +0330 Subject: [PATCH 1/4] added new process to replace the matches --- css/redirector.css | 6 +++++- js/editredirect.js | 18 +++++++++++++++++- js/redirect.js | 36 +++++++++++++++++++++++++++++++++++- redirector.html | 15 +++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/css/redirector.css b/css/redirector.css index 63ee0a7..f0cfd2d 100644 --- a/css/redirector.css +++ b/css/redirector.css @@ -479,7 +479,7 @@ a.disabled:hover, button[disabled]:hover { } .hidden { - display:none; + display:none !important; } #advanced-toggle { @@ -521,6 +521,10 @@ a[ng-click] { padding-bottom:10px; } +.replace-process-input input[type='checkbox'] { + margin-top: 6px; +} + /* Footer with link */ footer { margin-top:30px; diff --git a/js/editredirect.js b/js/editredirect.js index ca4ea07..8aab23b 100644 --- a/js/editredirect.js +++ b/js/editredirect.js @@ -14,11 +14,13 @@ function editRedirect(index) { activeRedirect = new Redirect(REDIRECTS[index]); //Make a new one, which we can dump a bunch of stuff on... activeRedirect.existing = true; activeRedirect.index = index; + toggleReplaceProcessForm(activeRedirect.processMatches); showForm('#edit-redirect-form', activeRedirect); setTimeout(() => el('input[data-bind="description"]').focus(), 200); //Why not working...? } function cancelEdit() { + toggleReplaceProcessForm(activeRedirect.processMatches, true); activeRedirect = null; hideForm('#edit-redirect-form'); } @@ -51,12 +53,23 @@ function toggleAdvancedOptions(ev) { } } +function toggleReplaceProcessForm(currentProcess, force) { + force = force ?? currentProcess !== 'replace'; + + for (const input of document.querySelectorAll('.replace-process-input')) { + input.classList.toggle('hidden', force); + } +} function editFormChange() { //Now read values back from the form... for (let input of el('#edit-redirect-form').querySelectorAll('input[type="text"][data-bind]')) { let prop = input.getAttribute('data-bind'); activeRedirect[prop] = input.value; + + if (prop === 'replaceFrom') { + activeRedirect.replacePattern = input.value; + } } activeRedirect.appliesTo = []; for (let input of el('#apply-to').querySelectorAll('input:checked')) { @@ -65,6 +78,10 @@ function editFormChange() { activeRedirect.processMatches = el('#process-matches option:checked').value; activeRedirect.patternType = el('[name="patterntype"]:checked').value; + activeRedirect.usePatternForReplace = el('#use-pattern').checked; + activeRedirect.replaceAll = el('#replace-all').checked; + + toggleReplaceProcessForm(activeRedirect.processMatches); activeRedirect.updateExampleResult(); @@ -93,7 +110,6 @@ function cancelDelete() { hideForm('#delete-redirect-form'); } - function setupEditAndDeleteEventListeners() { el('#btn-save-redirect').addEventListener('click', saveRedirect); diff --git a/js/redirect.js b/js/redirect.js index eff4d2c..62e4b1c 100644 --- a/js/redirect.js +++ b/js/redirect.js @@ -37,6 +37,11 @@ Redirect.prototype = { error : null, includePattern : '', excludePattern : '', + replacePattern : '', + replaceFrom : '', + replacement : '', + replaceAll : false, + usePatternForReplace : false, patternDesc:'', redirectUrl : '', patternType : '', @@ -48,6 +53,7 @@ Redirect.prototype = { var incPattern = this._preparePattern(this.includePattern); var excPattern = this._preparePattern(this.excludePattern); + var replPattern = this._preparePattern(this.replacePattern); if (incPattern) { this._rxInclude = new RegExp(incPattern, 'gi'); @@ -55,6 +61,10 @@ Redirect.prototype = { if (excPattern) { this._rxExclude = new RegExp(excPattern, 'gi'); } + if (replPattern) { + const falgs = this.replaceAll ? 'gi' : 'i'; + this._rxReplace = new RegExp(replPattern, falgs); + } }, equals : function(redirect) { @@ -62,6 +72,11 @@ Redirect.prototype = { && this.exampleUrl == redirect.exampleUrl && this.includePattern == redirect.includePattern && this.excludePattern == redirect.excludePattern + && this.replacePattern == redirect.replacePattern + && this.replaceFrom == redirect.replaceFrom + && this.replacement == redirect.replacement + && this.replaceAll == redirect.replaceAll + && this.usePatternForReplace == redirect.usePatternForReplace && this.patternDesc == redirect.patternDesc && this.redirectUrl == redirect.redirectUrl && this.patternType == redirect.patternType @@ -77,6 +92,11 @@ Redirect.prototype = { error : this.error, includePattern : this.includePattern, excludePattern : this.excludePattern, + replacePattern : this.replacePattern, + replaceFrom : this.replaceFrom, + replacement : this.replacement, + replaceAll : this.replaceAll, + usePatternForReplace : this.usePatternForReplace, patternDesc : this.patternDesc, redirectUrl : this.redirectUrl, patternType : this.patternType, @@ -189,6 +209,7 @@ Redirect.prototype = { //Private functions below _rxInclude : null, _rxExclude : null, + _rxReplace : null, _preparePattern : function(pattern) { if (!pattern) { @@ -221,6 +242,11 @@ Redirect.prototype = { this.error = o.error || null; this.includePattern = o.includePattern || ''; this.excludePattern = o.excludePattern || ''; + this.replacePattern = o.replacePattern || ''; + this.replaceFrom = o.replaceFrom || ''; + this.replacement = o.replacement || ''; + this.replaceAll = o.replaceAll || false; + this.usePatternForReplace = o.usePatternForReplace || false; this.redirectUrl = o.redirectUrl || ''; this.patternType = o.patternType || Redirect.WILDCARD; @@ -274,7 +300,15 @@ Redirect.prototype = { var resultUrl = this.redirectUrl; for (var i = matches.length - 1; i > 0; i--) { var repl = matches[i] || ''; - if (this.processMatches == 'urlDecode') { + if (this.processMatches == 'replace') { + const pattern = this.usePatternForReplace ? this._rxReplace ?? '' : this.replaceFrom; + + if (this.replaceAll) { + repl = repl.replaceAll(pattern, this.replacement); + } else { + repl = repl.replace(pattern, this.replacement); + } + } else if (this.processMatches == 'urlDecode') { repl = unescape(repl); } else if (this.processMatches == 'doubleUrlDecode') { repl = unescape(unescape(repl)); diff --git a/redirector.html b/redirector.html index e0f6f4c..00b4100 100644 --- a/redirector.html +++ b/redirector.html @@ -93,6 +93,7 @@