-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoffscreen.js
More file actions
77 lines (65 loc) · 2.88 KB
/
offscreen.js
File metadata and controls
77 lines (65 loc) · 2.88 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
// offscreen.js
console.log("[Offscreen] Document loaded and script running.");
// Check if the current context has access to the clipboard API.
// While execCommand is the primary method here, it's good practice to be aware.
if (navigator.clipboard) {
console.log("[Offscreen] Clipboard API is available.");
} else {
console.warn("[Offscreen] Clipboard API is *not* available. Relying on execCommand.");
}
// Listen for messages from the service worker (background.js)
chrome.runtime.onMessage.addListener(handleMessages);
function handleMessages(message, sender, sendResponse) {
console.log("[Offscreen] Received message:", message);
// Check if the message is intended for this offscreen document
// (Good practice if you might have multiple offscreen documents later)
if (message.target !== 'offscreen') {
console.log("[Offscreen] Message not targeted for offscreen. Ignoring.");
return false; // Indicate message not handled
}
// Handle specific actions based on the message
switch (message.action) {
case 'copyToClipboard':
copyTextToClipboard(message.data, sendResponse);
break;
// Add other cases here if the offscreen document needs more capabilities
default:
console.warn(`[Offscreen] Unrecognized action received: ${message.action}`);
sendResponse({ success: false, error: 'Unrecognized action' });
}
// Crucially, return true to indicate that sendResponse will be called asynchronously
// (even though execCommand is synchronous, this prevents the message channel from closing prematurely)
return true;
}
// Function to perform the actual copy operation
function copyTextToClipboard(text, sendResponse) {
const textArea = document.getElementById('clipboardHelper');
if (!textArea) {
console.error("[Offscreen] Clipboard helper textarea not found!");
sendResponse({ success: false, error: 'Offscreen document setup error.' });
return;
}
// Set the text in the helper textarea
textArea.value = text;
// Select the text
textArea.select();
try {
// Execute the copy command
// Note: document.execCommand() is technically deprecated but is currently
// the most reliable way to copy from an offscreen document in MV3.
const success = document.execCommand('copy');
if (success) {
console.log("[Offscreen] Text successfully copied to clipboard:", text);
sendResponse({ success: true });
} else {
console.error("[Offscreen] document.execCommand('copy') returned false.");
sendResponse({ success: false, error: 'Copy command failed.' });
}
} catch (err) {
console.error("[Offscreen] Error during document.execCommand('copy'):", err);
sendResponse({ success: false, error: err.message || 'Exception during copy.' });
}
// Optional: Clear the textarea after copying to avoid lingering data
textArea.value = '';
}
console.log("[Offscreen] Message listener attached.");