From 8efd97ba7526a1491f57f235492d010355e285e7 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 25 Sep 2021 21:13:32 +0700 Subject: [PATCH] allow save pre-config list of server --- client/scripts/WebConsole.js | 25 +++++++++++++++++++++++ client/scripts/WebConsoleJqueryHandler.js | 3 ++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/client/scripts/WebConsole.js b/client/scripts/WebConsole.js index 389f8c3..8b46d88 100644 --- a/client/scripts/WebConsole.js +++ b/client/scripts/WebConsole.js @@ -14,6 +14,31 @@ var autoPasswordCompleted = false; //When true, saved password was used. If a 40 var statusCommandsInterval = -1; var commandHistoryIndex = -1; //Saves current command history index. -1 when not browsing history. +/** + * Load list of servers in file servers.json + * and auto update in next request when file is changed + */ +function readServerList() { + let hash = persistenceManager.getSetting('server:hash') + + /** + * Hash code function used for compare version of file servers.json + * https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript + */ + const hashCode = s => s.split('').reduce((a,b)=>{a=((a<<5)-a)+b.charCodeAt(0);return a&a},0); + + fetch('servers.json') + .then(res => res.text()) + .then(json => { + if (hash !== hashCode(json)) { + persistenceManager.setSetting('server:hash', hashCode(json)) + JSON.parse(json).forEach(server => persistenceManager.saveServer(server)) + } + }) + .then(updateServerList) + .catch(() => console.info('Ignore load new list in file servers.json.')); +} + /** * Prepare and show server to user */ diff --git a/client/scripts/WebConsoleJqueryHandler.js b/client/scripts/WebConsoleJqueryHandler.js index 37240af..052a61b 100644 --- a/client/scripts/WebConsoleJqueryHandler.js +++ b/client/scripts/WebConsoleJqueryHandler.js @@ -11,6 +11,7 @@ $(document).ready(function() { $("#serverContainer").hide(); persistenceManager.initializeSettings(); setLanguage(persistenceManager.getLanguage()); + readServerList(); updateServerList(); //Check SSL host @@ -223,4 +224,4 @@ $("#showDateSettingsSwitch").click(function() { $("#readLogFileSwitch").click(function() { //Update modal switches and boxes with saved settings persistenceManager.setSetting("retrieveLogFile", $("#readLogFileSwitch").is(":checked")); -}); \ No newline at end of file +});