-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
213 lines (177 loc) · 6.27 KB
/
popup.js
File metadata and controls
213 lines (177 loc) · 6.27 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
document.addEventListener('DOMContentLoaded', async () => {
// Get DOM elements
const tabNameInput = document.getElementById('tabName');
const renameBtn = document.getElementById('renameBtn');
const persistCheck = document.getElementById('persistCheck');
const recentList = document.getElementById('recentList');
const savedList = document.getElementById('savedList');
let currentTab = null;
let recentNames = [];
let savedUrls = {};
// Get current tab info
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
currentTab = tabs[0];
// Populate input with current tab title
tabNameInput.value = currentTab.title;
tabNameInput.select();
// Load saved data from storage
const data = await chrome.storage.local.get(['recentNames', 'savedUrls']);
recentNames = data.recentNames || [];
savedUrls = data.savedUrls || {};
// Check if current URL has a saved name
let urlKey = '';
try {
urlKey = new URL(currentTab.url).hostname;
} catch {
// Invalid URL (e.g., chrome:// pages) - urlKey remains empty
}
if (savedUrls[urlKey]) {
persistCheck.checked = true;
}
// Update UI
updateRecentList();
updateSavedList();
// Event listeners
renameBtn.addEventListener('click', renameTab);
tabNameInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') renameTab();
});
// Function to rename the tab
async function renameTab() {
const newName = tabNameInput.value.trim();
if (!newName) return;
// Save to recent names (avoid duplicates)
if (!recentNames.includes(newName)) {
recentNames.unshift(newName);
if (recentNames.length > 5) recentNames.pop();
await chrome.storage.local.set({ recentNames });
updateRecentList();
}
// Save to persistent URLs if checked (only if urlKey is valid)
if (persistCheck.checked && urlKey) {
savedUrls[urlKey] = newName;
await chrome.storage.local.set({ savedUrls });
updateSavedList();
} else if (urlKey && savedUrls[urlKey]) {
delete savedUrls[urlKey];
await chrome.storage.local.set({ savedUrls });
updateSavedList();
}
chrome.runtime.sendMessage({
action: 'setTabName',
tabId: currentTab.id,
name: newName
}).catch(() => {});
chrome.tabs.sendMessage(currentTab.id, {
action: 'renameTab',
name: newName
}).catch(() => {});
}
// Clear all recent names
async function clearRecentNames() {
recentNames = [];
await chrome.storage.local.set({ recentNames });
updateRecentList();
}
// Delete a specific recent name by index
async function deleteRecentName(index) {
recentNames.splice(index, 1);
await chrome.storage.local.set({ recentNames });
updateRecentList();
}
// Update recent names list
function updateRecentList() {
recentList.innerHTML = '';
// Add clear button header if there are items
if (recentNames.length > 0) {
const headerDiv = document.createElement('div');
headerDiv.className = 'list-header';
const headerTitle = document.createElement('span');
headerTitle.textContent = 'Recent Names';
headerTitle.className = 'header-title';
const clearBtn = document.createElement('button');
clearBtn.textContent = 'Clear All';
clearBtn.className = 'clear-btn';
clearBtn.addEventListener('click', clearRecentNames);
headerDiv.appendChild(headerTitle);
headerDiv.appendChild(clearBtn);
recentList.appendChild(headerDiv);
}
recentNames.forEach((name, index) => {
const li = document.createElement('li');
const nameSpan = document.createElement('span');
nameSpan.textContent = name;
nameSpan.className = 'item-text';
nameSpan.addEventListener('click', () => {
tabNameInput.value = name;
});
const buttonsDiv = document.createElement('div');
buttonsDiv.className = 'item-buttons';
const useBtn = document.createElement('button');
useBtn.textContent = 'Use';
useBtn.className = 'action-btn';
useBtn.addEventListener('click', () => {
tabNameInput.value = name;
renameTab();
});
const deleteBtn = document.createElement('button');
deleteBtn.textContent = '×';
deleteBtn.title = 'Delete';
deleteBtn.className = 'delete-btn';
deleteBtn.addEventListener('click', (e) => {
e.stopPropagation();
deleteRecentName(index);
});
buttonsDiv.appendChild(useBtn);
buttonsDiv.appendChild(deleteBtn);
li.appendChild(nameSpan);
li.appendChild(buttonsDiv);
recentList.appendChild(li);
});
if (recentNames.length === 0) {
const li = document.createElement('li');
li.textContent = 'No recent names';
li.style.color = '#999';
recentList.appendChild(li);
}
}
// Update saved URLs list
function updateSavedList() {
savedList.innerHTML = '';
const urlEntries = Object.entries(savedUrls);
if (urlEntries.length === 0) {
const li = document.createElement('li');
li.textContent = 'No saved URLs';
li.style.color = '#999';
savedList.appendChild(li);
return;
}
urlEntries.forEach(([url, name]) => {
const li = document.createElement('li');
const urlDiv = document.createElement('div');
urlDiv.className = 'item-text';
const urlSpan = document.createElement('span');
urlSpan.textContent = url;
urlSpan.title = url;
const nameSpan = document.createElement('span');
nameSpan.textContent = ` → ${name}`;
nameSpan.style.color = '#666';
urlDiv.appendChild(urlSpan);
urlDiv.appendChild(nameSpan);
const deleteBtn = document.createElement('button');
deleteBtn.textContent = 'Delete';
deleteBtn.className = 'action-btn';
deleteBtn.addEventListener('click', async () => {
delete savedUrls[url];
await chrome.storage.local.set({ savedUrls });
updateSavedList();
if (url === urlKey) {
persistCheck.checked = false;
}
});
li.appendChild(urlDiv);
li.appendChild(deleteBtn);
savedList.appendChild(li);
});
}
});