This repository was archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
139 lines (120 loc) · 4.54 KB
/
Copy pathindex.js
File metadata and controls
139 lines (120 loc) · 4.54 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
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
"use strict";
function activatePlugin(initialTabs) {
let repositionRightOnly = false;
function updateOptions() {
browser.storage.local.get(["repositionRightOnly"], result => {
repositionRightOnly = result.repositionRightOnly || false;
console.log("loaded settings:", {repositionRightOnly});
});
}
updateOptions();
browser.storage.onChanged.addListener(updateOptions);
const windowInfoMap = new Map();
function getWindowInfo(windowId) {
let windowInfo = windowInfoMap.get(windowId);
if(windowInfo === undefined) {
windowInfo = {numTabs: 0};
windowInfoMap.set(windowId, windowInfo);
}
return windowInfo;
}
initialTabs.forEach(tab => {
const windowInfo = getWindowInfo(tab.windowId);
windowInfo.numTabs++;
if(tab.active) {
windowInfo.activeTabId = tab.id;
}
});
console.log("initial tabs loaded:", {windowInfoMap, initialTabs});
browser.tabs.onActivated.addListener(({tabId, windowId}) => {
const windowInfo = getWindowInfo(windowId);
windowInfo.lastActiveTabId = windowInfo.activeTabId;
windowInfo.activeTabId = tabId;
console.log("onActivated:", {tabId, windowId, windowInfo});
});
function getLastTabIndex(openedTab) {
const windowInfo = getWindowInfo(openedTab.windowId);
const lastTabId = openedTab.active ? windowInfo.lastActiveTabId : windowInfo.activeTabId;
if(lastTabId === undefined) {
// opening new window
return Promise.reject("no active tab");
}
return browser.tabs.get(lastTabId).then(tab => {
console.log("Last tab:", tab);
return tab.index;
});
}
function moveTabNextToCurrent(openedTab) {
return getLastTabIndex(openedTab)
.then(index => {
// If new tab is left of last, last one has already been shifted one
if(openedTab.index > index) {
index++;
}
console.log("New tab index: " + index);
return browser.tabs.move(openedTab.id, {index: index});
}).catch(e => {
if(e !== "no active tab") {
throw e;
}
});
}
let enabled = true;
browser.tabs.onCreated.addListener(tab => {
console.log("Tab created:", tab);
const windowInfo = getWindowInfo(tab.windowId);
windowInfo.numTabs++;
if(tab.active) {
windowInfo.activeTabId = tab.id;
}
if(windowInfo.opening) {
const now = new Date().getTime();
if(now - windowInfo.lastTabCreated < 200) {
console.log("New tab created too soon after window, probably restoring, skipping:", {tab, windowInfo});
windowInfo.lastTabCreated = now;
return;
}
windowInfo.opening = false;
}
if(enabled && (!repositionRightOnly || tab.index === windowInfo.numTabs - 1)) {
moveTabNextToCurrent(tab);
}
});
browser.tabs.onRemoved.addListener((tabId, removeInfo) => {
const windowInfo = getWindowInfo(removeInfo.windowId);
windowInfo.numTabs--;
console.log("Tab removed:", {tabId, removeInfo});
});
browser.commands.onCommand.addListener(command => {
if(command === "open-new-default") {
enabled = false;
browser.tabs.create({}).then(() => {
enabled = true;
}).catch(e => {
enabled = true;
throw e;
});
}
});
browser.windows.onRemoved.addListener(windowId => {
console.log("Window removed:", windowId);
windowInfoMap.delete(windowId);
});
browser.windows.onCreated.addListener(window => {
console.log("New window:", window);
const windowInfo = getWindowInfo(window.id);
windowInfo.opening = true;
windowInfo.lastTabCreated = new Date().getTime();
});
browser.tabs.onAttached.addListener((tabId, attachInfo) => {
console.log("Tab attached:", tabId, attachInfo);
const windowInfo = getWindowInfo(attachInfo.newWindowId);
windowInfo.numTabs++;
});
browser.tabs.onDetached.addListener((tabId, detachInfo) => {
console.log("Tab detached:", tabId, detachInfo);
const windowInfo = getWindowInfo(detachInfo.oldWindowId);
windowInfo.numTabs--;
});
}
browser.tabs.query({}).then(activatePlugin);