From b58698b257fb6552e053cd245d63a140d3f7a478 Mon Sep 17 00:00:00 2001 From: Joel Martin Date: Sun, 27 Apr 2014 16:55:31 -0500 Subject: JS: add localStorage history load/save. Slurp using sync XHR. --- js/core.js | 13 ++++++++++++- js/jq_readline.js | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) (limited to 'js') diff --git a/js/core.js b/js/core.js index b5a52e0..3ab2117 100644 --- a/js/core.js +++ b/js/core.js @@ -39,7 +39,18 @@ function println() { } function slurp(f) { - return require('fs').readFileSync(f, 'utf-8'); + if (typeof require !== 'undefined') { + return require('fs').readFileSync(f, 'utf-8'); + } else { + var req = new XMLHttpRequest(); + req.open("GET", f, false); + req.send(); + if (req.status == 200) { + return req.responseText; + } else { + throw new Error("Failed to slurp file: " + f); + } + } } diff --git a/js/jq_readline.js b/js/jq_readline.js index e9f624f..51bb571 100644 --- a/js/jq_readline.js +++ b/js/jq_readline.js @@ -1,3 +1,21 @@ +var max_history_length = 1000; + +function jq_load_history(jq) { + if (localStorage['mal_history']) { + var lines = JSON.parse(localStorage['mal_history']); + if (lines.length > max_history_length) { + lines = lines.slice(lines.length-max_history_length); + } + jq.SetHistory(lines); + } +} + +function jq_save_history(jq) { + var lines = jq.GetHistory(); + localStorage['mal_history'] = JSON.stringify(lines); +} + + var readline = { 'readline': function(prompt_str) { return prompt(prompt_str); -- cgit v1.2.3