-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
195 lines (156 loc) · 6.19 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
let extensionOn = true;
let tabOn = true;
let tabList = null;
let autofocus = null;
let thisSite = null;
// Initializing tooltip
M.Tooltip.init(document.querySelectorAll('.tooltipped'), { enterDelay: 500 });
$(".power-button").addEventListener("click", toggleExtensionOnOff);
$("#tab-power-button").addEventListener("click", tabPermanentlyOnOff);
$("#tab-switch").addEventListener("change", toggleTabOnOff);
$("#autofocus-switch").addEventListener("change", updateAutofocusList);
$("#shortcuts").addEventListener("click", () => {
chrome.tabs.create({ active: true, url: "chrome://extensions/shortcuts" });
});
$("#heading").addEventListener("click", () => {
chrome.tabs.create({ active: true, url: "https://chrome.google.com/webstore/detail/search-box-focus-hit-tab/amgmdnojamodmpfjaokfgpijhpcednjm" });
});
$("#logo").addEventListener("click", () => {
chrome.tabs.create({ active: true, url: "https://chrome.google.com/webstore/detail/search-box-focus-hit-tab/amgmdnojamodmpfjaokfgpijhpcednjm" });
});
chrome.storage.local.get((storage) => {
extensionOn = storage.power ? storage.power.status : true;
tabOn = storage.tabulation ? storage.tabulation.status : true;
tabList = storage.tabList || {};
autofocus = storage.autofocus || {};
// Updating "thisSite"
chrome.tabs.query({ currentWindow: true, active: true }, function (tab) {
thisSite = tab[0].url.replace(/^.*\/\//, "").replace(/\/.*/, "");
// Add www. to the url if it's not already there.
if (!/^www/.test(thisSite)) thisSite = "www." + thisSite;
updatePopup();
});
});
function updatePopup() {
const suffix = `${extensionOn ? "" : "_disabled"}.png`;
chrome.action.setIcon({
path: {
"16": "icons/icons8-google-web-search-16" + suffix,
"48": "icons/icons8-google-web-search-48" + suffix,
"128": "icons/icons8-google-web-search-128" + suffix
}
});
$("#logo").src = "icons/icons8-google-web-search-full-48" + suffix;
if (extensionOn) {
// Changing the hover color of the power button
$(".power-button").classList.remove("turn-on");
$(".power-button").classList.add("turn-off");
// Display the options
$("#options").classList.remove("hide");
// Update the tab switch
if (tabList[thisSite]) {
$("#tab-switch").checked = false;
$("#tab-tooltip").dataset.tooltip = `Tab disabled for "${thisSite}"`;
} else {
$("#tab-switch").checked = true;
$("#tab-tooltip").dataset.tooltip = `Tab enabled for "${thisSite}"`;
}
// Turn the visual display of autofocus on/off
if (autofocus[thisSite]) {
$("#autofocus-switch").checked = true;
$("#autofocus-tooltip").dataset.tooltip = `Autofocus enabled for "${thisSite}"`;
} else {
$("#autofocus-switch").checked = false;
$("#autofocus-tooltip").dataset.tooltip = `Autofocus disabled for "${thisSite}"`;
}
// Visually disable the whole Tab section if necessary
if (!tabOn) {
$("#tab-switch").disabled = true;
$("#tab-text").style.color = "gray";
$("#tab-section").classList.add("gray-bg");
$("#tab-power-button").style.color = "gray";
}
updateDisplayedShortcuts();
} else {
// Changing the hover color of the power button
$(".power-button").classList.remove("turn-off");
$(".power-button").classList.add("turn-on");
// Hide the options
$("#options").classList.add("hide");
}
}
function updateDisplayedShortcuts() {
// Make sure the shortcuts are displayed correctly
chrome.commands.getAll((commands) => {
let commandExists = false;
commands.forEach(command => {
if (command.name === "focus-search-bar" && command.shortcut) {
commandExists = true;
if (!tabOn || tabList[thisSite]) {
$("#shortcut-display").textContent = command.shortcut;
} else {
$("#shortcut-display").textContent = `Tab or ${command.shortcut}`;
}
}
});
if (!commandExists) {
if (!tabOn || tabList[thisSite]) {
$("#shortcut-display").textContent = "";
} else {
$("#shortcut-display").textContent = "Tab";
}
}
});
}
function updateAutofocusList() {
const addToList = $("#autofocus-switch").checked;
// Update list locally
if (addToList) {
autofocus[thisSite] = true;
$("#autofocus-tooltip").dataset.tooltip = `Autofocus enabled for "${thisSite}"`;
} else {
delete autofocus[thisSite];
$("#autofocus-tooltip").dataset.tooltip = `Autofocus disabled for "${thisSite}"`;
}
// Update chrome storage
chrome.storage.local.set({ autofocus: autofocus });
}
function toggleExtensionOnOff() {
extensionOn = !extensionOn;
updatePopup();
chrome.storage.local.set({ power: { status: extensionOn } });
}
function tabPermanentlyOnOff() {
if (tabOn) {
tabOn = false;
$("#tab-switch").disabled = true;
$("#tab-text").style.color = "gray";
$("#tab-section").classList.add("gray-bg");
$("#tab-power-button").style.color = "gray";
} else {
tabOn = true;
$("#tab-switch").disabled = false;
$("#tab-text").style.color = "black";
$("#tab-section").classList.remove("gray-bg");
$("#tab-power-button").style.color = "rgb(70, 70, 70)";
}
updateDisplayedShortcuts();
chrome.storage.local.set({ tabulation: { status: tabOn } });
}
function toggleTabOnOff() {
if (tabList[thisSite]) {
delete tabList[thisSite];
$("#tab-tooltip").dataset.tooltip = `Tab enabled for "${thisSite}"`;
} else {
tabList[thisSite] = true;
$("#tab-tooltip").dataset.tooltip = `Tab disabled for "${thisSite}"`;
}
updateDisplayedShortcuts();
chrome.storage.local.set({ tabList: tabList });
}
function $(selector, multiple = false) {
if (multiple) {
return document.querySelectorAll(selector);
}
return document.querySelector(selector);
}