-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
204 lines (156 loc) · 4.96 KB
/
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
199
200
201
202
203
204
init();
let platformInfo;
async function init() {
//await extensionStorage.clear();
// Ensure relevant browser settings are set
if (browser.browserSettings && browser.browserSettings.contextMenuShowEvent) {
await browser.browserSettings.contextMenuShowEvent.set({ value: "mouseup" });
}
// Ensure options are initialized
await getOptions();
// Cache platform info
platformInfo = await browser.runtime.getPlatformInfo();
// Add message listener
browser.runtime.onMessage.addListener(onMessage);
}
async function onMessage(message, sender) {
switch (message.type) {
case "getOptions": {
return await getOptions();
}
case "setOptions": {
await setOptions(message.data);
return;
}
case "closeCurrentTab": {
const currentWindowInfo = await browser.windows.getCurrent();
if (currentWindowInfo) {
const currentTabInfo = await browser.tabs.query({ active: true, windowId: currentWindowInfo.id });
if (currentTabInfo.length > 0) {
await browser.tabs.remove(currentTabInfo[0].id);
}
}
return;
}
case "createNewTab": {
await browser.tabs.create({});
return;
}
case "reloadCurrentTab": {
await browser.tabs.reload();
return;
}
case "activateLeftTab": {
const currentWindowInfo = await browser.windows.getCurrent();
if (currentWindowInfo) {
const currentTabInfo = (await browser.tabs.query({ active: true, windowId: currentWindowInfo.id }))[0];
if (currentTabInfo) {
for (let leftTabIndex = currentTabInfo.index - 1; leftTabIndex >= 0; leftTabIndex--) {
const leftTabInfo = (await browser.tabs.query({ windowId: currentWindowInfo.id, index: leftTabIndex }))[0];
if (!leftTabInfo) {
break;
}
if (message.data.skipTabsWithSpecialPages && isSpecialPageURL(leftTabInfo.url)) {
continue;
}
await browser.tabs.update(leftTabInfo.id, { active: true });
break;
}
}
}
return;
}
case "activateRightTab": {
const currentWindowInfo = await browser.windows.getCurrent();
if (currentWindowInfo) {
const currentTabInfo = (await browser.tabs.query({ active: true, windowId: currentWindowInfo.id }))[0];
if (currentTabInfo) {
for (let rightTabIndex = currentTabInfo.index + 1; ; rightTabIndex++) {
const rightTabInfo = (await browser.tabs.query({ windowId: currentWindowInfo.id, index: rightTabIndex }))[0];
if (!rightTabInfo) {
break;
}
if (message.data.skipTabsWithSpecialPages && isSpecialPageURL(rightTabInfo.url)) {
continue;
}
await browser.tabs.update(rightTabInfo.id, { active: true });
break;
}
}
}
return;
}
case "undoCloseTab": {
const currentWindowInfo = await browser.windows.getCurrent();
if (currentWindowInfo) {
const recentlyClosedSessions = await browser.sessions.getRecentlyClosed();
const filteredSessions = recentlyClosedSessions.filter((session) => {
return session.tab != null && (session.tab.windowId === 0 || session.tab.windowId === currentWindowInfo.id);
});
filteredSessions.sort((s1, s2) => s2.lastModified - s1.lastModified);
if (filteredSessions.length > 0) {
await browser.sessions.restore(filteredSessions[0].tab.sessionId);
}
}
return;
}
case "getPlatformInfo": {
return platformInfo;
}
}
}
async function getOptions() {
const defaultOptions = {
leftEnabled: true,
rightEnabled: true,
upEnabled: true,
downEnabled: true,
ctrlLeftEnabled: true,
ctrlRightEnabled: true,
ctrlUpEnabled: true,
ctrlDownEnabled: true,
leftAction: 'navigateBack',
rightAction: 'navigateForward',
upAction: 'createNewTab',
downAction: 'reload',
ctrlLeftAction: 'activateLeftTab',
ctrlRightAction: 'activateRightTab',
ctrlUpAction: 'undoCloseTab',
ctrlDownAction: 'closeCurrentTab',
minDelta: 4
}
const options = (await extensionStorage.get('options')).options || {};
let hadMissingOption = false;
for (const propertyName in defaultOptions) {
if (options[propertyName] == null) {
hadMissingOption = true;
options[propertyName] = defaultOptions[propertyName];
}
}
if (hadMissingOption) {
await setOptions(options);
}
return options;
}
async function setOptions(newOptions) {
await extensionStorage.set({ options: newOptions });
await broadcastMessageToContentScripts({ type: 'optionsUpdated', data: newOptions });
}
async function broadcastMessageToContentScripts(message) {
const tabs = await browser.tabs.query({ windowType: "normal" });
for (tab of tabs) {
if (/^https?:\/\//.test(tab.url)) {
browser.tabs.sendMessage(tab.id, message);
}
}
}
function isSpecialPageURL(url) {
if (!/^https?:\/\//.test(url)) {
return true;
} else if (navigator.userAgent.indexOf("Firefox") >= 0 && (/^https?:\/\/addons\.mozilla\.org/.test(url) || /^https?:\/\/testpilot\.firefox\.com/.test(url))) {
return true;
} else if (navigator.userAgent.indexOf("Chrome") >= 0 && /^https?:\/\/chrome\.google\.com\/webstore\//.test(url)) {
return true;
}
return false;
}