From 1e754fbfc2994eee9f4716ddb0ffd2006db30182 Mon Sep 17 00:00:00 2001 From: Nikita Karamov Date: Wed, 23 Sep 2020 18:39:32 +0200 Subject: [PATCH] Normalize URL coming from URI parameters --- src/script/index.js | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/src/script/index.js b/src/script/index.js index 60f2b61..e0a697c 100644 --- a/src/script/index.js +++ b/src/script/index.js @@ -1,3 +1,13 @@ +function normalizeUrl(url) { + if (url.indexOf("http://") == -1 && url.indexOf("https://") == -1) { + url = "https://" + url; + } + if (url.charAt(url.length - 1) !== '/'){ + url = url + "/"; + } + return url; +} + var prefillInstance = window.localStorage.getItem('mastodon_instance'); var paramPairs = window.location.search.substr(1).split('&'); @@ -15,7 +25,7 @@ delete i delete paramPair if (prefillInstance != null) { - document.getElementById('instance').value = prefillInstance; + document.getElementById('instance').value = normalizeUrl(prefillInstance); } document @@ -23,16 +33,9 @@ document .addEventListener('submit', function (e) { e.preventDefault(); var text = e.target.elements['text'].value; - var instance = e.target.elements['instance'].value; + var instance = normalizeUrl(e.target.elements['instance'].value); var remember = e.target.elements['remember'].checked; - if (instance.indexOf("http://") == -1 && instance.indexOf("https://") == -1) { - instance = "https://" + instance; - } - if (instance.charAt(instance.length - 1) !== '/'){ - instance = instance + "/"; - } - if (remember) { window.localStorage.setItem('mastodon_instance', instance); }