This repository has been archived by the owner on Dec 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathoptions.js
97 lines (83 loc) · 3.79 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
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
//var broker = document.getElementById("broker");
//var port = document.getElementById("port");
//var subtopic = document.getElementById("subtopic");
//var reconnect_timeout = document.getElementById("reconnectTimeout");
//var clear_notifications = document.getElementById("clearNotifications");
//var notification_timeout = document.getElementById("notificationTimeout");
// Returns the id of an element
function $(id) {
return document.getElementById(id);
}
// grays out the timeout selector
// function disableNotifyTimeout(isDisabled) {
// //var options = document.getElementById("options");
// document.getElementById("notificationTimeout").style.color = isDisabled ? 'graytext' : 'black'; // The label color.
// document.getElementById("notificationTimeout").disabled = isDisabled; // control selector.
// }
// Reset options to sane default values.
function resetOptions()
{
localStorage.broker = "test.mosquitto.org"; // broker websocket address
localStorage.port = 80; // broker websocket port
localStorage.username = ""; // broker username, leave blank for none
localStorage.password = ""; // broker password, leave blank for none
localStorage.subtopic = "/mqtt2chrome/messages"; // topic to subscribe to
localStorage.reconnectTimeout = 10; // Clear notifications after this many seconds
localStorage.clearNotifications = true; // Enable automatic clearing of notifications
localStorage.notificationTimeout = 10; // Clear notifications after this many seconds
}
// Restores options from localStorage.
function restoreOptions()
{
console.log("Restoring options");
if (!localStorage.isInitialized)
{
resetOptions();
localStorage.isInitialized = true;
}
console.log("retrieving options from local storage");
document.getElementById("broker").value = localStorage.broker;
document.getElementById("port").value = localStorage.port;
document.getElementById("username").value = localStorage.username;
document.getElementById("password").value = localStorage.password;
document.getElementById("reconnectTimeout").value = localStorage.reconnectTimeout;
document.getElementById("subtopic").value = localStorage.subtopic;
document.getElementById("clearNotifications").checked = JSON.parse(localStorage.clearNotifications);
document.getElementById("notificationTimeout").value = localStorage.notificationTimeout;
// if (!document.getElementById("clearNotifications").value)
// {
// disableNotifyTimeout(true);
// }
// document.getElementById("clearNotifications").onchange = function() {
// disableNotifyTimeout(!document.getElementById("clearNotifications").checked);
// };
}
// Saves options to localStorage.
function saveOptions()
{
console.log("saving options");
localStorage.broker = document.getElementById("broker").value;
localStorage.port = document.getElementById("port").value;
localStorage.username = document.getElementById("username").value;
localStorage.password = document.getElementById("password").value;
localStorage.reconnectTimeout = document.getElementById("reconnectTimeout").value;
localStorage.subtopic = document.getElementById("subtopic").value;
localStorage.clearNotifications = document.getElementById("clearNotifications").checked;
localStorage.notificationTimeout = document.getElementById("notificationTimeout").value;
}
function saveAndClose()
{
saveOptions();
// Now close the tab
chrome.tabs.getSelected(null, function(tab) {
chrome.tabs.remove(tab.id);
});
//disconenct and re-connect with new settings
}
function init()
{
console.log("initializing options");
restoreOptions();
document.querySelector('#save').addEventListener('click', saveAndClose);
}
document.addEventListener('DOMContentLoaded', init);