From 06e4d62543da202d320f17c9d67f4fd961adb347 Mon Sep 17 00:00:00 2001 From: Joxit Date: Sun, 3 Sep 2017 00:39:28 +0200 Subject: [PATCH 1/2] [query-param] Add url query param for sharing --- src/scripts/script.js | 45 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/scripts/script.js b/src/scripts/script.js index 7bef7a1..1b0bcc5 100644 --- a/src/scripts/script.js +++ b/src/scripts/script.js @@ -15,8 +15,23 @@ * along with this program. If not, see . */ var registryUI = {} +registryUI.URL_QUERY_PARAM_REGEX = /[&?]url=/; +registryUI.URL_PARAM_REGEX = /^url=/; + registryUI.url = function() { - return registryUI.getRegistryServer(0); + if (!registryUI._url) { + var url = registryUI.getUrlQueryParam(); + if (url) { + try { + registryUI._url = registryUI.decodeURI(url); + return registryUI._url; + } catch (e) { + console.log(e); + } + } + registryUI._url = registryUI.getRegistryServer(0); + } + return registryUI._url; } registryUI.getRegistryServer = function(i) { try { @@ -37,6 +52,9 @@ registryUI.addServer = function(url) { return; } registryServer.push(url); + if (!registryUI._url) { + registryUI.updateHistory(url); + } localStorage.setItem('registryServer', JSON.stringify(registryServer)); } registryUI.changeServer = function(url) { @@ -48,6 +66,7 @@ registryUI.changeServer = function(url) { } registryServer.splice(index, 1); registryServer = [url].concat(registryServer); + registryUI.updateHistory(url); localStorage.setItem('registryServer', JSON.stringify(registryServer)); } registryUI.removeServer = function(url) { @@ -60,6 +79,30 @@ registryUI.removeServer = function(url) { registryServer.splice(index, 1); localStorage.setItem('registryServer', JSON.stringify(registryServer)); } + +registryUI.updateHistory = function(url) { + history.pushState(null, '', '?url=' + registryUI.encodeURI(url) + window.location.hash); + registryUI._url = url; +} + +registryUI.getUrlQueryParam = function () { + var search = window.location.search; + if (registryUI.URL_QUERY_PARAM_REGEX.test(search)) { + var param = search.split(/^\?|&/).find(function(param) { + return param && registryUI.URL_PARAM_REGEX.test(param); + }); + return param ? param.replace(registryUI.URL_PARAM_REGEX, '') : param; + } +}; + +registryUI.encodeURI = function(url) { + return url.indexOf('&') < 0 ? window.encodeURI(url) : btoa(url); +}; + +registryUI.decodeURI = function(url) { + return url.startsWith('http') ? window.decodeURI(url) : atob(url); +}; + registryUI.isImageRemoveActivated = true; registryUI.catalog = {}; registryUI.taglist = {}; From ca5fb09fc6ac3d747faec00f75d5d584890f05e9 Mon Sep 17 00:00:00 2001 From: Joxit Date: Wed, 6 Sep 2017 23:32:10 +0200 Subject: [PATCH 2/2] [query-param] Change decode/encodeURI to URIComponent --- src/scripts/script.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scripts/script.js b/src/scripts/script.js index 1b0bcc5..933bab4 100644 --- a/src/scripts/script.js +++ b/src/scripts/script.js @@ -96,11 +96,11 @@ registryUI.getUrlQueryParam = function () { }; registryUI.encodeURI = function(url) { - return url.indexOf('&') < 0 ? window.encodeURI(url) : btoa(url); + return url.indexOf('&') < 0 ? window.encodeURIComponent(url) : btoa(url); }; registryUI.decodeURI = function(url) { - return url.startsWith('http') ? window.decodeURI(url) : atob(url); + return url.startsWith('http') ? window.decodeURIComponent(url) : atob(url); }; registryUI.isImageRemoveActivated = true;