-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpopup.js
205 lines (178 loc) · 7.7 KB
/
popup.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Checks if URL is valid or not.
function ValidUrl(str) {
// Check if URL is valid.
if (!/^https?:\/\//i.test(str)) {
str = 'http://' + str;
}
var pattern = new RegExp('(http|ftp|https)://[\\w-]+(\\.[\\w-]+)+([\\w-.,@?^=%&:/~+#-]*[\\w@?^=%&;/~+#-])?');
if (!pattern.test(str)) {
return false;
} else {
return true;
}
}
function onLoad() {
// Load link array on chrome.storage.sync API on start.
chrome.storage.sync.get(["linkArray"], function (items) {
document.getElementById("link1").value = items.linkArray[0];
document.getElementById("link2").value = items.linkArray[1];
document.getElementById("link3").value = items.linkArray[2];
document.getElementById("link4").value = items.linkArray[3];
});
// Get all commands, and set texts on divs.
chrome.commands.getAll(function (commandsArray) {
for (var i = 1; i < commandsArray.length; i++) {
var statusShortcut = document.getElementById("shortcut" + i);
if (commandsArray[i].shortcut == "") {
statusShortcut.innerText = "Not Set";
statusShortcut.style.backgroundColor = "red";
} else {
statusShortcut.innerText = commandsArray[i].shortcut;
statusShortcut.style.backgroundColor = "#00E676";
}
}
});
// Load dark theme if darkTheme = true.
chrome.storage.sync.get(["darkTheme"], function (items) {
if (items.darkTheme) {
var wrapper = document.getElementById("wrapper");
wrapper.style.backgroundColor = "#333333";
document.body.style.backgroundColor = "#333333";
document.body.style.color = "#FFFFFF";
wrapper.style.color = "#FFFFFF";
document.getElementById("icon").src = "images/icon_48x48_light.png";
document.getElementById("link1").style.color = "#FFFFFF";
document.getElementById("link2").style.color = "#FFFFFF";
document.getElementById("link3").style.color = "#FFFFFF";
document.getElementById("link4").style.color = "#FFFFFF";
}
});
}
function onSubmit() {
// Reject save if URLs are not valid.
var invalidURL = false;
for (i = 1; i < 5; i++) {
if ((!ValidUrl(document.getElementById("link" + i).value)) && (document.getElementById("link" + i).value)) {
document.getElementById("link" + i).className = "invalidInput";
invalidURL = true;
}
}
if (invalidURL)
return;
//update the link array
var linkArray = [document.getElementById("link1").value,
document.getElementById("link2").value,
document.getElementById("link3").value,
document.getElementById("link4").value];
// Store link array in chrome storage API.
chrome.storage.sync.set({
"linkArray": linkArray
}, function () {
//removes the old notification bar and sets a new one
var current = document.getElementById("notification");
var newone = current.cloneNode(true); //copy current to new
current.parentNode.replaceChild(newone, current); //replace current
newone.style.display = "block"; //activate animation of new
});
// On click, keep save changes green.
document.getElementById("submit").className = "saved";
}
function toggleDarkTheme() {
// Change GUI theme.
chrome.storage.sync.get(["darkTheme"], function (items) {
// Grab wrapper from HTML.
var wrapper = document.getElementById("wrapper");
// Toggle GUI colors.
if (items.darkTheme) {
wrapper.style.backgroundColor = "#F5F5F5";
document.body.style.backgroundColor = "#F5F5F5";
wrapper.style.color = "#000000";
document.getElementById("link1").style.color = "#000000";
document.getElementById("link2").style.color = "#000000";
document.getElementById("link3").style.color = "#000000";
document.getElementById("link4").style.color = "#000000";
var icon = document.getElementById("icon").src = "images/icon_48x48.png";
} else {
wrapper.style.backgroundColor = "#333333";
document.body.style.backgroundColor = "#333333";
wrapper.style.color = "#FFFFFF";
document.getElementById("link1").style.color = "#FFFFFF";
document.getElementById("link2").style.color = "#FFFFFF";
document.getElementById("link3").style.color = "#FFFFFF";
document.getElementById("link4").style.color = "#FFFFFF";
var icon = document.getElementById("icon").src = "images/icon_48x48_light.png";
}
// Save new dark theme settings.
chrome.storage.sync.set({
"darkTheme": !items.darkTheme
}, function () {});
});
}
function onStatusClick() {
chrome.tabs.create({
url: "chrome://extensions/configureCommands"
});
}
function onMouseHover() {
var statusElement = this;
var current = parseInt(this.id.slice(-1));
// Check statuses if shortcuts are set, and change colors accordingly on hover.
chrome.commands.getAll(function (commandsArray) {
//Checking to see if shortcut is set
if (commandsArray[current].shortcut == "") {
statusElement.style.backgroundColor = "#C62828";
} else {
statusElement.style.backgroundColor = "#00C853";
}
});
}
function onMouseOut() {
var statusElement = this;
var current = parseInt(this.id.slice(-1));
// Check statuses if shortcuts are set, and change colors accordingly on hover.
chrome.commands.getAll(function (commandsArray) {
if (commandsArray[current].shortcut == "") {
statusElement.style.backgroundColor = "red";
} else {
statusElement.style.backgroundColor = "#00E676";
}
});
}
function onInputFocus() {
// resets style to default in case it was previously invalid
this.className = "input";
}
function onType() {
document.getElementById("submit").className = "unsaved";
}
// Load links.
window.onload = onLoad;
// Save links.
document.getElementById("submit").onclick = onSubmit;
// Clicking status opens up 'Keyboard shortcuts' in extensions menu.
document.getElementById("shortcut1").onclick = onStatusClick;
document.getElementById("shortcut2").onclick = onStatusClick;
document.getElementById("shortcut3").onclick = onStatusClick;
document.getElementById("shortcut4").onclick = onStatusClick;
// On mouse hover, make background color darker.
document.getElementById("shortcut1").onmouseover = onMouseHover;
document.getElementById("shortcut2").onmouseover = onMouseHover;
document.getElementById("shortcut3").onmouseover = onMouseHover;
document.getElementById("shortcut4").onmouseover = onMouseHover;
// On mouse out, make background color lighter.
document.getElementById("shortcut1").onmouseout = onMouseOut;
document.getElementById("shortcut2").onmouseout = onMouseOut;
document.getElementById("shortcut3").onmouseout = onMouseOut;
document.getElementById("shortcut4").onmouseout = onMouseOut;
// Clicking icon toggles dark theme
document.getElementById("cf").onclick = toggleDarkTheme;
// Input focus resets input style
document.getElementById("link1").onfocus = onInputFocus;
document.getElementById("link2").onfocus = onInputFocus;
document.getElementById("link3").onfocus = onInputFocus;
document.getElementById("link4").onfocus = onInputFocus;
// Change save button color when user types in field.
document.getElementById("link1").onkeypress = onType;
document.getElementById("link2").onkeypress = onType;
document.getElementById("link3").onkeypress = onType;
document.getElementById("link4").onkeypress = onType;