-
Notifications
You must be signed in to change notification settings - Fork 0
/
webscript.js
142 lines (114 loc) · 4.8 KB
/
webscript.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Initialising necessary html elements
const longitudeposttemp = document.getElementById("longitudetemp");
const latitudeposttemp = document.getElementById("latitudetemp");
const messageInput = document.querySelector("#message-input");
const usernameInput = document.querySelector("#username-input");
const sendbtn = document.querySelector("#send-button");
// This function locates the user and inserts localisation data into an invisible form for the website to fetch its values
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
let lat = position.coords.latitude;
let long = position.coords.longitude;
latitudeposttemp.value = lat;
sessionStorage.setItem("latitude-autosave", lat);
longitudeposttemp.value = long;
sessionStorage.setItem("longitude-autosave", long);
});
}
// A helper function that uploads a corresponding value stored in sessionStorage
// Based on the provided element and the item's name
function uploadSessionItem(element, itemName) {
const storedValue = sessionStorage.getItem(itemName);
if (storedValue) {
element.value = storedValue;
}
}
// Keep username and message after refresh
function uploadAutosaves() {
var dict = {
messageInput: "message-autosave",
usernameInput: "username-autosave",
latitudeposttemp: "latitude-autosave",
longitudeposttemp: "longitude-autosave"
};
for (var element in dict) {
var itemName = dict [element];
uploadSessionItem(element, itemName)
}
/*
if (sessionStorage.getItem("message-autosave")) {
messageInput.value = sessionStorage.getItem("message-autosave");
}
if (sessionStorage.getItem("username-autosave")) {
usernameInput.value = sessionStorage.getItem("username-autosave");
}
if (sessionStorage.getItem("latitude-autosave")) {
latitudeposttemp.value = sessionStorage.getItem("latitude-autosave");
}
if (sessionStorage.getItem("longititude-autosave")) {
longitudeposttemp.value = sessionStorage.getItem("longititude-autosave");
}*/
}
// When the pages has loaded, we upload the autosaves
window.onload = () => {
uploadAutosaves();
};
// Each time the user inputs something in the message input,
// We save that the latest active element was the message input HTML element
// And we save its inner text
messageInput.addEventListener("keyup", () => {
sessionStorage.setItem("message-autosave", messageInput.value);
sessionStorage.setItem("focused-input", "messageInput");
});
// Each time the user inputs something in the username input,
// We save that the latest active element was the username input HTML element
// And we save its inner text
usernameInput.addEventListener("keyup", () => {
sessionStorage.setItem("username-autosave", usernameInput.value);
sessionStorage.setItem("focused-input", "usernameInput");
});
// Each time we click on the send button, the message button's inner text is erased
sendbtn.addEventListener("click", () => {
sessionStorage.setItem("message-autosave", "");
});
// Automatically submit an invisible form to reload page and keep POST values
var auto_refresh = setInterval(submitform, 10000);
// This function fetches the user's geolocation data (with their consent), and stores them in HTML elements
// to keep them safe in case the page refreshes
function submitform() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
let lat = position.coords.latitude;
let long = position.coords.longitude;
var latitudepost = document.getElementById("test2");
latitudepost.value = lat;
var longitudepost = document.getElementById("test3");
longitudepost.value = long;
//if (not user_is_typing){
document.getElementById("reload").submit();
//}
});
}
}
// Keep scroll position after reloading, and keep focusing on the input, despite the refresh
document.addEventListener("DOMContentLoaded", function(e) {
var scrollpos = localStorage.getItem("scroll-pos");
if (scrollpos) {
window.scrollTo(0, scrollpos);
}
var activeElementName = localStorage.getItem("active-element");
if (activeElementName) {
if (activeElementName === messageInput.id) {
messageInput.focus();
}
else if (activeElementName === usernameInput.id) {
usernameInput.focus();
}
}
});
// Update all items stored in local storage like scroll position and previous active element
// to ensure a better UX
window.onbeforeunload = function(e) {
localStorage.setItem("scroll-pos", window.scrollY);
localStorage.setItem("active-element", document.activeElement.id);
};