-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker.js
More file actions
67 lines (59 loc) · 2.08 KB
/
worker.js
File metadata and controls
67 lines (59 loc) · 2.08 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
'use strict';
// Define keyboard shortcut hint based on the user's platform
const isMac = navigator.platform.toUpperCase().indexOf('MAC') >= 0;
const shortcutHint = isMac ? '(Cmd+Shift+X)' : '(Ctrl+Shift+X)';
// Context Menu Setup
const setupContextMenu = () => {
chrome.contextMenus.create({
id: 'copy-plain',
title: `Copy plain text to the clipboard ${shortcutHint}`,
contexts: ['selection'], // Show only when text is selected
});
};
// Handle Context Menu Click
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
if (info.menuItemId === 'copy-plain') {
handleCopyAction(tab, info.frameId);
}
});
// Handle Keyboard Shortcut
chrome.commands.onCommand.addListener(async (command) => {
if (command === 'copy_plain_text') {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab) {
handleCopyAction(tab);
}
}
});
async function handleCopyAction(tab, frameId = null) {
try {
// Attempt to get the selected text
const result = await chrome.scripting.executeScript({
target: { tabId: tab.id, frameIds: frameId ? [frameId] : undefined },
func: () => window.getSelection().toString().trim(),
});
if (result && result[0]?.result) {
const selectedText = result[0].result;
// Inject clipboard logic into the active tab
await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: (text) => {
navigator.clipboard.writeText(text).then(() => {
console.log('Text copied to clipboard:', text);
}).catch((err) => {
console.error('Failed to write to clipboard:', err);
alert('Failed to copy text: ' + err.message);
});
},
args: [selectedText],
});
} else {
console.warn('No text selected.');
}
} catch (err) {
console.error('Failed to copy plain text:', err);
}
}
// Initialize Context Menu
chrome.runtime.onInstalled.addListener(setupContextMenu);
chrome.runtime.onStartup.addListener(setupContextMenu);