From 2df763cd19c1f4e76a567333456517c15d9630b6 Mon Sep 17 00:00:00 2001 From: relikd Date: Tue, 27 Feb 2024 20:24:29 +0100 Subject: [PATCH] ref: save & load config --- script.js | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/script.js b/script.js index b576516b..5d6e54ab 100644 --- a/script.js +++ b/script.js @@ -3,6 +3,7 @@ var DB_result = []; var baseUrls = {}; var PER_PAGE = 30; var plistGeneratorUrl = ''; // will append ?d= +NodeList.prototype.forEach = Array.prototype.forEach; // fix for < iOS 9.3 /* * Init @@ -26,8 +27,9 @@ function loadFile(url, onErrFn, fn) { } function loadDB() { + var config = null; try { - loadConfig(); + config = loadConfig(true); } catch (error) { alert(error); } @@ -43,36 +45,35 @@ function loadDB() { }); } -function loadConfig() { +function loadConfig(chkServer) { const params = location.hash.substring(1).split('&'); + const data = {}; params.forEach(function (param) { const pair = param.split('=', 2); - const key = pair[0]; - const value = pair[1]; - const input = document.getElementById(key); - if (input) { - if (input.type === 'checkbox') { - input.checked = value; - } else { - input.value = value; - } - if (key == 'plistServer') { - setPlistGen(); - } + data[pair[0]] = decodeURIComponent(pair[1]); + }); + document.querySelectorAll('input,select').forEach(function (input) { + if (input.type === 'checkbox') { + input.checked = data[input.id] || null; + } else { + input.value = data[input.id] || null; } }); + if (chkServer && data['plistServer']) { + setPlistGen(); + } + return data; } function saveConfig() { const data = []; - NodeList.prototype.forEach = Array.prototype.forEach; // fix for < iOS 9.3 document.querySelectorAll('input,select').forEach(function (e) { const value = e.type === 'checkbox' ? e.checked : e.value; if (value) { - data.push(e.id + '=' + value); + data.push(e.id + '=' + encodeURIComponent(value)); } }); - this.location.hash = '#' + data.join('&'); + location.hash = '#' + data.join('&'); } /*