-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
184 lines (157 loc) · 6.18 KB
/
popup.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
document.getElementById('groupNow').addEventListener('click', () => {
console.log('🔍 Group Now button clicked, sending message to background');
chrome.runtime.sendMessage({ action: 'groupTabs' }, (response) => {
const resultElement = document.getElementById('groupResult');
if (resultElement) {
if (chrome.runtime.lastError) {
resultElement.textContent = 'Error: ' + chrome.runtime.lastError.message;
resultElement.style.color = 'red';
} else {
resultElement.textContent = 'Grouping process initiated!';
resultElement.style.color = 'green';
}
}
// Update stats after a short delay to reflect changes
setTimeout(updateStats, 1000);
});
});
document.getElementById('openSettings').addEventListener('click', () => {
chrome.runtime.openOptionsPage();
});
// Add diagnostics button
const diagBtn = document.createElement('button');
diagBtn.textContent = 'Run Diagnostics';
diagBtn.className = 'button-diag';
diagBtn.addEventListener('click', runDiagnostics);
// Add to the body instead of looking for .controls which doesn't exist
document.body.appendChild(diagBtn);
// Create result section
const resultSection = document.createElement('div');
resultSection.id = 'groupResult';
resultSection.style.marginTop = '10px';
resultSection.style.fontWeight = 'bold';
document.body.appendChild(resultSection);
function updateStats() {
chrome.tabs.query({currentWindow: true}, (tabs) => {
const totalTabs = tabs.length;
let groupedTabs = 0;
const groups = new Set();
tabs.forEach(tab => {
if (tab.groupId !== chrome.tabs.TAB_ID_NONE) {
groupedTabs++;
groups.add(tab.groupId);
}
});
// Safely update stats elements
const safeUpdateElement = (id, value) => {
const element = document.getElementById(id);
if (element) {
element.textContent = value;
}
};
safeUpdateElement('totalTabs', totalTabs);
safeUpdateElement('groupedTabs', groupedTabs);
safeUpdateElement('groupCount', groups.size);
});
}
// Diagnostic function to test tab grouping functionality
async function runDiagnostics() {
const diagResultElem = document.getElementById('groupResult');
diagResultElem.textContent = 'Running diagnostics...';
diagResultElem.style.color = 'blue';
try {
// 1. Check permissions
const permissions = ['tabs', 'tabGroups'];
const hasPermissions = await chrome.permissions.contains({permissions});
if (!hasPermissions) {
diagResultElem.textContent = 'Error: Required permissions are missing';
diagResultElem.style.color = 'red';
return;
}
// 2. Check settings
chrome.storage.sync.get(null, (settings) => {
console.log('📊 Current settings:', settings);
// 3. Check current tabs
chrome.tabs.query({currentWindow: true}, async (tabs) => {
const groupableTabs = tabs.filter(tab => !tab.pinned && !tab.url.startsWith('chrome:'));
console.log(`📑 Found ${tabs.length} tabs (${groupableTabs.length} groupable)`);
if (groupableTabs.length < 3) {
diagResultElem.textContent = 'Diagnostic: Not enough groupable tabs (need at least 3)';
diagResultElem.style.color = 'orange';
return;
}
// 4. Try a forced simple grouping of 3 tabs
try {
// Only try this if we have enough ungrouped tabs
const ungroupedTabs = groupableTabs.filter(tab => tab.groupId === chrome.tabs.TAB_ID_NONE);
if (ungroupedTabs.length >= 3) {
const testGroupTabs = ungroupedTabs.slice(0, 3).map(tab => tab.id);
diagResultElem.textContent = 'Testing tab grouping API...';
// Create a simple test group
const groupId = await chrome.tabs.group({tabIds: testGroupTabs});
console.log(`✅ Created test group with ID ${groupId}`);
// Name the group
await chrome.tabGroups.update(groupId, {title: 'Test Group'});
diagResultElem.textContent = 'Diagnostics passed! Tab grouping API is working';
diagResultElem.style.color = 'green';
// Clean up by ungrouping
setTimeout(async () => {
await chrome.tabs.ungroup(testGroupTabs);
console.log('🧹 Cleaned up test group');
}, 3000);
} else {
diagResultElem.textContent = 'Diagnostic: All tabs are already grouped!';
diagResultElem.style.color = 'orange';
}
} catch (error) {
console.error('❌ Tab grouping API error:', error);
diagResultElem.textContent = `API Error: ${error.message}`;
diagResultElem.style.color = 'red';
}
});
});
} catch (error) {
console.error('❌ Diagnostic error:', error);
diagResultElem.textContent = `Diagnostic error: ${error.message}`;
diagResultElem.style.color = 'red';
}
}
// Update stats when popup is opened
document.addEventListener('DOMContentLoaded', () => {
// First ensure all required elements are present
const elementsToCheck = ['totalTabs', 'groupedTabs', 'groupCount', 'groupNow', 'openSettings'];
let allElementsPresent = true;
for (const id of elementsToCheck) {
if (!document.getElementById(id)) {
console.error(`Missing required element: ${id}`);
allElementsPresent = false;
}
}
if (allElementsPresent) {
updateStats();
} else {
console.error('Cannot initialize popup due to missing elements');
}
// Add CSS for diagnostic button
const style = document.createElement('style');
style.textContent = `
.button-diag {
background-color: #f0f0f0;
border: 1px solid #999;
color: #333;
padding: 8px 12px;
margin-top: 8px;
border-radius: 4px;
cursor: pointer;
}
.button-diag:hover {
background-color: #e0e0e0;
}
`;
document.head.appendChild(style);
});
// Listen for tab updates and refresh stats
chrome.tabs.onUpdated.addListener(updateStats);
chrome.tabs.onRemoved.addListener(updateStats);
chrome.tabGroups.onCreated.addListener(updateStats);
chrome.tabGroups.onRemoved.addListener(updateStats);