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

Create Redirect

+ +
From baa5121d3810650c365b7f139de8fde153b492aa Mon Sep 17 00:00:00 2001 From: MrYuto <75252297+MrYuto@users.noreply.github.com> Date: Tue, 1 Mar 2022 14:40:19 +0330 Subject: [PATCH 2/4] Deleted flags variable --- js/redirect.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/js/redirect.js b/js/redirect.js index 62e4b1c..80a65d4 100644 --- a/js/redirect.js +++ b/js/redirect.js @@ -62,8 +62,7 @@ Redirect.prototype = { this._rxExclude = new RegExp(excPattern, 'gi'); } if (replPattern) { - const falgs = this.replaceAll ? 'gi' : 'i'; - this._rxReplace = new RegExp(replPattern, falgs); + this._rxReplace = new RegExp(replPattern, this.replaceAll ? 'gi' : 'i'); } }, From edeef18b2c8addc509c63bbfe41e82783b54c6ba Mon Sep 17 00:00:00 2001 From: MrYuto <75252297+MrYuto@users.noreply.github.com> Date: Wed, 2 Mar 2022 09:19:10 +0330 Subject: [PATCH 3/4] Fixed some bugs --- css/redirector.css | 4 ++-- js/redirect.js | 11 ++++++++++- js/util.js | 4 +++- redirector.html | 2 +- 4 files changed, 16 insertions(+), 5 deletions(-) diff --git a/css/redirector.css b/css/redirector.css index f0cfd2d..c403bcd 100644 --- a/css/redirector.css +++ b/css/redirector.css @@ -478,8 +478,8 @@ a.disabled:hover, button[disabled]:hover { margin-top:8px; } -.hidden { - display:none !important; +.hidden, .replace-process-input.hidden { + display:none; } #advanced-toggle { diff --git a/js/redirect.js b/js/redirect.js index 80a65d4..c1d7921 100644 --- a/js/redirect.js +++ b/js/redirect.js @@ -61,7 +61,7 @@ Redirect.prototype = { if (excPattern) { this._rxExclude = new RegExp(excPattern, 'gi'); } - if (replPattern) { + if (this.usePatternForReplace && replPattern) { this._rxReplace = new RegExp(replPattern, this.replaceAll ? 'gi' : 'i'); } }, @@ -165,6 +165,15 @@ Redirect.prototype = { } } + if (this.patternType == Redirect.REGEX && this.usePatternForReplace && this.replacePattern) { + try { + new RegExp(this.replacePattern, this.replaceAll ? 'gi' : 'i'); + } catch(e) { + this.error = 'Invalid regular expression in Replace pattern.'; + return; + } + } + if (!this.appliesTo || this.appliesTo.length == 0) { this.error = 'At least one request type must be chosen.'; return; diff --git a/js/util.js b/js/util.js index 4249acc..85815c8 100644 --- a/js/util.js +++ b/js/util.js @@ -20,9 +20,11 @@ function dataBind(el, dataObject) { } else if (tag.tagName.toLowerCase() === 'select') { for (let opt of tag.querySelectorAll('option')) { if (opt.getAttribute('value') === dataObject[prop]) { - opt.setAttribute('selected', 'selected'); + opt.setAttribute('selected', ''); + opt.selected = true; } else { opt.removeAttribute('selected'); + opt.selected = false; } } } else if (Array.isArray(dataObject[prop])) { diff --git a/redirector.html b/redirector.html index 00b4100..81ca13a 100644 --- a/redirector.html +++ b/redirector.html @@ -113,7 +113,7 @@

Create Redirect

- +
From da821b9b38426a0b9be38f815b97b430ff7dc452 Mon Sep 17 00:00:00 2001 From: MrYuto <75252297+MrYuto@users.noreply.github.com> Date: Wed, 2 Mar 2022 09:25:51 +0330 Subject: [PATCH 4/4] Added an explanation about replace process --- help.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/help.html b/help.html index ec5c5b9..bde5963 100644 --- a/help.html +++ b/help.html @@ -23,6 +23,7 @@

Table of contents

  • Process Matches
    1. No Processing
    2. +
    3. Replace matches
    4. URL decode matches
    5. Double URL decode matches
    6. URL encode matches
    7. @@ -95,6 +96,10 @@

      Basic usage

      different ways to process the Regular expression matches before using them. The decoding options available are:
      • No Processing: This is the default. Just use the matches from the original url exactly as they are.
      • +
      • Replace matches: Sometimes you may want to modify some parts of your matches. + This helps you replace, delete or add something. You can use this as simple as possible like finding a character and changing it or by using search patterns to find something and javascript + replacement patterns to replace it. +
      • URL Decode matches: A common usage of Redirector is to catch urls like http://foo.com/redirect.php?url=http%3A%2F%2Fbar%2Ecom%2Fpath and try to catch the url parameter and redirect to it. A pattern like http://foo.com/redirect.php?url=* might be used for that purpose. However, if the url parameter is escaped (also known