-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
254 lines (229 loc) · 8.02 KB
/
popup.js
File metadata and controls
254 lines (229 loc) · 8.02 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
// Debug logging setup
const DEBUG = true;
function log(message) {
if (DEBUG) console.log(`[Popup] ${message}`);
}
// List of special page patterns that shouldn't allow capture
const SPECIAL_PAGES = [
'chrome://*',
'chrome-extension://*',
'edge://*',
'about:*',
'view-source:*'
];
// Additional restricted domains (especially for Google domains)
const RESTRICTED_DOMAINS = [
'accounts.google.com',
'www.google.com',
'google.com'
];
// Helper function to check if current URL matches special page patterns
function isSpecialPage(url) {
return SPECIAL_PAGES.some(pattern => {
const regexPattern = pattern.replace(/\*/g, '.*');
return new RegExp(`^${regexPattern}$`).test(url);
});
}
// Helper function to check if URL is in restricted domains
function isRestrictedDomain(url) {
try {
const urlObj = new URL(url);
return RESTRICTED_DOMAINS.some(domain =>
urlObj.hostname === domain || urlObj.hostname.endsWith('.' + domain)
);
} catch (e) {
console.error('Invalid URL:', e);
return false;
}
}
function handleButtonFeedback(buttonId) {
const button = document.getElementById(buttonId);
if (button) {
button.style.opacity = '0.7';
setTimeout(() => button.style.opacity = '1', 200);
}
}
// Function to show/hide warning message
function toggleWarningMessage(show, isRestricted = false) {
let warningEl = document.getElementById('pageWarning');
if (!warningEl && show) {
warningEl = document.createElement('div');
warningEl.id = 'pageWarning';
warningEl.style.backgroundColor = '#fff3cd';
warningEl.style.color = '#856404';
warningEl.style.padding = '10px';
warningEl.style.margin = '10px';
warningEl.style.borderRadius = '4px';
warningEl.style.fontSize = '1rem';
warningEl.style.textAlign = 'center';
warningEl.innerText = isRestricted ?
'This page has security restrictions that prevent screen capture.' :
'This is a special page. Unable to perform screen capture actions on this page.';
document.querySelector('.container').insertBefore(
warningEl,
document.querySelector('.container').firstChild
);
} else if (warningEl && !show) {
warningEl.remove();
}
}
// Function to disable/enable capture buttons
function toggleCaptureButtons(disable) {
const buttons = [
'captureFull',
'captureArea',
'captureWhole',
'captureDesktop' // Add desktop capture button to the list
];
buttons.forEach(id => {
const button = document.getElementById(id);
if (button) {
button.disabled = disable;
if (disable) {
button.style.opacity = '0.5';
button.style.cursor = 'not-allowed';
} else {
button.style.opacity = '1';
button.style.cursor = 'pointer';
}
}
});
}
function showError(message) {
const errorDiv = document.createElement('div');
errorDiv.style.backgroundColor = '#ffebee';
errorDiv.style.color = '#c62828';
errorDiv.style.padding = '10px';
errorDiv.style.margin = '10px';
errorDiv.style.borderRadius = '4px';
errorDiv.style.fontSize = '1rem';
errorDiv.textContent = message;
const container = document.querySelector('.container');
container.insertBefore(errorDiv, container.firstChild);
setTimeout(() => errorDiv.remove(), 3000);
}
// Check current tab when popup opens
document.addEventListener('DOMContentLoaded', () => {
log('Popup loaded');
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0]) {
const url = tabs[0].url;
const isSpecial = isSpecialPage(url);
const isRestricted = isRestrictedDomain(url);
if (isSpecial || isRestricted) {
toggleWarningMessage(true, isRestricted);
toggleCaptureButtons(true);
}
}
});
});
// Event listeners for capture buttons
document.getElementById('captureFull').addEventListener('click', () => {
log('Full capture button clicked');
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0] && !isSpecialPage(tabs[0].url) && !isRestrictedDomain(tabs[0].url)) {
// Get capture mode from storage
chrome.storage.sync.get({ captureMode: 'hardware' }, (settings) => {
log(`Sending captureFullScreen message with mode: ${settings.captureMode}`);
chrome.runtime.sendMessage({
action: 'captureFullScreen',
mode: 'full',
devicePixelRatio: window.devicePixelRatio,
captureMode: settings.captureMode
}, (response) => {
log(`Response from background: ${JSON.stringify(response)}`);
if (chrome.runtime.lastError) {
log(`Error: ${chrome.runtime.lastError.message}`);
}
// Auto-dismiss popup after starting capture
setTimeout(() => window.close(), 100);
});
});
}
});
});
document.getElementById('captureArea').addEventListener('click', () => {
log('Area capture button clicked');
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0] && !isSpecialPage(tabs[0].url) && !isRestrictedDomain(tabs[0].url)) {
// Get capture mode from storage
chrome.storage.sync.get({ captureMode: 'hardware' }, (settings) => {
chrome.runtime.sendMessage({
action: 'captureScreen',
mode: 'selected',
captureMode: settings.captureMode
});
// Auto-dismiss popup after starting capture
window.close();
});
}
});
});
document.getElementById('captureWhole').addEventListener('click', () => {
log('Whole page capture button clicked');
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0] && !isSpecialPage(tabs[0].url) && !isRestrictedDomain(tabs[0].url)) {
// Get capture mode from storage
chrome.storage.sync.get({ captureMode: 'hardware' }, (settings) => {
log(`Sending captureWholePage message with mode: ${settings.captureMode}`);
chrome.runtime.sendMessage({
action: 'captureWholePage',
mode: 'whole',
devicePixelRatio: window.devicePixelRatio,
captureMode: settings.captureMode
}, (response) => {
log(`Response from background: ${JSON.stringify(response)}`);
if (chrome.runtime.lastError) {
log(`Error: ${chrome.runtime.lastError.message}`);
}
// Auto-dismiss popup after starting capture
setTimeout(() => window.close(), 100);
});
});
}
});
});
document.getElementById('captureDesktop').addEventListener('click', () => {
log('Desktop capture button clicked');
const button = document.getElementById('captureDesktop');
button.style.opacity = '0.7';
setTimeout(() => button.style.opacity = '1', 200);
// Get devicePixelRatio from the popup window context
const ratio = window.devicePixelRatio || 1;
// Get capture mode from storage
chrome.storage.sync.get({ captureMode: 'hardware' }, (settings) => {
chrome.runtime.sendMessage({
action: 'captureDesktop',
mode: 'desktop',
devicePixelRatio: ratio,
captureMode: settings.captureMode
}, (response) => {
log(`Received response from background: ${JSON.stringify(response)}`);
if (chrome.runtime.lastError) {
log(`Error: ${chrome.runtime.lastError.message}`);
showError(chrome.runtime.lastError.message);
} else if (response && !response.success) {
showError(response.error || 'Failed to capture desktop');
} else {
// Auto-dismiss popup after successful desktop capture initiation
window.close();
}
});
});
});
document.getElementById('openOptions').addEventListener('click', () => {
log('Options button clicked');
handleButtonFeedback('openOptions');
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL('options.html'));
}
});
// Listen for messages from background script
chrome.runtime.onMessage.addListener((message) => {
log(`Received message: ${JSON.stringify(message)}`);
if (message.action === 'captureError') {
showError(message.error);
}
});