-
Notifications
You must be signed in to change notification settings - Fork 16
/
background.js
217 lines (184 loc) · 6.46 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
Firefox addon "Undo Close Tab"
Copyright (C) 2020 Manuel Reimer <[email protected]>
Copyright (C) 2017 YFdyh000 <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
// Fired if the toolbar button is clicked.
// Restores the last closed tab in list.
async function ToolbarButtonClicked(tab, OnClickData) {
// Get list of closed tabs and exit if there are none
const tabs = await TabHandling.GetLastClosedTabs(false, true);
if (!tabs.length)
return;
// Always restore the most recently closed tab
await TabHandling.Restore(tabs[0].sessionId);
// Next, run over the tabs and also restore all tabs closed "with the last
// closed one" (to mass-restore "close to the right" or "close others")
const prefs = await Storage.get();
if (prefs.restoreGroup && tabs[0]._tabCloseTime) {
for (let ti = 1; ti < tabs.length; ti++) {
if (!tabs[ti]._tabCloseTime)
break;
if (tabs[ti - 1]._tabCloseTime - tabs[ti]._tabCloseTime < prefs.groupTime)
await TabHandling.Restore(tabs[ti].sessionId);
else
break;
}
}
// Allow to restore in background with shift+click on the toolbar button
if (OnClickData.modifiers.includes("Shift"))
browser.tabs.update(tab.id, {active: true});
}
// Helper function used to create menu entries based on tab properties
function TabMenuProperties(tab, id_prefix) {
return {
id: id_prefix + ":" + tab.sessionId,
title: tab.title.replace(/&/g, "&&"),
icons: {18: tab.favIconUrl || "icons/no-favicon.svg"}
}
}
// Fired if a menu is shown
// Updates the context menu entries with the list of last closed tabs.
var lastMenuInstanceId = 0;
var nextMenuInstanceId = 1;
async function OnMenuShown() {
var menuInstanceId = nextMenuInstanceId++;
lastMenuInstanceId = menuInstanceId;
const prefs = await Storage.get();
const tabs = await TabHandling.GetLastClosedTabs(prefs.showNumber, prefs.onlyCurrent);
// How many items are allowed on the top level?
const max_allowed = browser.menus.ACTION_MENU_TOP_LEVEL_LIMIT - (prefs.showClearList ? 1 : 0);
// Start with a completely empty menu
await browser.menus.removeAll();
// This block is for creating the "page" or "tab" context menus.
// They are only drawn if at least one tab can be restored.
if ((prefs.showTabMenu || prefs.showPageMenu) && tabs.length) {
let contexts = [];
if (prefs.showTabMenu)
contexts.push("tab");
if (prefs.showPageMenu)
contexts.push("page");
let rootmenu = browser.menus.create({
id: "RootMenu",
title: browser.i18n.getMessage("page_contextmenu_submenu"),
contexts: contexts
});
tabs.forEach((tab) => {
browser.menus.create({
...TabMenuProperties(tab, "PM"),
contexts: contexts,
parentId: rootmenu
});
});
if (prefs.showClearList) {
browser.menus.create({
id: "ClearListSeparator",
type: "separator",
contexts: contexts,
parentId: rootmenu
});
browser.menus.create({
id: "PM:ClearList",
title: browser.i18n.getMessage("clearlist_menuitem"),
contexts: contexts,
parentId: rootmenu
});
}
}
if (prefs.showPageMenuitem) {
browser.menus.create({
id: "UndoCloseTab",
title: browser.i18n.getMessage("extensionName"),
contexts: ["page"]
});
}
// If closed tabs count is less or equal maximum allowed menu entries, then
// no "More items" menu is needed.
if (tabs.length <= max_allowed) {
tabs.forEach((tab) => {
browser.menus.create({
...TabMenuProperties(tab, "BA"),
contexts: ["browser_action"]
});
});
}
// If there are too much items, place "maximum - 1" items to the top level
// and place the rest of them into a submenu.
else {
tabs.splice(0, max_allowed - 1).forEach((tab) => {
browser.menus.create({
...TabMenuProperties(tab, "BA"),
contexts: ["browser_action"]
});
});
let moreMenu = browser.menus.create({
id: "MoreClosedTabs",
title: browser.i18n.getMessage("more_entries_menu"),
icons: {18: "icons/folder.svg"},
contexts: ["browser_action"]
});
tabs.forEach((tab) => {
browser.menus.create({
...TabMenuProperties(tab, "BA"),
contexts: ["browser_action"],
parentId: moreMenu
});
});
}
if (tabs.length && prefs.showClearList) {
browser.menus.create({
id: "BA:ClearList",
title: browser.i18n.getMessage("clearlist_menuitem"),
contexts: ["browser_action"],
});
}
// This is how Mozilla describes how to prevent race conditions.
if (menuInstanceId !== lastMenuInstanceId) {
return; // Menu was closed and shown again.
}
await browser.menus.refresh();
}
// Fired if one of our context menu entries is clicked.
// Restores the tab, referenced by this context menu entry.
async function ContextMenuClicked(aInfo) {
if (aInfo.menuItemId == "UndoCloseTab") {
await ToolbarButtonClicked();
return;
}
if (aInfo.menuItemId.endsWith("ClearList")) {
const prefs = await Storage.get();
TabHandling.ClearList(prefs.onlyCurrent);
return;
}
const sessionid = aInfo.menuItemId.substring(aInfo.menuItemId.indexOf(":") + 1);
TabHandling.Restore(sessionid);
}
TabHandling.Init();
//
// Register event listeners
//
// Check for the API we expect for the "full desktop feature set"
// This needs revision as soon as there is some Android browser with
// browser.sessions support.
if (browser.menus !== undefined &&
browser.windows !== undefined &&
browser.sessions !== undefined) {
browser.menus.onShown.addListener(OnMenuShown);
browser.menus.onClicked.addListener(ContextMenuClicked);
browser.menus.onHidden.addListener(function() {
lastMenuInstanceId = 0;
});
}
browser.browserAction.onClicked.addListener(ToolbarButtonClicked);
IconUpdater.Init("icons/undoclosetab.svg");