-
Notifications
You must be signed in to change notification settings - Fork 8
/
background-script.js
193 lines (175 loc) · 5.79 KB
/
background-script.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
/* eslint-disable functional/no-this-expression */
/* eslint-disable functional/no-expression-statement */
/* eslint-disable functional/no-conditional-statement */
/* eslint-disable functional/functional-parameters */
// TODO fix this
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
/**
* @callback predicate
* @param {A} x
* @return {boolean}
* @template A
*/
/**
* @param {predicate<A>} f
* @param {ReadonlyArray<A>} xs
* @template A
* @return {ReadonlyArray<A>}
*/
const dropWhile = (f, xs) =>
xs[0] !== undefined ? dropWhileNotEmpty(f, [xs[0], ...xs.slice(1)]) : [];
/**
* @param {predicate<A>} f
* @param {readonly [A, ...readonly A[]]} xs
* @template A
* @return {ReadonlyArray<A>}
*/
const dropWhileNotEmpty = (f, [x, ...xs]) =>
f(x) ? dropWhile(f, xs) : [x, ...xs];
/**
* @param {predicate<A>} f
* @param {ReadonlyArray<A>} xs
* @template A
* @return {ReadonlyArray<A>}
*/
const takeWhile = (f, xs) =>
xs[0] !== undefined ? takeWhileNotEmpty(f, [xs[0], ...xs.slice(1)]) : [];
/**
* @param {predicate<A>} f
* @param {readonly [A, ...readonly A[]]} xs
* @template A
* @return {ReadonlyArray<A>}
*/
const takeWhileNotEmpty = (f, [x, ...xs]) =>
f(x) ? [x, ...takeWhile(f, xs)] : [];
/**
* @param {ReadonlyArray<A>} xs
* @template A
* @return {A | undefined}
*/
const last = (xs) => xs[xs.length - 1];
// TODO https://github.com/danielnixon/link-fixer/issues/13
const defaultTabPosition = "relatedAfterCurrent";
/**
* @return {Promise<Record<string, any>>}
*/
const getOptions = () =>
new Promise((resolve) => chrome.storage.sync.get((items) => resolve(items)));
const tabPositions = {
/**
* @param {chrome.tabs.Tab} senderTab
* @param {ReadonlyArray<chrome.tabs.Tab>} tabs
* @return {number|undefined}
*/
relatedAfterCurrent: (senderTab, tabs) => {
const tabsAfterSenderTab = dropWhile(
(tab) => tab.index <= senderTab.index,
tabs
);
const tabsOpenedBySenderTab = takeWhile(
(tab) =>
tab.openerTabId !== undefined && tab.openerTabId === senderTab.id,
tabsAfterSenderTab
);
const lastTabOpenedBySenderTab = last(tabsOpenedBySenderTab);
return lastTabOpenedBySenderTab !== undefined
? lastTabOpenedBySenderTab.index + 1
: undefined;
},
/**
* @param {chrome.tabs.Tab} senderTab
* @return {number}
*/
afterCurrent: (senderTab) => senderTab.index + 1,
/**
* @return {number}
*/
atEnd: () => Number.MAX_SAFE_INTEGER,
};
// Respect Firefox browserSettings if we have them. (`browser` is undefined in Chrome,
// `newTabPosition` is undefined in older versions of Firefox).
// See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/browserSettings
/** @type {browser.types.Setting | undefined | null} */
const newTabPosition =
// `browser` is undefined in Chrome
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.browser &&
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
this.browser.browserSettings &&
this.browser.browserSettings.newTabPosition;
/**
* @returns {Promise<"afterCurrent" | "relatedAfterCurrent" | "atEnd">} tab position
*/
const getNewTabPosition = () =>
// `newTabPosition` is undefined in older versions of Firefox
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition, @typescript-eslint/no-unsafe-return
newTabPosition !== undefined
? newTabPosition.get({}).then((x) => x.value) // eslint-disable-line @typescript-eslint/no-unsafe-return
: Promise.resolve(defaultTabPosition);
/**
* @param {chrome.tabs.Tab|undefined} senderTab
* @param {ReadonlyArray<chrome.tabs.Tab>} tabs
* @return {Promise<number|undefined>}
*/
const calculateNewTabIndex = (senderTab, tabs) => {
if (senderTab !== undefined) {
return getNewTabPosition().then((newTabPosition) => {
switch (newTabPosition) {
case "afterCurrent":
return tabPositions.afterCurrent(senderTab);
case "relatedAfterCurrent":
return tabPositions.relatedAfterCurrent(senderTab, tabs);
case "atEnd":
return tabPositions.atEnd();
default:
return undefined;
}
});
} else {
return Promise.resolve(undefined);
}
};
chrome.runtime.getPlatformInfo((info) => {
const isMac = info.os === "mac";
chrome.runtime.onMessage.addListener(
(/** @type {Message} */ message, sender) => {
const openInNewWindow =
!!message.shiftKey && !(message.metaKey || message.ctrlKey);
if (openInNewWindow) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
chrome.windows.create({
url: message.url,
});
} else {
// ctrl+click opens a context menu on Mac, so don't create the new tab.
const shouldOpenTab = !(isMac && message.ctrlKey);
if (shouldOpenTab) {
chrome.tabs.query(
{
windowId: sender.tab && sender.tab.windowId,
},
(tabs) => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
calculateNewTabIndex(sender.tab, tabs).then((newTabIndex) => {
return getOptions().then((options) => {
const openInForeground =
options["tabPosition"] === "foreground";
const active = message.shiftKey
? !openInForeground
: openInForeground;
// eslint-disable-next-line @typescript-eslint/no-floating-promises
chrome.tabs.create({
url: message.url,
active: active,
openerTabId: sender.tab && sender.tab.id,
index: newTabIndex,
});
});
});
}
);
}
}
}
);
});