-
Notifications
You must be signed in to change notification settings - Fork 5
/
service_worker.js
138 lines (118 loc) · 4.07 KB
/
service_worker.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
// 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 tryInsertAfter = new Promise((resolve, reject) => {
// test for the settings api
// and the setting itself
if (api.hasOwnProperty('browserSettings')
&& api.browserSettings.newTabPosition) {
const RIGHT = 'afterCurrent';
// check the setting value
api.browserSettings.newTabPosition.get({}).then(pref => {
// update the setting value if not set to always add to the right
if (pref.value != RIGHT) {
api.browserSettings.newTabPosition.set({
value: RIGHT
});
}
});
resolve();
}
// no settings object (chromium)
// or no setting (older firefox)
reject();
});
const init = () => {
// Reference to the active tab
var activeTab = null;
// Update cached ref to active tab
function cacheActiveTab() {
api.tabs.query({currentWindow: true, active: true}).then(function(tabs) {
if (activeTab === null) {
initEventHandlers();
}
activeTab = tabs[0];
});
}
// Get active tab reference on startup
cacheActiveTab();
// As getting active tab id at startup is async, really start our own
// activity until we actually have it.
// This is called in cacheActiveTab if activeTab is null.
function initEventHandlers() {
// Update reference to active tab any time there's a tab
// activated event.
api.tabs.onActivated.addListener(function(activeInfo) {
api.tabs.get(activeInfo.tabId).then(function(tabInfo) {
activeTab = tabInfo;
});
});
// Any time a new tab is created, set its index to the index
// of the active tab, plus one.
api.tabs.onCreated.addListener(makeRight);
// ODD. If instead of this, I get a fresh reference to the active tab
// right before moving, it still has stale index!!
// Soooo, I guess we're doing this.
api.tabs.onMoved.addListener(cacheActiveTab);
}
// Move the referenced tab to the immediate right of the active tab,
// or to the immediate right of the last pinned tab.
function makeRight(newTab) {
// Too soon after startup.
if (!activeTab) {
return;
}
// The new tab either dragged to new window or something went wrong.
if (newTab.windowId != activeTab.windowId) {
return;
}
// To the right
var 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.
api.windows.getCurrent({populate: true}).then(function(win) {
// 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
api.tabs.move(newTab.id, { index: targetIndex }).then(function(t) {
// woohoo.
}, function(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.
function getFirstNonPinnedTab(win) {
for (var tab of win.tabs) {
if (!tab.pinned) {
return tab;
}
}
}
};
tryInsertAfter.then(() => /* newer firefox */ true, init);