From a8152179c4c03e8fa54adf073a05c11bfc4f667b Mon Sep 17 00:00:00 2001
From: Ian C <ianc@noddybox.co.uk>
Date: Tue, 31 Jul 2018 07:42:02 +0000
Subject: Added try/catch to calls to JSON.parse and removed old scroll
 position save/restore code.

---
 www/scripts/main.js | 112 +++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 76 insertions(+), 36 deletions(-)

(limited to 'www/scripts')

diff --git a/www/scripts/main.js b/www/scripts/main.js
index fe069dd..53027aa 100644
--- a/www/scripts/main.js
+++ b/www/scripts/main.js
@@ -3,27 +3,11 @@ var globalDb = [];
 var globalGroups = [];
 var globalCurrentGroup = "";
 
-var globalScrollY = 0;
-
 function Error(message)
 {
 	alert(message);
 }
 
-function SaveScroll()
-{
-	globalScrollY = window.pageYOffset;
-}
-
-function RestoreScroll()
-{
-	if (globalScrollY > 0)
-	{
-		window.scroll(0, globalScrollY);
-		globalScrollY = 0;
-	}
-}
-
 function SetEnable(id, enabled)
 {
 	var elem = document.getElementById(id);
@@ -411,9 +395,27 @@ function LoadTable()
 
 function RefreshDataAsync()
 {
-	globalDb = JSON.parse(this.responseText);
-	LoadGroups(true);
-	LoadTable();
+	try
+	{
+		globalDb = JSON.parse(this.responseText);
+	}
+	catch(e)
+	{
+		Error(e);
+		globalDb = [];
+		return;
+	}
+
+	if (globalDb.error)
+	{
+		Error(globalDb.error);
+		globalDb = [];
+	}
+	else
+	{
+		LoadGroups(true);
+		LoadTable();
+	}
 }
 
 function RefreshData()
@@ -429,7 +431,16 @@ function LoadInitialData()
 
 function DoLoadAsync()
 {
-	globalDb = JSON.parse(this.responseText);
+	try
+	{
+		globalDb = JSON.parse(this.responseText);
+	}
+	catch(e)
+	{
+		Error(e);
+		globalDb = [];
+		return;
+	}
 
 	if (globalDb.error)
 	{
@@ -456,7 +467,15 @@ function DoLoad()
 
 function DoChangePassphraseAsync()
 {
-	var response = JSON.parse(this.responseText);
+	try
+	{
+		var response = JSON.parse(this.responseText);
+	}
+	catch(e)
+	{
+		Error(e);
+		return;
+	}
 
 	if (response.error)
 	{
@@ -511,7 +530,15 @@ function DoSelectGroup()
 
 function DoAddAsync()
 {
-	var response = JSON.parse(this.responseText);
+	try
+	{
+		var response = JSON.parse(this.responseText);
+	}
+	catch(e)
+	{
+		Error(e);
+		return;
+	}
 
 	if (response.error)
 	{
@@ -566,7 +593,15 @@ function DoAdd()
 
 function DoEditAsync()
 {
-	var response = JSON.parse(this.responseText);
+	try
+	{
+		var response = JSON.parse(this.responseText);
+	}
+	catch(e)
+	{
+		Error(e);
+		return;
+	}
 
 	if (response.error)
 	{
@@ -616,7 +651,15 @@ function DoEdit()
 
 function DoDeleteAsync()
 {
-	var response = JSON.parse(this.responseText);
+	try
+	{
+		var response = JSON.parse(this.responseText);
+	}
+	catch(e)
+	{
+		Error(e);
+		return;
+	}
 
 	if (response.error)
 	{
@@ -649,8 +692,6 @@ function DoCopyPassword()
 {
 	var id = "Password" + this.dataset.pmId;
 
-	SaveScroll();
-
 	if (!ShowPasswords())
 	{
 		SetInputType(id, "text");
@@ -662,15 +703,12 @@ function DoCopyPassword()
 	{
 		SetInputType(id, "password");
 	}
-
-	RestoreScroll();
 }
 
 function DoShow()
 {
 	if (!ShowPasswords())
 	{
-		SaveScroll();
 		SetInputType("Password" + this.dataset.pmId, "text");
 	}
 }
@@ -680,13 +718,20 @@ function DoHide()
 	if (!ShowPasswords())
 	{
 		SetInputType("Password" + this.dataset.pmId, "password");
-		setTimeout(RestoreScroll, 0);
 	}
 }
 
 function DoReorderAsync()
 {
-	var response = JSON.parse(this.responseText);
+	try
+	{
+		var response = JSON.parse(this.responseText);
+	}
+	catch(e)
+	{
+		Error(e);
+		return;
+	}
 
 	if (response.error)
 	{
@@ -695,7 +740,6 @@ function DoReorderAsync()
 	else
 	{
 		RefreshData();
-		RestoreScroll();
 	}
 }
 
@@ -704,8 +748,6 @@ function DoUp()
 	var id = this.dataset.pmId;
 	var prev = this.dataset.pmPrevId;
 
-	SaveScroll();
-
 	WebRequest("reorder.php", DoReorderAsync,
 	[
 		"from_id", id,
@@ -719,8 +761,6 @@ function DoDown()
 	var id = this.dataset.pmId;
 	var next = this.dataset.pmNextId;
 
-	SaveScroll();
-
 	WebRequest("reorder.php", DoReorderAsync,
 	[
 		"from_id", id,
-- 
cgit v1.2.3