-
Notifications
You must be signed in to change notification settings - Fork 1
/
background-script.js
103 lines (91 loc) · 3.17 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
let loadInBackground = true;
// initialise loadInBackground with the value in storage or set to true
browser.storage.local.get("loadInBackground").then(function(res) {
if (res.loadInBackground === undefined) {
browser.storage.local.set({
"loadInBackground": loadInBackground
});
} else {
loadInBackground = res.loadInBackground;
}
});
// update loadInBackground when it is changed in the options
browser.storage.onChanged.addListener(function(changes) {
if (changes["loadInBackground"]) {
loadInBackground = changes["loadInBackground"].newValue;
}
});
// init lastActiveTab
let lastActiveTab = {
"id": 0,
"pinned": false,
"windowId": 0
};
function setLastActiveTab(id, pinned, windowId) {
lastActiveTab.id = id;
lastActiveTab.pinned = pinned;
lastActiveTab.windowId = windowId;
}
// initialise lastActiveTab with current active tab as no initial onActivated ist emitted
browser.tabs.query({active: true, currentWindow: true}).then((activeTabs) => {
setLastActiveTab(activeTabs[0].id, activeTabs[0].pinned, activeTabs[0].windowId);
});
// keep lastActiveTab up to date when user changes tab
browser.tabs.onActivated.addListener(function(activeInfo) {
browser.tabs.get(activeInfo.tabId).then((activeTab) => {
// prevent saving tab when its immediately closed again
if (activeTab.status == "complete") {
setLastActiveTab(activeTab.id, activeTab.pinned, activeTab.windowId);
}
});
});
browser.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (tab.active && tab.status === "complete") {
// save lastActiveTab when it was not saved by onActivated and it has finished loading
browser.tabs.get(tabId).then((activeTab) => {
setLastActiveTab(activeTab.id, activeTab.pinned, activeTab.windowId);
});
}
else if (tab.active && tab.pinned && tab.status === "complete") {
// compensate the fact that no onActivated event is emitted when the active tab is pinned
browser.tabs.get(tabId).then((activeTab) => {
setLastActiveTab(activeTab.id, activeTab.pinned, activeTab.windowId);
});
}
}, {
"properties": ["status", "pinned"]
});
// update lastActiveTab when focusing a different firefox window
browser.windows.onFocusChanged.addListener(function(windowId) {
if (browser.windows.WINDOW_ID_NONE === windowId) {
return;
}
browser.tabs.query({active: true, currentWindow: true}).then((activeTabs) => {
setLastActiveTab(activeTabs[0].id, activeTabs[0].pinned, activeTabs[0].windowId);
});
});
// when a new tab is opened and the last active tab was pinned, move tab to the right and conditionaly keep it activated
browser.tabs.onCreated.addListener(function(tab) {
if (lastActiveTab.pinned && lastActiveTab.windowId === tab.windowId) {
browser.tabs.query({
"windowId": tab.windowId,
"pinned": true
})
.then(function(tabs) {
if (tab.index == tabs.length || tab.index == tabs.length + 1) {
browser.tabs.move(tab.id, {
index: -1
})
.then(function() {
// in all cases scroll tab bar to the right
browser.tabs.update(tab.id, { active: true }).then(function() {
if (loadInBackground) {
// conditionaly refocus source tab
browser.tabs.update(lastActiveTab.id, { active: true });
}
});
});
}
});
}
});