-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpopup.js
More file actions
265 lines (232 loc) · 8.79 KB
/
popup.js
File metadata and controls
265 lines (232 loc) · 8.79 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import { browserAPI } from './shared/browser-api.js';
import { debugLog } from './shared/logger.js';
import { createLocalizedModalHelpers } from './shared/modal.js';
import { initializeThemeWatcher } from './shared/theme.js';
const URL_PARAMS = new URLSearchParams(window.location.search);
async function getActiveTab() {
// Open popup.html?tab=5 to use tab ID 5, etc.
if (URL_PARAMS.has("tab")) {
const tabId = parseInt(URL_PARAMS.get("tab"));
return await browserAPI.tabs.get(tabId);
}
const tabs = await browserAPI.tabs.query({
active: true,
currentWindow: true
});
return tabs[0];
}
// Internationalization helper
function initializeI18n() {
// Get all elements with data-i18n attribute
const elements = document.querySelectorAll('[data-i18n]');
elements.forEach(element => {
const key = element.getAttribute('data-i18n');
const message = browserAPI.i18n.getMessage(key);
if (message) {
// Set the content based on element type
if (element.tagName === 'INPUT' && element.type === 'button') {
element.value = message;
} else if (element.tagName === 'INPUT' && element.placeholder !== undefined) {
element.placeholder = message;
} else if (element.tagName === 'META' && element.name === 'description') {
element.content = message;
} else if (element.tagName === 'TITLE') {
element.textContent = message;
} else {
element.textContent = message;
}
}
});
// Handle data-i18n-title attributes
const elementsWithTitle = document.querySelectorAll('[data-i18n-title]');
elementsWithTitle.forEach(element => {
const key = element.getAttribute('data-i18n-title');
const message = browserAPI.i18n.getMessage(key);
if (message) {
element.title = message;
}
});
}
const { showConfirmModal, showAlertModal } = createLocalizedModalHelpers(
(key, defaultValue) => browserAPI.i18n.getMessage(key) || defaultValue
);
document.addEventListener('DOMContentLoaded', async function () {
// Initialize internationalization first
initializeI18n();
// Initialize theme watcher
initializeThemeWatcher();
const highlightsContainer = document.getElementById('highlights-container');
const noHighlights = document.getElementById('no-highlights');
const clearAllBtn = document.getElementById('clear-all');
const viewAllPagesBtn = document.getElementById('view-all-pages');
const openSettingsBtn = document.getElementById('open-settings');
// Load highlight information from current active tab
async function loadHighlights() {
const tab = await getActiveTab();
const currentUrl = tab.url;
if (!currentUrl) return;
const result = await browserAPI.storage.local.get([currentUrl]);
let highlights = result[currentUrl] || [];
// Since it's a group structure, use the position of the representative span
highlights.sort((a, b) => {
const posA = a.spans && a.spans[0] ? a.spans[0].position : 0;
const posB = b.spans && b.spans[0] ? b.spans[0].position : 0;
return posA - posB;
});
debugLog('Loaded highlights for popup (sorted by position):', highlights);
// Enable/disable clear-all button based on highlight count
clearAllBtn.disabled = highlights.length === 0;
// Display highlight list (group basis)
if (highlights.length > 0) {
noHighlights.style.display = 'none';
highlightsContainer.innerHTML = '';
highlights.forEach(group => {
const highlightItem = document.createElement('div');
highlightItem.className = 'highlight-item';
highlightItem.dataset.groupId = group.groupId;
highlightItem.style.setProperty('--highlight-color', group.color);
// Truncate text if too long
let displayText = group.text;
if (displayText.length > 80) {
displayText = displayText.substring(0, 77) + '...';
}
const textSpan = document.createElement('div');
textSpan.className = 'highlight-text';
textSpan.textContent = displayText;
// Add delete button
const deleteBtn = document.createElement('span');
deleteBtn.className = 'delete-btn';
const removeLabel = browserAPI.i18n.getMessage('removeHighlight');
deleteBtn.title = removeLabel;
deleteBtn.setAttribute('aria-label', removeLabel);
const deleteIcon = document.createElement('span');
deleteIcon.className = 'delete-icon';
deleteIcon.textContent = 'x';
deleteBtn.appendChild(deleteIcon);
deleteBtn.addEventListener('click', async function (e) {
e.stopPropagation();
const confirmMessage =
browserAPI.i18n.getMessage('confirmDeleteHighlight') ||
browserAPI.i18n.getMessage('confirmDeletePage') ||
'Delete this highlight?';
const confirmed = await showConfirmModal(confirmMessage);
if (confirmed) {
await deleteHighlight(group.groupId, currentUrl);
}
});
highlightItem.appendChild(textSpan);
highlightItem.appendChild(deleteBtn);
highlightsContainer.appendChild(highlightItem);
});
} else {
noHighlights.style.display = 'block';
highlightsContainer.innerHTML = '';
highlightsContainer.appendChild(noHighlights);
}
}
// Delete highlight (group basis)
async function deleteHighlight(groupId, url) {
const response = await browserAPI.runtime.sendMessage({
action: 'deleteHighlight',
url: url,
groupId: groupId, // Delete by groupId
notifyRefresh: true
});
if (response && response.success) {
debugLog('Highlight group deleted through background:', groupId);
await loadHighlights();
}
}
// Delete all highlights
clearAllBtn.addEventListener('click', async function () {
debugLog('Clearing all highlights');
const confirmMessage = browserAPI.i18n.getMessage('confirmClearAll');
const confirmed = await showConfirmModal(confirmMessage);
if (confirmed) {
const tab = await getActiveTab();
const currentUrl = tab.url;
if (!currentUrl) return;
const response = await browserAPI.runtime.sendMessage({
action: 'clearAllHighlights',
url: currentUrl,
notifyRefresh: true
});
if (response && response.success) {
debugLog('All highlights cleared through background');
await loadHighlights();
}
}
});
// View list of highlighted pages
function openPagesList() {
debugLog('Opening all pages list');
const targetUrl = browserAPI.runtime.getURL('pages-list.html');
// browserAPI.windows is not available on Firefox Android
if (browserAPI.windows) {
browserAPI.windows.getAll({populate: true}, function(windows) {
let found = false;
for (const win of windows) {
for (const tab of win.tabs) {
if (tab.url && tab.url.startsWith(targetUrl)) {
browserAPI.windows.update(win.id, {focused: true});
browserAPI.tabs.update(tab.id, {active: true});
browserAPI.tabs.sendMessage(tab.id, {action: 'refreshPagesList'});
found = true;
break;
}
}
if (found) break;
}
if (!found) {
const w = 860, h = 600;
const left = Math.round((window.screen.width - w) / 2);
const top = Math.round((window.screen.height - h) / 2);
browserAPI.windows.create({
url: targetUrl,
type: 'popup',
width: w,
height: h,
left,
top,
});
}
});
} else {
// Mobile fallback: use tabs API only
browserAPI.tabs.query({}, function(tabs) {
const existingTab = tabs.find(tab => tab.url && tab.url.startsWith(targetUrl));
if (existingTab) {
browserAPI.tabs.update(existingTab.id, {active: true});
browserAPI.tabs.sendMessage(existingTab.id, {action: 'refreshPagesList'});
} else {
browserAPI.tabs.create({ url: targetUrl });
}
// Close the popup so the user sees the page directly
window.close();
});
}
}
viewAllPagesBtn.addEventListener('click', openPagesList);
openSettingsBtn.addEventListener('click', () => {
const settingsUrl = browserAPI.runtime.getURL('settings.html');
if (browserAPI.windows) {
const w = 440, h = 620;
const left = Math.round((window.screen.width - w) / 2);
const top = Math.round((window.screen.height - h) / 2);
browserAPI.windows.create({
url: settingsUrl,
type: 'popup',
width: w,
height: h,
left,
top,
});
} else {
// Mobile fallback
browserAPI.tabs.create({ url: settingsUrl });
window.close();
}
});
// Initialization
await loadHighlights();
});