-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
227 lines (186 loc) · 6.59 KB
/
popup.js
File metadata and controls
227 lines (186 loc) · 6.59 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
/**
* Dev Feedback Capture - Popup Script
*/
(function() {
'use strict';
const {
SHORTCUT_LABEL,
MAC_SHORTCUT_LABEL,
canInjectIntoUrl,
getEffectivePageUrl,
makeStorageKey
} = globalThis.DevFeedbackShared;
const STORAGE_KEYS = {
captureMode: 'dev-feedback-popup-mode'
};
let currentTab = null;
let currentTabId = null;
let selectedMode = window.localStorage.getItem(STORAGE_KEYS.captureMode) || 'element';
function getShortcutLabel() {
return navigator.platform.toLowerCase().includes('mac') ? MAC_SHORTCUT_LABEL : SHORTCUT_LABEL;
}
function setWarning(message) {
const warning = document.getElementById('warning');
warning.textContent = message;
warning.style.display = message ? 'block' : 'none';
}
function setInfo(message) {
const info = document.getElementById('info');
info.textContent = message;
info.style.display = message ? 'block' : 'none';
}
async function init() {
document.getElementById('shortcut-label').textContent = getShortcutLabel();
bindCaptureModeInputs();
document.getElementById('primary-action-btn').addEventListener('click', handlePrimaryAction);
await loadCurrentTab();
syncModeUi();
}
function bindCaptureModeInputs() {
document.querySelectorAll('input[name="capture-mode"]').forEach((input) => {
input.checked = input.value === selectedMode;
input.addEventListener('change', () => {
selectedMode = input.value;
window.localStorage.setItem(STORAGE_KEYS.captureMode, selectedMode);
syncModeUi();
});
});
}
async function loadCurrentTab() {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
currentTab = tabs && tabs[0] ? tabs[0] : null;
currentTabId = currentTab && typeof currentTab.id === 'number' ? currentTab.id : null;
const pageLabel = document.getElementById('page-label');
if (!currentTab) {
pageLabel.textContent = 'No active tab';
updateUI(false, 0);
return;
}
pageLabel.textContent = getEffectivePageUrl(currentTab.url || currentTab.pendingUrl || currentTab.title || 'Current tab');
await refreshState();
}
async function refreshState() {
const primaryButton = document.getElementById('primary-action-btn');
const itemCount = await getItemCount();
const feedbackState = await getFeedbackState();
updateUI(feedbackState.feedbackMode, itemCount);
primaryButton.disabled = !currentTabId;
syncModeUi();
}
async function getItemCount() {
if (!currentTab?.url) {
return 0;
}
const storageKey = makeStorageKey(currentTab.url);
const result = await chrome.storage.local.get([storageKey]);
return Array.isArray(result[storageKey]) ? result[storageKey].length : 0;
}
async function getFeedbackState() {
if (!currentTabId) {
return { feedbackMode: false };
}
try {
const response = await chrome.tabs.sendMessage(currentTabId, { action: 'get-state' });
return response || { feedbackMode: false };
} catch (error) {
return { feedbackMode: false };
}
}
function syncModeUi() {
const primaryButton = document.getElementById('primary-action-btn');
const canInject = canInjectIntoUrl(currentTab?.url || '');
setWarning('');
setInfo('');
if (!currentTabId) {
primaryButton.disabled = true;
primaryButton.textContent = 'No Active Tab';
return;
}
if (selectedMode === 'region') {
primaryButton.disabled = false;
primaryButton.classList.remove('stop');
primaryButton.textContent = 'Capture Region';
if ((currentTab?.url || '').startsWith('file://')) {
setInfo('If region capture fails on a local PDF, enable "Allow access to file URLs" on the extension first.');
} else {
setInfo('Region capture takes a viewport screenshot and opens an editor tab for drawing the crop.');
}
return;
}
primaryButton.textContent = document.getElementById('feedback-mode-status').textContent === 'ON'
? 'Stop Element Mode'
: 'Start Element Mode';
primaryButton.classList.toggle('stop', document.getElementById('feedback-mode-status').textContent === 'ON');
primaryButton.disabled = !canInject;
if (!canInject) {
setWarning('Element mode needs an injectable page such as http, https, or file. Use Region mode for PDFs and browser viewer surfaces.');
return;
}
setInfo('Element mode injects the feedback UI into the current tab only after you start it.');
}
async function handlePrimaryAction() {
if (!currentTabId) {
return;
}
if (selectedMode === 'region') {
await startRegionCapture();
return;
}
await toggleElementMode();
}
async function toggleElementMode() {
setWarning('');
const ensured = await chrome.runtime.sendMessage({
action: 'ensure-content-script',
tabId: currentTabId,
url: currentTab?.url || ''
});
if (!ensured || !ensured.ok) {
setWarning(ensured?.reason || 'Unable to load the in-page feedback UI on this tab.');
return;
}
try {
const response = await chrome.tabs.sendMessage(currentTabId, { action: 'toggle-feedback-mode' });
updateUI(Boolean(response?.feedbackMode), await getItemCount());
syncModeUi();
} catch (error) {
setWarning('Refresh the current page and try again. The feedback UI did not attach cleanly.');
}
}
async function startRegionCapture() {
setWarning('');
let viewportMetrics = null;
try {
viewportMetrics = await chrome.tabs.sendMessage(currentTabId, { action: 'get-viewport-metrics' });
} catch (error) {
viewportMetrics = null;
}
const response = await chrome.runtime.sendMessage({
action: 'start-region-capture',
tab: {
id: currentTab.id,
windowId: currentTab.windowId,
url: currentTab.url,
title: currentTab.title
},
viewportMetrics
});
if (!response || !response.ok) {
setWarning(response?.reason || 'Unable to start region capture on this tab.');
return;
}
window.close();
}
function updateUI(feedbackMode, itemCount) {
const statusText = document.getElementById('feedback-mode-status');
const itemCountEl = document.getElementById('item-count');
statusText.textContent = feedbackMode ? 'ON' : 'OFF';
statusText.classList.toggle('active', feedbackMode);
itemCountEl.textContent = String(itemCount);
}
document.addEventListener('DOMContentLoaded', () => {
init().catch((error) => {
setWarning(error.message || 'Unable to initialize the popup.');
});
});
})();