This repository has been archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tabsweeper.js
174 lines (149 loc) · 4.58 KB
/
tabsweeper.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
const notifications = browser.notifications;
const tabs = browser.tabs;
const storage = browser.storage;
class TabSweeper {
constructor() {
this.urls = {
iconUrl: "icons/tabsweeper.png",
sidebarUrl: "/sidebar/panel.html"
};
this.messages = {
pressToOpen: "\n\nPress Ctrl+Shift+Y to toggle the Tab Sweeper sidebar",
nothingToClean: "Your browser tabs are already clean!",
tabsSaved: "Tabs saved and closed!"
};
this.disallowedURLs = [
"chrome:",
"javascript:",
"data:",
"file:",
"moz-extension:",
"about:"
];
this.sessions = [];
browser.browserAction.onClicked.addListener(() => this.cleanup());
browser.commands.onCommand.addListener((command) => this.onCommand(command));
}
/*
* Clean up open tabs and save data to local storage
*/
cleanup() {
this.getTabs()
.then(result => this.filterTabs(result))
.then(result => this.loadSavedSessions(result))
.then(result => this.saveSession(result))
.catch(e => console.error(e));
}
/*
* get open tabs in the window that aren't pinned
*/
getTabs() {
return tabs.query({
currentWindow: true,
pinned: false
}).then(openTabs => {
return { openTabs };
});
}
/*
* Filter the open tabs list for unsupported URI schemes
* extract and store information about tab count and URLs
*/
filterTabs(result) {
let openTabs = result.openTabs;
let tabCount = openTabs.length;
// filter out disallowed URLs
openTabs = openTabs.filter(tab =>
!this.disallowedURLs.some(scheme => tab.url.startsWith(scheme))
);
if (openTabs.length === 0) {
result.nothingToClean = true;
return result;
}
result.nothingToClean = false;
result.tabIDs = openTabs.map(tab => tab.id);
result.tabsToSave = openTabs.map(tab => {
return {
url: tab.url,
title: tab.title,
favIconUrl: tab.favIconUrl
}
});
result.allTabsClosing = tabCount === result.tabIDs.length;
return result;//loadSavedSessions();
}
/*
* get sessions from local storage
*/
loadSavedSessions(result) {
return storage.local.get()
.then(storageData => {
if (storageData && storageData.sessions) {
this.sessions = storageData.sessions;
}
return result;
});
}
/*
* Save the session to local storage
*/
saveSession(result) {
if (result.nothingToClean) {
return this.notify(`${this.messages.nothingToClean}${this.messages.pressToOpen}`);
}
this.sessions.unshift({
created: Date.now(),
tabs: result.tabsToSave
});
return this.saveSessionsToStorage(result)
.then((result) => this.createBlankTab(result))
.then((result) => this.closeTabs(result))
.then(() => this.notify(`${this.messages.tabsSaved}${this.messages.pressToOpen}`));
}
/*
* save sessions to local storage
*/
saveSessionsToStorage(result) {
return storage.local.set({ sessions: this.sessions })
.then(() => result);
}
/*
* Create a blank tab, so the window stays open
*/
createBlankTab(result) {
if (result.allTabsClosing) {
return tabs.create({})
.then(() => result);
}
return result;
}
/*
* close a set of tabs
*/
closeTabs(result) {
return tabs.remove(result.tabIDs).then(() => result);
}
/*
* Display a notification
*/
notify(message) {
return notifications.create("TabSweeper-notify", {
message,
type: "basic",
title: "Complete",
iconUrl: browser.extension.getURL(this.urls.iconUrl)
}).then(id => {
return new Promise((resolve, reject) => {
setTimeout(() => {
notifications.clear(id).then(resolve, reject);
});
});
});
}
onCommand(command) {
if (command == "execute-cleanup") {
this.cleanup();
}
}
}
new TabSweeper();