summaryrefslogtreecommitdiffstats
path: root/dist/js/app.js
blob: 7bbd5b9c10789e558d6877a0660c767dcc7d14eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127




const app_version = "1.0.15.0-beta";
const previous_commit = "47c8dd3a9874dcaf505abcea61cd550a2e87c816";
const BEZIAPP_UPDATE_INTERVAL = 300; // update vsakih 300 sekund

if ("serviceWorker" in navigator) {
	navigator.serviceWorker.register("/sw.js")
		.then(() => { })
		.catch((err) => console.log("Service worker registration failed", err));
}

// Listen to messages from service workers.
if (navigator.serviceWorker) {
	navigator.serviceWorker.addEventListener('message', (event) => {
		if (event.data.msg === "install") {
			window.location.replace("/index.html");
		}
	});
}

/**
 * Displays a user-friendly text to the user and
 * detailed text to developer (console)
 * @param {string} usermsg User-friendly message
 * @param {string} devmsg Developer-friendly message
 */
async function UIAlert(usermsg, devmsg) {
	if (true) { // če bo kakšen dev switch?
		M.toast({ html: usermsg });
		console.log(`[BežiApp UIAlert] ${usermsg} ${devmsg}`);
	} else {
		M.toast({ html: `${usermsg} ${devmsg}` });
	}
}

/**
 * Handles GSEC error - notifies the user and prints a console message
 * @param {Object} err GSEC error object
 */
function gsecErrorHandlerUI(err) {
	console.log(`gsecErrorHanderUI: handling ${err}`);
	if (err == GSEC_ERR_NET || err == GSEC_ERR_NET_POSTBACK_GET ||
		err == GSEC_ERR_NET_POSTBACK_POST) {

		UIAlert(D("gsecErrNet"));
	} else if (err == GSEC_ERR_LOGIN) {
		UIAlert(D("gsecErrLogin"));
		localforage.setItem("logged_in", false).then(() => {
			window.location.replace("/index.html");
		});
	} else {
		UIAlert(D("gsecErrOther"));
	}
}

var update_app_function = async function () {
	try {
		$.get("/cache_name.txt?cache_kill=" + Date.now(), (data, status) => {
			var cache_name = data.split("///")[1].split("|||")[0];
			var data_to_send = {
				action: "checkversion",
				valid_cache_name: cache_name
			}
			try {
				navigator.serviceWorker.controller.postMessage(JSON.stringify(data_to_send));
			} catch (e) {
				console.log("update requested but sw is not available in app.js");
			}
		});
	} catch (e) {
		console.log("update requested but failed because of network error probably in update_app_function in app.js");
	}
}

var error_report_function = async function (msg, url, lineNo, columnNo, error) {
	// catching everything here so no looping error shit. that's the last thing we want
	try {
		localforage.getItem("errorReporting").then(async function (value) {
			let selectedE = value;
			if (value == null || value.length < 1) {
				selectedE = "on";
			}
			if (selectedE == "on") {
				var data = {};
				data.error = { "msg": msg, "url": url, "line": lineNo, "column": columnNo, "obj": error };
				data.client = { "ua": navigator.userAgent, "app_version": app_version, "previous_commit": previous_commit, "username": null };

				// Load required data
				data.client.username = await localforage.getItem("username");

				data.type = "error";
				$.post("https://beziapp-report.gimb.tk/", data);
			} else {
				console.log("error not reported as reporting is disabled!");
			}
		}).catch(() => { });
		return false;
	} catch (e) {
		console.log("error_erport_function: !!! ERROR! (caught) - probably some network error.");
	}
}

window.onerror = error_report_function;
window.onunhandledrejection = error_report_function;

async function try_app_update() {
	localforage.getItem("lastUpdate").then((data) => {
		if (Math.floor(Date.now() / 1000) > Number(data) + BEZIAPP_UPDATE_INTERVAL) {
			// trigger an update
			localforage.setItem("lastUpdate", Math.floor(Date.now() / 1000)).then(() => {
				update_app_function();
			});
		}
	});
}

document.addEventListener("DOMContentLoaded", () => {
	try_app_update();
	var update_interval = setInterval(() => {

		try_app_update();

	}, 1000 * BEZIAPP_UPDATE_INTERVAL);
})