-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtabhandling.js
93 lines (78 loc) · 3.51 KB
/
tabhandling.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
/*
Firefox addon "Undo Close Tab"
Copyright (C) 2023 Manuel Reimer <[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";
// Abstraction layer for the handling of closed tabs. Handles bug workarounds
// and Android support.
const TabHandling = {
// Returns a list of last closed tabs.
// Parameters:
// aMaxResults: Maximum amount of results to return (false means "all")
// aOnlyCurrent: If "true" only returns tabs from the current window
// Return value: List of tab objects
GetLastClosedTabs: async function(aMaxResults, aOnlyCurrent) {
// Get the session API tabs first (these should be the most current tabs)
let tabs = await this._SessionsGetLastClosedTabs();
// If requested and possible (windows API is available), then limit list
// to tabs from the current window
if (aOnlyCurrent && browser.windows !== undefined) {
const currentWindow = await browser.windows.getCurrent();
tabs = tabs.filter((tab) => { return tab.windowId === undefined || tab.windowId === currentWindow.id});
}
// HACK! See https://github.com/M-Reimer/undoclosetab/issues/117#issuecomment-1527397147
tabs = tabs.filter((tab) => { return tab.title !== undefined });
// If requested, limit the return list to the given amount of entries
if (aMaxResults && tabs.length > aMaxResults)
tabs = tabs.splice(0, aMaxResults);
// Finally return the tab list
return tabs;
},
// Private method which gets a recently closed tabs list from "sessions" API
_SessionsGetLastClosedTabs: async function () {
// Filter the saved closed items to only contain tabs
const sessions = await browser.sessions.getRecentlyClosed();
const tabs = sessions.filter(s => s.tab);
// Now remove the additional "tab" object, we have to walk through,
// (don't need that) after backing up the session lastModified.
tabs.forEach((o, i, a) => {
a[i].tab._tabCloseTime = a[i].lastModified;
a[i] = a[i].tab
});
// Finally return the tab list
return tabs;
},
// This function clears the list of recently closed items
ClearList: async function(aOnlyCurrent) {
// Prefilter the tab list and remove the resulting tabs
let tabs = await this._SessionsGetLastClosedTabs();
if (aOnlyCurrent) {
const currentWindow = await browser.windows.getCurrent();
tabs = tabs.filter(t => t.windowId === currentWindow.id);
}
const promises = [];
tabs.forEach((tab) => {
promises.push(browser.sessions.forgetClosedTab(tab.windowId, tab.sessionId));
});
await Promise.all(promises);
},
Restore: async function(aSessionId) {
const session = await browser.sessions.restore(aSessionId);
const currentWindow = await browser.windows.getCurrent();
if (session.tab.windowId != currentWindow.id)
browser.windows.update(session.tab.windowId, {focused: true});
},
Init: function() {
// Currently unused
}
}