-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.js
68 lines (51 loc) · 2.45 KB
/
options.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
// cpt = close pinned tabs, false to default
// isURL() code taken from https://stackoverflow.com/questions/1701898/how-to-detect-whether-a-string-is-in-url-format-using-javascript
function isURL(str) {
let regexp = /(http|https|file||chrome):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
return regexp.test(str);
}
function saveSettings() {
let cpt = document.getElementById("cpt").checked;
if (cpt === true) localStorage.setItem('cpt', 'true');
else localStorage.setItem('cpt', 'false');
let action = document.querySelector("input[name=action]:checked").value;
localStorage.setItem("action", action);
let customurl = document.querySelector("input[name=customurl]").value || "";
localStorage.setItem("customurl", customurl);
let status = document.getElementById('status');
status.textContent = 'Settings saved!';
setTimeout(function() {
status.textContent = '';
}, 750);
}
let restoreOptions = function() {
let cpt = localStorage.getItem("cpt") || "false";
if (cpt === "true") document.getElementById("cpt").checked = true;
let action = localStorage.getItem("action") || "newtab";
document.querySelector(`input[type="radio"][value="${action}"]`).checked = true;
let customurl = localStorage.getItem("customurl") || "";
document.querySelector("input[name=customurl]").value = customurl;
}
const saveButton = document.querySelector('#save');
saveButton.addEventListener('click', (event) => {
let action = document.querySelector("input[name=action]:checked").value;
if(action === "custom") {
let customurl = document.querySelector("input[name=customurl]").value;
if(isURL(customurl)) saveSettings();
else {
let status = document.getElementById("status");
let warning = document.getElementById("warning");
let saveButton = document.getElementById("save");
status.textContent = "Settings not saved";
warning.textContent = "Enter a valid URL starting with https://, http://, file:// or chrome://"
saveButton.style.backgroundColor = "red";
setTimeout(function() {
status.textContent = " ";
warning.textContent = ""
saveButton.style.backgroundColor = "green";
}, 1500);
}
}
else saveSettings();
});
document.addEventListener("DOMContentLoaded", restoreOptions);