From 22e6dd50188000ec50df2025bc4c9d384b8bb74d Mon Sep 17 00:00:00 2001
From: echicken <echicken@bbs.electronicchicken.com>
Date: Fri, 26 Feb 2021 14:21:30 -0500
Subject: [PATCH] v4_fetch_jsonl for handling JSON-lines responses.

---
 webv4/root/js/common.js | 33 ++++++++++++++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/webv4/root/js/common.js b/webv4/root/js/common.js
index a48b8843ea..93d6cabb2e 100644
--- a/webv4/root/js/common.js
+++ b/webv4/root/js/common.js
@@ -15,7 +15,7 @@ async function v4_fetch(url, method, body) {
 		const data = await response.json();
 		return data;
 	} catch (err) {
-		console.log('Error on fetch', url, init);
+		console.error('Error on fetch', url, init);
 	}
 }
 
@@ -36,6 +36,19 @@ function v4_post(url, data) {
 	return v4_fetch(url, 'POST', fd);
 }
 
+async function v4_fetch_jsonl(url) {
+	try {
+		const response = await fetch(url);
+		const text = await response.text();
+		return text.split(/\r\n/).reduce((a, c) => {
+			if (c !== '') a.push(JSON.parse(c));
+			return a;
+		}, []);
+	} catch (err) {
+		console.error('Error on fetch_jsonl', url, err);
+	}
+}
+
 async function login(evt) {
 	if ($('#input-username').val() == '' || $('#input-password').val() == '') {
 		return;
@@ -63,6 +76,24 @@ function scrollUp() {
 	window.scrollBy(0, -document.getElementById('navbar').offsetHeight);
 }
 
+// Add a parameter to the query string
+function insertParam(key, value) {
+    key = encodeURIComponent(key);
+    value = encodeURIComponent(value);
+    var kvp = window.location.search.substr(1).split('&');
+    var i = kvp.length,	x;
+    while (i--) {
+		x = kvp[i].split('=');
+		if (x[0] !== key) continue;
+		x[1] = value;
+		kvp[i] = x.join('=');
+		break;
+    }
+    if (i<0) kvp[kvp.length] = [key,value].join('=');
+    window.location.search = kvp.join('&');
+}
+
+
 function sendTelegram(alias) {
     function send_tg(evt) {
         if (typeof evt !== 'undefined') evt.preventDefault();
-- 
GitLab