-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathservice_worker.js
More file actions
100 lines (83 loc) · 3.17 KB
/
service_worker.js
File metadata and controls
100 lines (83 loc) · 3.17 KB
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
// browser (Firefox) vs chrome (Chromium)
const isChrome = typeof chrome != 'undefined';
const api = isChrome ? chrome : browser;
// attempt working around poor chrome behavior due to
// https://issues.chromium.org/issues/40805401
if (isChrome) {
// https://stackoverflow.com/questions/66618136/persistent-service-worker-in-chrome-extension
const keepAlive = () => {
setInterval(api.runtime.getPlatformInfo, 20000);
};
api.runtime.onStartup.addListener(keepAlive);
keepAlive();
}
// new firefoxen have a setting for this
const tryBrowserPref = async () =>{
// Test for the settings API and the setting itself
if (!api.hasOwnProperty('browserSettings')
|| !api.browserSettings.hasOwnProperty('newTabPosition')) {
return false;
}
// Update the setting value to always add to the right
// even if already set.
await api.browserSettings.newTabPosition.set({ value: 'afterCurrent' });
return true;
};
// Cache the active tab. Populated only via onActivated, so it stays null
// until Chrome activates a tab — which naturally gates makeRight() during
// session restore (onCreated fires before onActivated).
let activeTabCache = null;
api.tabs.onActivated.addListener(async (activeInfo) => {
activeTabCache = await api.tabs.get(activeInfo.tabId);
});
// Move the referenced tab to the immediate right of the active tab,
// or to the immediate right of the last pinned tab.
const makeRight = async newTab => {
// No active tab yet — still in session restore or pre-interaction.
if (!activeTabCache) return;
const activeTab = activeTabCache;
// The new tab either dragged to new window or something went wrong.
if (newTab.windowId != activeTab.windowId) {
return;
}
// To the right
let targetIndex = activeTab.index + 1;
// Only bother moving if it wouldn't organically be placed immediately to the
// right of the active tab.
if (newTab.index == targetIndex) {
return;
}
// We need current window for a few things required for correct tab placement.
// And apparently tab references go STALE. Dammit.
const win = await api.windows.getCurrent({ populate: true });
// Maybe is a restored tab, or another add-on, or something else is wonky.
if (newTab.index < win.tabs.length - 1 || newTab.index > win.tabs.length - 1) {
return;
}
// If the active tab is pinned, we have to set the target index
// to that of the first non-pinned tab.
if (activeTab.pinned) {
targetIndex = getFirstNonPinnedTab(win).index;
}
// YOU GOT TO MOVE IT MOVE IT
try {
await api.tabs.move(newTab.id, { index: targetIndex });
} catch (e) {
console.error('AlwaysRight: tab move fail', e);
}
}
// Return a tab object for the first non-pinned tab in the tab strip
// for the given window.
const getFirstNonPinnedTab = win => {
for (const tab of win.tabs) {
if (!tab.pinned) {
return tab;
}
}
}
// Try native Firefox setting (fire-and-forget for Firefox, no-op on Chrome)
tryBrowserPref();
// Always register manual tab management as fallback.
// On Firefox with browserSettings support, the tab is already placed correctly
// and makeRight() returns early via the targetIndex check.
api.tabs.onCreated.addListener(makeRight);