|
6 | 6 | const manifest = browser.runtime.getManifest(); |
7 | 7 | const extname = manifest.name; |
8 | 8 |
|
9 | | - let delayTimerId = null; |
10 | | - let isActive = true; |
11 | | - let regexList = null; |
12 | | - let includeAllWindows = false; |
| 9 | + let settings = { |
| 10 | + delayTimerId: null, |
| 11 | + isActive: true, |
| 12 | + ignoredREMatchers: [], |
| 13 | + ignoreWindowScope: false, |
| 14 | + ignoreContainerScope: false, |
| 15 | + ignoreGroupScope: false, |
| 16 | + ignoreURLPath: false, |
| 17 | + ignoreURLParams: false, |
| 18 | + }; |
13 | 19 |
|
14 | 20 | function serializeSearchParams(searchParams) { |
15 | 21 | // Sort the key/value pairs |
|
22 | 28 | return out; |
23 | 29 | } |
24 | 30 |
|
25 | | - async function buildRegExList() { |
| 31 | + async function storageString2REArray(storageId) { |
26 | 32 | const out = []; |
27 | | - (await storage.get("string", "matchers", "")) |
28 | | - .split("\n") |
29 | | - .forEach((line) => { |
30 | | - line = line.trim(); |
31 | | - if (line !== "") { |
32 | | - try { |
33 | | - out.push(new RegExp(line)); |
34 | | - } catch (e) { |
35 | | - console.error(e); |
36 | | - } |
| 33 | + (await storage.get("string", storageId, "")).split("\n").forEach((line) => { |
| 34 | + line = line.trim(); |
| 35 | + if (line !== "") { |
| 36 | + try { |
| 37 | + out.push(new RegExp(line)); |
| 38 | + } catch (e) { |
| 39 | + console.error(e); |
37 | 40 | } |
38 | | - }); |
| 41 | + } |
| 42 | + }); |
39 | 43 | return out; |
40 | 44 | } |
41 | 45 |
|
42 | | - function isOnRegexList(url) { |
| 46 | + function containsMatch(regexList, str) { |
43 | 47 | for (let i = 0; i < regexList.length; i++) { |
44 | | - if (regexList[i].test(url)) { |
| 48 | + if (regexList[i].test(str)) { |
45 | 49 | return true; |
46 | 50 | } |
47 | 51 | } |
48 | 52 | return false; |
49 | 53 | } |
50 | 54 |
|
| 55 | + async function syncSetting(type, id, fallback) { |
| 56 | + settings[id] = await storage.get(type, id, fallback); |
| 57 | + } |
| 58 | + |
51 | 59 | async function onStorageChanged() { |
52 | | - regexList = await buildRegExList(); |
53 | | - includeAllWindows = await storage.get( |
54 | | - "boolean", |
55 | | - "includeAllWindows", |
56 | | - false, |
| 60 | + settings.ignoredREMatchers = await storageString2REArray( |
| 61 | + "ignoredREMatchersString", |
57 | 62 | ); |
58 | | - console.debug(includeAllWindows); |
| 63 | + await syncSetting("boolean", "ignoreWindowScope", false); |
| 64 | + await syncSetting("boolean", "ignoreContainerScope", false); |
| 65 | + await syncSetting("boolean", "ignoreGroupScope", false); |
| 66 | + await syncSetting("boolean", "ignoreURLPath", false); |
| 67 | + await syncSetting("boolean", "ignoreURLParams", false); |
| 68 | + delayed_delDups(); |
59 | 69 | } |
60 | 70 |
|
61 | 71 | async function delayed_delDups() { |
62 | | - clearTimeout(delayTimerId); |
63 | | - delayTimerId = setTimeout(delDups, 2500); // 2.5 seconds w/o tab status changes |
| 72 | + clearTimeout(settings.delayTimerId); |
| 73 | + settings.delayTimerId = setTimeout(delDups, 3500); // 3.5 seconds w/o tab status changes |
64 | 74 | } |
65 | 75 |
|
66 | 76 | async function delDups() { |
67 | | - if (!isActive) { |
| 77 | + if (!settings.isActive) { |
68 | 78 | return; |
69 | 79 | } |
70 | 80 |
|
71 | 81 | let qryobj = { |
72 | 82 | pinned: false, |
73 | 83 | }; |
74 | 84 |
|
75 | | - if (!includeAllWindows) { |
76 | | - qryobj["currentWindow"] = true; |
77 | | - } |
78 | | - |
79 | 85 | const allTabs = await browser.tabs.query(qryobj); |
80 | | - const last_focused_window = await browser.windows.getLastFocused({ |
81 | | - populate: false, |
82 | | - }); |
83 | 86 |
|
84 | 87 | // check if any tab is still loading , if so we wait |
85 | 88 | if (allTabs.some((t) => t.status !== "complete")) { |
|
94 | 97 | const dup_groups = new Map(); |
95 | 98 |
|
96 | 99 | for (const t of allTabs) { |
97 | | - if (!isOnRegexList(t.url)) { |
| 100 | + if (!containsMatch(settings.ignoredREMatchers, t.url)) { |
98 | 101 | const urlobj = new URL(t.url); |
99 | | - tab_url = |
100 | | - urlobj.origin + |
101 | | - urlobj.pathname + |
102 | | - serializeSearchParams(urlobj.searchParams); |
| 102 | + //if (!settings.ignoredHostnames.includes(urlobj.hostname)) { |
| 103 | + let key = urlobj.origin; |
103 | 104 |
|
104 | | - const key = t.cookieStoreId + "_" + tab_url; |
| 105 | + // ----- |
| 106 | + |
| 107 | + if (!settings.ignoreURLPath) { |
| 108 | + key = key + "_" + urlobj.pathname; |
| 109 | + } |
| 110 | + if (!settings.ignoreURLParams) { |
| 111 | + key = key + "_" + serializeSearchParams(urlobj.searchParams); |
| 112 | + } |
| 113 | + if (!settings.ignoreContainerScope) { |
| 114 | + key = t.cookieStoreId + "_" + key; |
| 115 | + } |
| 116 | + if (!settings.ignoreWindowScope) { |
| 117 | + key = t.windowId + "_" + key; |
| 118 | + } |
| 119 | + if (!settings.ignoreGroupScope) { |
| 120 | + if (browser.tabGroups) { |
| 121 | + if (t.groupId) { |
| 122 | + key = t.groupId + "_" + key; |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + // ----- |
105 | 128 |
|
106 | 129 | if (!dup_groups.has(key)) { |
107 | 130 | dup_groups.set(key, []); |
|
137 | 160 | } else { |
138 | 161 | // in this case BOTH could be active/focused in the window |
139 | 162 | if (focus_groups.includes(k)) { |
140 | | - if (a.active && b.active) { |
141 | | - // closeing neither would be a way to go or close the one in the inactive Window |
142 | | - // But even if the window is inactive the user could have them open for comparision ... |
143 | | - // this is kind of a not so straight forward decision |
144 | | - // i mean the user could still pin the tabs that is a workaround at least |
145 | | - |
146 | | - // lets prefer the active one in the last focused window |
147 | | - if (a.windowId === last_focused_window.id) { |
| 163 | + if (!(a.active && b.active)) { |
| 164 | + // prefer active |
| 165 | + if (a.active) { |
148 | 166 | return -1; |
149 | 167 | } |
150 | | - if (b.windowId === last_focused_window.id) { |
| 168 | + if (b.active) { |
151 | 169 | return 1; |
152 | 170 | } |
153 | | - // if we get here, both tabs are in unfocused windows (cant have 2 focused windows AFAIK) ... what todo now? |
154 | | - // lets just fall back to lastAccessed |
155 | | - return b.lastAccessed - a.lastAccessed; |
156 | | - } |
157 | | - |
158 | | - // prefer active |
159 | | - if (a.active) { |
160 | | - return -1; |
161 | | - } |
162 | | - if (b.active) { |
163 | | - return 1; |
164 | 171 | } |
165 | 172 | } |
166 | | - //return b.lastAccessed - a.lastAccessed; |
167 | 173 | } |
168 | | - // and last fallback |
| 174 | + // final fallback |
169 | 175 | return b.lastAccessed - a.lastAccessed; |
170 | 176 | }); |
171 | 177 |
|
|
180 | 186 | await browser.tabs.remove(tid); |
181 | 187 | } catch (e) {} |
182 | 188 | } |
183 | | - delayTimerId = null; |
| 189 | + settings.delayTimerId = null; |
184 | 190 | } |
185 | 191 |
|
186 | 192 | async function onBAClicked() { |
187 | | - clearTimeout(delayTimerId); |
188 | | - isActive = !isActive; |
189 | | - storage.set("isActive", isActive); |
190 | | - if (isActive) { |
| 193 | + clearTimeout(settings.delayTimerId); |
| 194 | + settings.isActive = !settings.isActive; |
| 195 | + storage.set("isActive", settings.isActive); |
| 196 | + if (settings.isActive) { |
191 | 197 | browser.browserAction.setBadgeBackgroundColor({ color: "green" }); |
192 | 198 | browser.browserAction.setBadgeText({ text: "on" }); |
193 | 199 | delayed_delDups(); |
|
199 | 205 |
|
200 | 206 | // setup |
201 | 207 | await onStorageChanged(); |
202 | | - isActive = await storage.get("boolean", "isActive", isActive); |
203 | | - storage.set("isActive", isActive); |
204 | | - |
205 | | - if (isActive) { |
| 208 | + settings.isActive = await storage.get( |
| 209 | + "boolean", |
| 210 | + "isActive", |
| 211 | + settings.isActive, |
| 212 | + ); |
| 213 | + storage.set("isActive", settings.isActive); |
| 214 | + |
| 215 | + if (settings.isActive) { |
206 | 216 | browser.browserAction.setBadgeBackgroundColor({ color: "green" }); |
207 | 217 | browser.browserAction.setBadgeText({ text: "on" }); |
208 | 218 | } else { |
|
217 | 227 | }); |
218 | 228 | browser.tabs.onCreated.addListener(delayed_delDups); |
219 | 229 | browser.storage.onChanged.addListener(onStorageChanged); |
| 230 | + // tab moving might trigger group changes |
| 231 | + browser.tabs.onMoved.addListener(() => { |
| 232 | + if (!settings.ignoreGroupScope) { |
| 233 | + if (browser.tabGroups) { |
| 234 | + delayed_delDups(); |
| 235 | + } |
| 236 | + } |
| 237 | + }); |
220 | 238 | })(); |
0 commit comments