-
Notifications
You must be signed in to change notification settings - Fork 3
/
background.js
138 lines (125 loc) · 3.63 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
// Copyright (C) 2023-2024 Seth Cottle
// This file is part of TabCloser.
// TabCloser 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 any later version.
// TabCloser 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. Please see the
// GNU General Public License for more details.
const debug = false;
const predefinedUrlPatterns = [
{
label: ' Asana',
pattern: '^https?://app\\.asana\\.com/-/desktop_app_link\\?.*',
},
{
label: ' AWS IAM Access Auth Success',
pattern: '^https://[a-z0-9-]+\\.awsapps\\.com/start/user-consent/login-success.html',
},
{
label: ' Discord Invites',
pattern: '^https?://discord\\.com/invite/',
},
{
label: ' Figma Files',
pattern: '^https?://(?:www\.)?figma\.com/file/',
},
{
label: ' Linear',
pattern: '^https?://linear\\.app/.*\\?noRedirect=1$',
},
{
label: ' Microsoft Teams',
pattern: '^https?://teams\\.microsoft\\.com/dl/launcher/.*',
},
{
label: ' Notion',
pattern: '^https?://www\\.notion\\.so/native/.*&deepLinkOpenNewTab=true',
},
{
label: ' Slack',
pattern: '^https?://(?!(app\\.slack\\.com|slack\\.com|api\\.slack\\.com|.*\\/(customize|account|apps)(\\/|$)|.*\\/home(\\/|$)))[a-z0-9-]+\\.slack\\.com/.*$',
},
{
label: ' Spotify',
pattern: '^https?://open\\.spotify\\.com',
},
{
label: ' VS Code Live Share',
pattern: '^https?://vscode\\.dev/liveshare',
},
{
label: ' Webex Joins',
pattern: '^https?://([a-z0-9-]+\\.)?webex\\.com/wbxmjs/joinservice',
},
{
label: ' Zoom Joins',
pattern: '^https?://([a-z0-9-]+\\.)?zoom\\.us/j/[^/]+#success$',
},
];
function shouldCloseTab(url) {
return new Promise((resolve) => {
chrome.storage.sync.get(['disabledUrls'], ({ disabledUrls }) => {
if (!disabledUrls) {
disabledUrls = [];
}
const shouldClose = predefinedUrlPatterns.some(({ pattern }) => {
const regex = new RegExp(pattern, 'i');
return regex.test(url) && !disabledUrls.includes(pattern);
});
resolve(shouldClose);
});
});
}
async function checkTab(tab, interval) {
const shouldClose = await shouldCloseTab(tab.url);
if (shouldClose) {
try {
await new Promise((resolve, reject) => {
setTimeout(() => {
chrome.tabs.remove(tab.id, () => {
const err = chrome.runtime.lastError;
if (err) {
reject(err);
} else {
resolve();
}
});
}, interval * 1000);
});
} catch (error) {
console.log(`Error closing tab with id ${tab.id}:`, error.message);
}
}
}
function processTabs(tabs, index, interval) {
if (index >= tabs.length) {
return;
}
const tab = tabs[index];
checkTab(tab, interval);
processTabs(tabs, index + 1, interval);
}
function runTabCloser() {
chrome.storage.sync.get(['interval'], ({ interval }) => {
interval = interval || 15;
if (debug) {
console.log('TabCloser interval:', interval);
}
chrome.tabs.query({}, (tabs) => processTabs(tabs, 0, interval));
});
if (debug) {
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === 'sync') {
for (const key in changes) {
console.log(`Changed ${key}:`, changes[key]);
}
}
});
}
}
chrome.tabs.onCreated.addListener((tab) => {
setTimeout(runTabCloser, 5000);
});
runTabCloser();