-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
220 lines (191 loc) · 5.92 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
218
219
220
const DO_NOTHING = {
title: "Do nothing",
id: "do_nothing"
}
const MERGE_AND_SORT_ACTION = {
title: "Merge windows and sort tabs",
id: "merge_and_sort"
}
const MERGE_ACTION = {
title: "Merge windows",
id: "merge"
}
const SORT_ACTION = {
title: "Sort tabs",
id: "sort"
}
const CLOSE_TABS_FROM_THIS_DOMAIN_ACTION = {
title: "Close tabs from this domain",
id: "close_tabs_from_this_domain"
}
const MOVE_TABS_FROM_THIS_DOMAIN_ACTION = {
title: "Move tabs from this domain to this window",
id: "move_tabs_from_this_domain"
}
const GROUP_TABS_FROM_THIS_DOMAON_ACTION = {
title: "Group tabs from this domain",
id: "group_tabs_from_this_domain"
}
const ALL_ACTIONS = [DO_NOTHING, MERGE_AND_SORT_ACTION, MERGE_ACTION, SORT_ACTION, CLOSE_TABS_FROM_THIS_DOMAIN_ACTION, MOVE_TABS_FROM_THIS_DOMAIN_ACTION, GROUP_TABS_FROM_THIS_DOMAON_ACTION]
async function getOptions() {
return await chrome.storage.sync.get({
defaultAction: MERGE_AND_SORT_ACTION.id,
ignorePinnedTabs: true,
ignorePopupWindows: true,
ignoreAppWindows: true,
showDefaultActionPopup: true,
})
}
async function getTabsFromDomain(url) {
let options = await getOptions()
let tabs = await chrome.tabs.query({
groupId: chrome.tabGroups.TAB_GROUP_ID_NONE,
url: url.protocol + "//" + url.host + "/*",
pinned: options.ignorePinnedTabs ? false : undefined,
})
return tabs
}
function baseAction(actionId) {
if (actionId == DO_NOTHING.id) {
//DO NOTHING
} else if (actionId == MERGE_AND_SORT_ACTION.id) {
mergeWindowsAndSortTabsAction()
} else if (actionId == MERGE_ACTION.id) {
mergeWindowsAction()
} else if (actionId == SORT_ACTION.id) {
sortTabsAction()
} else if (actionId == CLOSE_TABS_FROM_THIS_DOMAIN_ACTION.id) {
closeTabsFromCurrentDomainAction()
} else if (actionId == MOVE_TABS_FROM_THIS_DOMAIN_ACTION.id) {
moveTabsFromCurrentDomainAction()
} else if (actionId == GROUP_TABS_FROM_THIS_DOMAON_ACTION.id) {
groupTabsFromCurrentDomainAction()
}
}
async function mergeWindowsAndSortTabsAction() {
await mergeWindows()
await sortTabs()
}
async function mergeWindowsAction() {
await mergeWindows()
}
async function sortTabsAction() {
await sortTabs()
}
async function closeTabsFromCurrentDomainAction() {
let selectedTab = (await chrome.tabs.query({ active: true, currentWindow: true }))[0]
let url = new URL(selectedTab.url)
let tabs = await getTabsFromDomain(url)
for (let tab of tabs) {
await chrome.tabs.remove(tab.id)
}
}
async function moveTabsFromCurrentDomainAction() {
let selectedTab = (await chrome.tabs.query({ active: true, currentWindow: true }))[0]
let url = new URL(selectedTab.url)
let tabs = await getTabsFromDomain(url)
for (let tab of tabs) {
await chrome.tabs.move(tab.id, { windowId: selectedTab.windowId, index: -1 })
if (tab.pinned == true) {
await chrome.tabs.update(tab.id, { pinned: true })
}
}
}
async function groupTabsFromCurrentDomainAction() {
let selectedTab = (await chrome.tabs.query({ active: true, currentWindow: true }))[0]
let url = new URL(selectedTab.url)
let tabs = await getTabsFromDomain(url)
let tabIds = tabs.map(tab => tab.id)
let existingGroups = await chrome.tabGroups.query({ title: url.host })
if (existingGroups.length > 0) {
let existingGroupId = existingGroups[0].id
await chrome.tabs.group({ groupId: existingGroupId, tabIds: tabIds })
} else {
let groupId = await chrome.tabs.group({ tabIds: tabIds })
await chrome.tabGroups.update(groupId, { title: url.host })
}
}
async function mergeWindows() {
let options = await getOptions()
let currentWindow = await chrome.windows.getCurrent()
let windows = await chrome.windows.getAll({ populate: true })
for (let window of windows) {
if (window.id === currentWindow.id) {
continue;
}
if (options.ignoreAppWindows && window.type === "app") {
continue;
}
if (options.ignorePopupWindows && window.type === "popup") {
continue;
}
for (let tab of window.tabs) {
if (options.ignorePinnedTabs && tab.pinned) {
continue
}
if (tab.groupId !== chrome.tabGroups.TAB_GROUP_ID_NONE) {
await chrome.tabGroups.move(tab.groupId, { windowId: currentWindow.id, index: -1 })
} else {
await chrome.tabs.move(tab.id, { windowId: currentWindow.id, index: -1 })
}
if (tab.pinned == true) {
await chrome.tabs.update(tab.id, { pinned: true })
}
}
}
}
async function sortTabs() {
let options = await getOptions()
let currentWindow = await chrome.windows.getCurrent({ populate: true })
let tabs = currentWindow.tabs
if (options.ignorePinnedTabs) {
tabs = tabs.filter(tab => !tab.pinned)
}
tabs.sort(function (a, b) {
if (a.groupId < b.groupId) {
return -1
} else if (a.groupId > b.groupId) {
return 1
} else {
if (a.url < b.url) {
return -1
} else if (a.url > b.url) {
return 1
} else {
return 0
}
}
})
for (let tab of tabs) {
if (tab.groupId !== chrome.tabGroups.TAB_GROUP_ID_NONE) {
await chrome.tabGroups.move(tab.groupId, { windowId: currentWindow.id, index: -1 })
} else {
await chrome.tabs.move(tab.id, { windowId: currentWindow.id, index: -1 })
}
if (tab.pinned == true) {
await chrome.tabs.update(tab.id, { pinned: true })
}
}
}
chrome.contextMenus.onClicked.addListener(event => {
baseAction(event.menuItemId)
});
chrome.action.onClicked.addListener(async event => {
let options = await getOptions()
baseAction(options.defaultAction)
})
chrome.runtime.onInstalled.addListener(() => {
for (action of ALL_ACTIONS) {
if (action === DO_NOTHING) {
continue
}
chrome.contextMenus.create({
"title": action.title,
"id": action.id,
contexts: ["action"],
});
}
getOptions().then(options => {
chrome.action.setPopup({ popup: options.showDefaultActionPopup ? "popup.html" : "" })
})
})