-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.js
139 lines (131 loc) · 4.99 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
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
// Name: Jeremy Leon
// Course: CSC 415
// Semester: Fall 2015
// Instructor: Dr. Pulimood
// Project name: Timed Tab Limiter
// Description: Puts timers on Tabs created over specified Tab Goal up to specified Tab Limit
// Filename: options.js
// Description: Controls options page and ensures valid input of settings
// Last modified on: 11/6/15
//-----------------------------------------------------------------------------------------
//
// Function: loadOptions ()
//
// Pre-condition: Options page is loaded
// Post-condition: input text boxes are filled with saved values
//-----------------------------------------------------------------------------------------
function loadOptions() {
chrome.storage.sync.get("settings", function(items) {
loadSetting(items.settings.timerLength, 'time', 300);
loadSetting(items.settings.tabGoal, 'tabGoal', 10);
loadSetting(items.settings.tabLimit, 'tabLimit', 15);
});
}
//-----------------------------------------------------------------------------------------
//
// Function: loadSetting ()
//
// Parameters:
// setting: setting being loaded
// element: name of the HTML input text being updated
// defualtSetting: if setting fails to load, sets it at defualt value
//
// Pre-condition: Options page is loaded
// Post-condition: input text box matched saved value
//-----------------------------------------------------------------------------------------
function loadSetting(setting, element, defaultSetting) {
if (setting !== undefined){
document.getElementById(element).value = setting;
}
else {
document.getElementById(element).value = defaultSetting;
}
}
//-----------------------------------------------------------------------------------------
//
// Function: saveOptions ()
//
// Pre-condition: User presses save button
// Post-condition: If all settings are whole numbers greater than zeros, and tabGoal is
// less than tabLimit, saves the entered options and sends message to
// background.js to update settings object. Otherwise, tells user why
// entered values are invalid
//-----------------------------------------------------------------------------------------
function saveOptions() {
var time = +document.getElementById("time").value;
var tabGoal = +document.getElementById("tabGoal").value;
var tabLimit = +document.getElementById("tabLimit").value;
if (tabLimit <= tabGoal){
updateStatus('Tab Goal must be less than Tab Limit', 2000)
}
else if (validInput(time) && validInput(tabGoal) && validInput(tabLimit)){
chrome.storage.sync.set({
"settings": {
"timerLength": time,
"tabGoal": tabGoal,
"tabLimit" : tabLimit
}
}, function() {
chrome.runtime.sendMessage({
settings: "updated"
});
updateStatus('Options saved.', 750)
});
}
else if (isInteger(time) && isInteger(tabGoal) && isInteger(tabLimit)) {
updateStatus('Parameters must be greater than zero.', 2000)
}
else {
updateStatus('Not a whole number.', 2000)
}
}
//-----------------------------------------------------------------------------------------
//
// Function: updateStatus ()
//
// Parameters:
// msg: messagre to be displayed
// length: amount of time in milliseconds to display message
//
// Pre-condition: Options are saved
// Post-condition: Entered message is displayed on the options page for length
//-----------------------------------------------------------------------------------------
function updateStatus(msg, length) {
if (length === undefined) {
length = 1000
}
var status = document.getElementById('status');
status.textContent = msg;
setTimeout(function() {
status.textContent = '';
}, length);
}
//-----------------------------------------------------------------------------------------
//
// Function: isInteger ()
//
// Parameters:
// x: value being checked
//
// Pre-condition: Options are saved
// Post-condition: boolean value is set true if x is an integer, else false
//-----------------------------------------------------------------------------------------
function isInteger(x) {
return (typeof x === 'number') && (x % 1 === 0);
}
//-----------------------------------------------------------------------------------------
//
// Function: validInput ()
//
// Parameters:
// input: value being checked
//
// Pre-condition: Options are saved
// Post-condition: boolean value is set true if input is an integer and greater than zero
//-----------------------------------------------------------------------------------------
function validInput(input) {
return isInteger(input) && input > 0
}
document.addEventListener('DOMContentLoaded', loadOptions);
document.getElementById('save').addEventListener('click',
saveOptions);