forked from Darsh-A/Ai-TabGroups-ZenBrowser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclear.uc.js
More file actions
222 lines (207 loc) · 9.35 KB
/
clear.uc.js
File metadata and controls
222 lines (207 loc) · 9.35 KB
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
221
222
(() => {
const CONFIG = {
styles: `
.vertical-pinned-tabs-container-separator,
.zen-workspace-tabs-section[hide-separator] .vertical-pinned-tabs-container-separator {
display: flex !important;
flex-direction: column;
margin-left: 0 !important;
min-height: 1px !important;
background-color: var(--lwt-toolbarbutton-border-color, rgba(200, 200, 200, 0.1));
transition: width 0.1s ease-in-out, margin-right 0.1s ease-in-out, background-color 0.3s ease-out;
margin-top: 5px !important;
margin-bottom: 8px !important;
}
#clear-button {
opacity: 0;
transition: opacity 0.1s ease-in-out;
position: absolute;
right: 0;
font-size: 12px;
width: 60px;
pointer-events: auto;
align-self: end;
appearance: none;
margin-top: -8px;
padding: 1px;
color: grey;
label { display: block; }
}
#clear-button:hover {
opacity: 1;
color: white;
border-radius: 4px;
}
.vertical-pinned-tabs-container-separator:has(#clear-button):hover,
.zen-workspace-tabs-section[hide-separator] .vertical-pinned-tabs-container-separator:has(#clear-button):hover {
width: calc(100% - 60px);
margin-right: auto;
background-color: var(--lwt-toolbarbutton-hover-background, rgba(200, 200, 200, 0.2));
}
.vertical-pinned-tabs-container-separator:hover #clear-button,
.zen-workspace-tabs-section[hide-separator] .vertical-pinned-tabs-container-separator:hover #clear-button {
opacity: 1;
}
.tab-closing {
animation: fadeUp 0.5s forwards;
}
@keyframes fadeUp {
0% { opacity: 1; transform: translateY(0); }
100% { opacity: 0; transform: translateY(-20px); max-height: 0px; padding: 0; margin: 0; border: 0; }
}
.tabbrowser-tab {
transition: transform 0.3s ease-out, opacity 0.3s ease-out, max-height 0.5s ease-out, margin 0.5s ease-out, padding 0.5s ease-out;
}
`
};
let commandListenerAdded = false;
const injectStyles = () => {
let styleElement = document.getElementById('tab-clear-styles');
if (!styleElement) {
styleElement = Object.assign(document.createElement('style'), {
id: 'tab-clear-styles',
textContent: CONFIG.styles
});
document.head.appendChild(styleElement);
} else if (styleElement.textContent !== CONFIG.styles) {
styleElement.textContent = CONFIG.styles;
}
};
const clearTabs = () => {
try {
const currentWorkspaceId = window.gZenWorkspaces?.activeWorkspace;
if (!currentWorkspaceId) return;
const groupSelector = `tab-group:has(tab[zen-workspace-id="${currentWorkspaceId}"])`;
const tabsToClose = [];
for (const tab of gBrowser.tabs) {
const isSameWorkSpace = tab.getAttribute('zen-workspace-id') === currentWorkspaceId;
const groupParent = tab.closest('tab-group');
const isInGroupInCorrectWorkspace = groupParent ? groupParent.matches(groupSelector) : false;
const isEmptyZenTab = tab.hasAttribute("zen-empty-tab");
if (isSameWorkSpace && !tab.selected && !tab.pinned && !isInGroupInCorrectWorkspace && !isEmptyZenTab && tab.isConnected) {
tabsToClose.push(tab);
}
}
if (tabsToClose.length === 0) return;
tabsToClose.forEach(tab => {
tab.classList.add('tab-closing');
setTimeout(() => {
if (tab && tab.isConnected) {
try {
gBrowser.removeTab(tab, { animate: false, skipSessionStore: false, closeWindowWithLastTab: false });
} catch (removeError) {
if (tab && tab.isConnected) {
tab.classList.remove('tab-closing');
}
}
}
}, 500);
});
} catch (error) {
console.log(error);
}
};
function ensureButtonsExist(container) {
if (!container || !container.isConnected) return;
if (!container.querySelector('#clear-button')) {
try {
const buttonFragment = window.MozXULElement.parseXULToFragment(
`<toolbarbutton id="clear-button" command="cmd_zenClearTabs" label="↓ Clear" tooltiptext="Close ungrouped, non-pinned tabs"/>`
);
if (container.isConnected) {
container.appendChild(buttonFragment.firstChild.cloneNode(true));
}
} catch (e) { console.log(e); }
}
}
function addButtonsToAllSeparators() {
const separators = document.querySelectorAll(".vertical-pinned-tabs-container-separator");
if (separators.length > 0) {
separators.forEach(ensureButtonsExist);
} else {
const periphery = document.querySelector('#tabbrowser-arrowscrollbox-periphery');
if (periphery && !periphery.querySelector('#clear-button')) {
ensureButtonsExist(periphery);
}
}
}
function setupCommandsAndListener() {
const zenCommands = document.querySelector("commandset#zenCommandSet");
if (!zenCommands) return;
if (!zenCommands.querySelector("#cmd_zenClearTabs")) {
try {
const cmd = window.MozXULElement.parseXULToFragment(`<command id="cmd_zenClearTabs"/>`).firstChild;
zenCommands.appendChild(cmd);
} catch (e) { console.log(e); }
}
if (!commandListenerAdded) {
try {
zenCommands.addEventListener('command', (event) => {
if (event.target.id === "cmd_zenClearTabs") {
clearTabs();
}
});
commandListenerAdded = true;
} catch (e) { console.log(e); }
}
}
function setupZenWorkspaceHooks() {
if (typeof gZenWorkspaces === 'undefined' || typeof gZenWorkspaces.clearButtonHooksApplied !== 'undefined') {
return;
}
gZenWorkspaces.originalClearButtonHooks = {
onTabBrowserInserted: gZenWorkspaces.onTabBrowserInserted,
updateTabsContainers: gZenWorkspaces.updateTabsContainers,
};
gZenWorkspaces.clearButtonHooksApplied = true;
gZenWorkspaces.onTabBrowserInserted = function(event) {
if (typeof gZenWorkspaces.originalClearButtonHooks.onTabBrowserInserted === 'function') {
try { gZenWorkspaces.originalClearButtonHooks.onTabBrowserInserted.call(gZenWorkspaces, event); } catch (e) { /* Error handling removed */ }
}
setTimeout(addButtonsToAllSeparators, 150);
};
gZenWorkspaces.updateTabsContainers = function(...args) {
if (typeof gZenWorkspaces.originalClearButtonHooks.updateTabsContainers === 'function') {
try { gZenWorkspaces.originalClearButtonHooks.updateTabsContainers.apply(gZenWorkspaces, args); } catch (e) { /* Error handling removed */ }
}
setTimeout(addButtonsToAllSeparators, 150);
};
}
function initializeScript() {
let checkCount = 0;
const maxChecks = 30;
const checkInterval = 1000;
const initCheckInterval = setInterval(() => {
checkCount++;
const separatorExists = !!document.querySelector(".vertical-pinned-tabs-container-separator");
const peripheryExists = !!document.querySelector('#tabbrowser-arrowscrollbox-periphery');
const commandSetExists = !!document.querySelector("commandset#zenCommandSet");
const gBrowserReady = typeof gBrowser !== 'undefined' && gBrowser.tabContainer;
const gZenWorkspacesReady = typeof gZenWorkspaces !== 'undefined' && typeof gZenWorkspaces.activeWorkspace !== 'undefined';
const ready = gBrowserReady && commandSetExists && (separatorExists || peripheryExists) && gZenWorkspacesReady;
if (ready) {
clearInterval(initCheckInterval);
const finalSetup = () => {
try {
injectStyles();
setupCommandsAndListener();
addButtonsToAllSeparators();
setupZenWorkspaceHooks();
} catch (e) { console.log(e); }
};
if ('requestIdleCallback' in window) {
requestIdleCallback(finalSetup, { timeout: 2000 });
} else {
setTimeout(finalSetup, 500);
}
} else if (checkCount > maxChecks) {
clearInterval(initCheckInterval);
}
}, checkInterval);
}
if (document.readyState === "complete" || document.readyState === "interactive") {
initializeScript();
} else {
window.addEventListener("load", initializeScript, { once: true });
}
})();