-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuct-background.js
198 lines (160 loc) · 5.73 KB
/
uct-background.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
'use strict';
this.defaultSettings = {
toggles: [{
name: 'userchrome style',
enabled: true,
prefix: '\u180E',
state: false
},
{
name: 'Style 2',
enabled: false,
prefix: '\u200B',
state: false
},
{
name: 'Style 3',
enabled: false,
prefix: '\u200C',
state: false
}
],
general: {
settingsVersion: 1.1,
allowMultiple: false,
notifyMe: true
}
}
async function main() {
await initializeSettings();
await updateButtonStatus();
await updateTitlePrefixes();
// Always toggle style 1 on button click
// This event will only fire when the button is not in pop-up mode
browser.browserAction.onClicked.addListener(() => {
userToggle(1)
});
// Trigger on registered hotkeys
browser.commands.onCommand.addListener(userToggle);
console.log('Init complete');
}
// Update user settings to new defaults after updating the extension
async function updateSettings(settings) {
if (settings.general.settingsVersion < defaultSettings.general.settingsVersion) {
if (settings.general.settingsVersion < 1.1) {
settings.general.notifyMe = defaultSettings.general.notifyMe;
}
settings.general.settingsVersion < defaultSettings.general.settingsVersion
await browser.storage.local.set(settings);
}
return settings;
}
async function updateButtonStatus() {
let settings = await browser.storage.local.get('toggles');
// Use reduce function on array to count all enabled toggles
let togglesEnabled = settings.toggles.reduce((count, toggle) => toggle.enabled ? count + 1 : count, 0);
if (togglesEnabled < 2) {
let toggle = settings.toggles[0];
browser.browserAction.setTitle({
title: `Turn ${toggle.name} ` + (toggle.state ? 'off' : 'on')
});
// Disable popup mode
browser.browserAction.setPopup({ popup: null })
console.log('Disabled popup mode');
} else {
browser.browserAction.setTitle({
title: 'Show userchrome toggles'
});
// Enable popup mode
browser.browserAction.setPopup({ popup: "popup/popup.html" })
console.log('Enabled popup mode', togglesEnabled);
}
}
async function getStyleSettings(styleId) {
let settings = await browser.storage.local.get('toggles');
return settings.toggles[styleId - 1].prefix;
}
async function initializeSettings() {
let settings = await browser.storage.local.get();
if (settings.toggles) {
console.log('Loading user settings', settings);
settings = await updateSettings(settings);
} else {
console.log('Initializing default settings', defaultSettings);
await browser.storage.local.set(defaultSettings);
// Open settings page for the user
browser.runtime.openOptionsPage();
}
}
// Detect current window title prefix to allow toggling
async function toggleTitlePrefix(windowId, titlePrefix) {
const windowInfo = await browser.windows.get(windowId.id);
if (windowInfo.title && windowInfo.title.startsWith(titlePrefix))
titlePrefix = '';
return setTitlePrefix(windowId, titlePrefix);
}
// Update prefix for specified window
async function updateTitlePrefixes() {
// Only change current window
const windowId = await browser.windows.getCurrent();
const settings = await browser.storage.local.get(['toggles', 'general']);
const toggles = settings.toggles;
let titlePrefix = '';
// Loop through all toggles
for (let i = 0; i < toggles.length; i++) {
if (toggles[i].state) {
titlePrefix += String(toggles[i].prefix);
// When only one toggle may be active at once, stop after the first
if (!settings.general.allowMultiple)
break;
}
}
browser.windows.update(windowId.id, {
titlePreface: titlePrefix
});
}
// Respond to button clicks and registered hotkeys
async function userToggle(styleId, newState) {
// Extract style number from end of string
styleId = String(styleId).match(/[0-9]+$/);
let settings = await browser.storage.local.get(['toggles', 'general']);
let hrState = 'off';
let toggle = { name: 'all styles' }
if (styleId && !settings.toggles[styleId[0] - 1].enabled) {
console.log('Style is disabled', settings.toggles[styleId[0] - 1]);
return
}
// When only one option allowed or no valid style is selected, reset all others
// Also do this when no valid style has been found
if (!settings.general.allowMultiple || !styleId) {
for (let i = 0; i < settings.toggles.length; i++) {
if (!styleId || styleId[0] - 1 != i)
settings.toggles[i].state = false;
}
}
// When valid style has been selected
if (styleId) {
styleId = styleId[0];
// Invert toggle state or set requested state and save in settings
toggle = settings.toggles[styleId - 1];
if (typeof(newState) == 'undefined')
newState = !toggle.state;
settings.toggles[styleId - 1].state = newState;
if (newState)
hrState = 'on';
}
// Generate user notification when enabled
console.log('Toggling', styleId, hrState);
if (settings.general.notifyMe) {
browser.notifications.create(`toggle-${styleId}`, {
type: "basic",
title: "Userchrome style toggle",
message: `Turned ${toggle.name} ${hrState}`
});
}
await browser.storage.local.set(settings);
// Update title to reflect new truth
updateTitlePrefixes();
updateButtonStatus();
}
main();