|
63 | 63 | static INTERVAL = 1000 * 60; // 1 minute
|
64 | 64 |
|
65 | 65 | interval = null;
|
| 66 | + /** @type {ZenTabUnloader} */ |
66 | 67 | unloader = null;
|
67 | 68 |
|
68 |
| - #excludedUrls = []; |
69 |
| - #compiledExcludedUrls = []; |
70 |
| - |
71 | 69 | constructor(unloader) {
|
72 | 70 | this.unloader = unloader;
|
73 | 71 | this.interval = setInterval(this.intervalListener.bind(this), ZenTabsIntervalUnloader.INTERVAL);
|
74 |
| - this.#excludedUrls = this.lazyExcludeUrls; |
75 |
| - } |
76 |
| - |
77 |
| - get lazyExcludeUrls() { |
78 |
| - return [ |
79 |
| - ...ZEN_TAB_UNLOADER_DEFAULT_EXCLUDED_URLS, |
80 |
| - ...lazy.zenTabUnloaderExcludedUrls.split(',').map((url) => url.trim()), |
81 |
| - ]; |
82 |
| - } |
83 |
| - |
84 |
| - arraysEqual(a, b) { |
85 |
| - if (a === b) return true; |
86 |
| - if (a == null || b == null) return false; |
87 |
| - if (a.length !== b.length) return false; |
88 |
| - |
89 |
| - // If you don't care about the order of the elements inside |
90 |
| - // the array, you should sort both arrays here. |
91 |
| - // Please note that calling sort on an array will modify that array. |
92 |
| - // you might want to clone your array first. |
93 |
| - |
94 |
| - for (var i = 0; i < a.length; ++i) { |
95 |
| - if (a[i] !== b[i]) return false; |
96 |
| - } |
97 |
| - return true; |
98 |
| - } |
99 |
| - |
100 |
| - get excludedUrls() { |
101 |
| - // Check if excludedrls is the same as the pref value |
102 |
| - const excludedUrls = this.lazyExcludeUrls; |
103 |
| - if (!this.arraysEqual(this.#excludedUrls, excludedUrls) || !this.#compiledExcludedUrls.length) { |
104 |
| - this.#excludedUrls = excludedUrls; |
105 |
| - this.#compiledExcludedUrls = excludedUrls.map((url) => new RegExp(url)); |
106 |
| - } |
107 |
| - return this.#compiledExcludedUrls; |
108 | 72 | }
|
109 | 73 |
|
110 | 74 | intervalListener() {
|
111 | 75 | if (!lazy.zenTabUnloaderEnabled) {
|
112 | 76 | return;
|
113 | 77 | }
|
114 | 78 | const currentTimestamp = Date.now();
|
115 |
| - const excludedUrls = this.excludedUrls; |
116 | 79 | const tabs = ZenWorkspaces.allStoredTabs;
|
117 | 80 | for (let i = 0; i < tabs.length; i++) {
|
118 | 81 | const tab = tabs[i];
|
119 |
| - if (this.unloader.canUnloadTab(tab, currentTimestamp, excludedUrls)) { |
| 82 | + if (this.unloader.canUnloadTab(tab, currentTimestamp)) { |
120 | 83 | this.unloader.unload(tab);
|
121 | 84 | }
|
122 | 85 | }
|
|
126 | 89 | class ZenTabUnloader extends ZenDOMOperatedFeature {
|
127 | 90 | static ACTIVITY_MODIFIERS = ['muted', 'soundplaying', 'label', 'attention'];
|
128 | 91 |
|
| 92 | + #excludedUrls = []; |
| 93 | + #compiledExcludedUrls = []; |
| 94 | + #lastCheckedUrlTimestamp = 0; |
| 95 | + |
129 | 96 | constructor() {
|
130 | 97 | super();
|
| 98 | + |
| 99 | + this.#excludedUrls = this.lazyExcludeUrls; |
131 | 100 | if (!lazy.zenTabUnloaderEnabled) {
|
132 | 101 | return;
|
133 | 102 | }
|
|
217 | 186 | document.getElementById('context_closeDuplicateTabs').parentNode.appendChild(element);
|
218 | 187 | }
|
219 | 188 |
|
| 189 | + get lazyExcludeUrls() { |
| 190 | + return [ |
| 191 | + ...ZEN_TAB_UNLOADER_DEFAULT_EXCLUDED_URLS, |
| 192 | + ...lazy.zenTabUnloaderExcludedUrls.split(',').map((url) => url.trim()), |
| 193 | + ]; |
| 194 | + } |
| 195 | + |
| 196 | + arraysEqual(a, b) { |
| 197 | + if (a === b) return true; |
| 198 | + if (a == null || b == null) return false; |
| 199 | + if (a.length !== b.length) return false; |
| 200 | + |
| 201 | + const currentTimestamp = Date.now(); |
| 202 | + if (currentTimestamp - this.#lastCheckedUrlTimestamp < 5 * 1000) { |
| 203 | + return true; |
| 204 | + } |
| 205 | + |
| 206 | + this.#lastCheckedUrlTimestamp = currentTimestamp; |
| 207 | + // If you don't care about the order of the elements inside |
| 208 | + // the array, you should sort both arrays here. |
| 209 | + // Please note that calling sort on an array will modify that array. |
| 210 | + // you might want to clone your array first. |
| 211 | + |
| 212 | + for (const i = 0; i < a.length; ++i) { |
| 213 | + if (a[i] !== b[i]) return false; |
| 214 | + } |
| 215 | + return true; |
| 216 | + } |
| 217 | + |
| 218 | + get excludedUrls() { |
| 219 | + // Check if excludedrls is the same as the pref value |
| 220 | + const excludedUrls = this.lazyExcludeUrls; |
| 221 | + if (!this.arraysEqual(this.#excludedUrls, excludedUrls) || !this.#compiledExcludedUrls.length) { |
| 222 | + this.#excludedUrls = excludedUrls; |
| 223 | + this.#compiledExcludedUrls = excludedUrls.map((url) => new RegExp(url)); |
| 224 | + } |
| 225 | + return this.#compiledExcludedUrls; |
| 226 | + } |
| 227 | + |
220 | 228 | unload(tab, skipPermitUnload = false) {
|
221 | 229 | gBrowser.explicitUnloadTabs([tab], skipPermitUnload);
|
222 | 230 | tab.removeAttribute('linkedpanel');
|
|
229 | 237 |
|
230 | 238 | explicitUnloadTabs(tabs, extraArgs = {}) {
|
231 | 239 | for (let i = 0; i < tabs.length; i++) {
|
232 |
| - if (this.canUnloadTab(tabs[i], Date.now(), this.intervalUnloader.excludedUrls, true, extraArgs)) { |
| 240 | + if (this.canUnloadTab(tabs[i], Date.now(), true, extraArgs)) { |
233 | 241 | this.unload(tabs[i], true);
|
234 | 242 | }
|
235 | 243 | }
|
|
251 | 259 | }
|
252 | 260 | }
|
253 | 261 |
|
254 |
| - canUnloadTab(tab, currentTimestamp, excludedUrls, ignoreTimestamp = false, extraArgs = {}) { |
| 262 | + canUnloadTab(tab, currentTimestamp, ignoreTimestamp = false, extraArgs = {}) { |
255 | 263 | if (
|
256 | 264 | (tab.pinned && !ignoreTimestamp) ||
|
257 | 265 | tab.selected ||
|
|
266 | 274 | (tab.pictureinpicture && !ignoreTimestamp) ||
|
267 | 275 | (tab.soundPlaying && !ignoreTimestamp) ||
|
268 | 276 | (tab.zenIgnoreUnload && !ignoreTimestamp) ||
|
269 |
| - (excludedUrls.some((url) => url.test(tab.linkedBrowser?.currentURI.spec)) && |
| 277 | + (this.excludedUrls.some((url) => url.test(tab.linkedBrowser?.currentURI.spec)) && |
270 | 278 | tab.linkedBrowser?.currentURI.spec !== 'about:blank')
|
271 | 279 | ) {
|
272 | 280 | return false;
|
|
0 commit comments