-
Notifications
You must be signed in to change notification settings - Fork 20
/
background.js
153 lines (135 loc) · 5.17 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
const TAB_SESSION_KEY = "open-tabs-next-to-current-tab-uuid";
const windowIdTabIdsMapping = new Map();
const knownTabIds = [];
function setCurrentTabId(windowId, tabId) {
if (!windowIdTabIdsMapping.has(windowId)) {
windowIdTabIdsMapping.set(windowId, []);
}
windowIdTabIdsMapping.get(windowId).unshift(tabId);
}
function initializeCurrentTabId() {
const query = {
active: true,
currentWindow: true,
};
browser.tabs.query(query).then(
([tab]) => {
if (!tab) {
return;
}
setCurrentTabId(tab.windowId, tab.id);
}
);
}
function handleTabActivated(activeInfo) {
setCurrentTabId(activeInfo.windowId, activeInfo.tabId);
}
initializeCurrentTabId();
browser.tabs.onActivated.addListener(handleTabActivated);
browser.tabs.onCreated.addListener(moveTab);
browser.tabs.onRemoved.addListener(forgetTab);
browser.commands.onCommand.addListener(function(command) {
if (command == "open-new-tab-at-default-location") {
browser.tabs.onCreated.removeListener(moveTab);
browser.tabs.onCreated.addListener(fixListeners);
browser.tabs.create({});
}
});
if (browser.sessions.getTabValue && browser.sessions.setTabValue) {
addSessionKeyToExistingTabs();
}
function addSessionKeyToExistingTabs() {
browser.tabs.query({}).then(
allTabs => {
for (const existingTab of allTabs) {
browser.sessions.getTabValue(existingTab.id, TAB_SESSION_KEY).then(
(uuid) => {
if (uuid) {
if (knownTabIds.indexOf(uuid) === -1) {
knownTabIds[existingTab.id] = uuid;
return;
}
}
const newUUID = uuidv4();
knownTabIds[existingTab.id] = newUUID;
browser.sessions.setTabValue(existingTab.id, TAB_SESSION_KEY, newUUID);
}
);
}
}
);
}
function fixListeners() {
browser.tabs.onCreated.removeListener(fixListeners);
browser.tabs.onCreated.addListener(moveTab);
}
function moveTab(newTab) {
browser.windows.getCurrent({populate: true})
.then(
(currentWindow) => {
const tabIds = windowIdTabIdsMapping.get(currentWindow.id);
// handle tab being activated before being "created"
const currentTabId = tabIds[0] == newTab.id ? tabIds[1] : tabIds[0];
return Promise.all([
currentWindow,
browser.tabs.get(currentTabId),
]);
}
)
.then(
([currentWindow, currentTab]) => {
if (currentTab.windowId !== newTab.windowId) {
// tab created by drag into window without focus change
return;
}
if (browser.sessions.getTabValue && browser.sessions.setTabValue) {
browser.sessions.getTabValue(newTab.id, TAB_SESSION_KEY).then(
(uuid) => {
// restored and duplicated tabs have a uuid
if (uuid) {
// duplicated tabs' uuid will be in knownTabIds, so this is a restored tab
if (knownTabIds.indexOf(uuid) === -1) {
knownTabIds[newTab.id] = uuid;
// tab position should be correct
return;
}
}
// new or duplicated tabs need to be moved
const newUUID = uuidv4();
knownTabIds[newTab.id] = newUUID;
browser.sessions.setTabValue(newTab.id, TAB_SESSION_KEY, newUUID);
browser.tabs.move(newTab.id, {index: getNewIndex(currentWindow, currentTab)});
}
);
} else {
const isUndoCloseTab = newTab.index < currentWindow.tabs.length - 1 && !newTab.openerTabId;
const isRecoveredTab = newTab.index > currentWindow.tabs.length - 1;
if (!isUndoCloseTab && !isRecoveredTab) {
browser.tabs.move(newTab.id, {index: getNewIndex(currentWindow, currentTab)});
}
}
}
);
}
function forgetTab(tabId) {
delete knownTabIds[tabId];
}
function getNewIndex(currentWindow, currentTab) {
if (!currentTab.pinned) {
return currentTab.index + 1;
}
let lastPinnedTab = undefined;
for (const tab of currentWindow.tabs) {
if (tab.pinned) {
lastPinnedTab = tab;
} else {
return lastPinnedTab.index + 1;
}
}
}
function uuidv4() {
// see https://stackoverflow.com/a/2117523/520061
return ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
);
}