From f98a4718c179a97f8b66630ae9a037ee77ce7a0c Mon Sep 17 00:00:00 2001 From: Jiehong Ma Date: Tue, 21 Nov 2023 17:05:00 +0100 Subject: [PATCH] feature: save options in the url --- public/index.html | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/public/index.html b/public/index.html index 6f89156..f9479af 100755 --- a/public/index.html +++ b/public/index.html @@ -32,41 +32,36 @@ jq in your browser, without sending anything to any backend."> async function setup() { // Populate the form from the url parameters let params = (new URL(document.location)).searchParams; - let query = params.get('query'); + let query = params.get('query'); if (query) { document.getElementById("filter").value = query; } + ['compact-output', 'sort-keys', 'raw-input', 'raw-output', 'slurp'].forEach(option => { + let p = params.get(option); + if (p === 'true') { + document.getElementById(option).checked = true; + } + }); } async function jq() { let data = document.getElementById("input-json").value; let query = document.getElementById("filter").value; - let compactOutput = document.getElementById("compact-output").checked; - let sortKeys = document.getElementById("sort-keys").checked; - let rawInput = document.getElementById("raw-input").checked; - let rawOutput = document.getElementById("raw-output").checked; - let slurp = document.getElementById("slurp").checked; + let params = (new URL(document.location)).searchParams; let options = ["--monochrome-output"]; - if (compactOutput) { - options.push("--compact-output"); - } - if (sortKeys) { - options.push("--sort-keys"); - } - if (rawInput) { - options.push("--raw-input"); - } - if (rawOutput) { - options.push("--raw-output"); - } - if (slurp) { - options.push("--slurp"); - } + ['compact-output', 'sort-keys', 'raw-input', 'raw-output', 'slurp'].forEach(option => { + let part = document.getElementById(option).checked; + if (part) { + params.set(option, true); + options.push('--' + option); + } else { + params.delete(option); + } + }); // Update url query params with current query - let params = (new URL(document.location)).searchParams; params.set('query', query); const url = new URL(document.location); url.search = params.toString();