-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
61 lines (49 loc) · 1.24 KB
/
background.js
File metadata and controls
61 lines (49 loc) · 1.24 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
let creatingOffscreenDocument = null;
async function ensureOffscreenDocument() {
if (await chrome.offscreen.hasDocument()) {
return;
}
if (!creatingOffscreenDocument) {
creatingOffscreenDocument = chrome.offscreen.createDocument({
url: "offscreen.html",
reasons: ["CLIPBOARD"],
justification: "Copy the active tab URL to the clipboard."
});
}
try {
await creatingOffscreenDocument;
} finally {
creatingOffscreenDocument = null;
}
}
async function getActiveTabUrl() {
const [tab] = await chrome.tabs.query({
active: true,
lastFocusedWindow: true
});
return tab?.url ?? "";
}
async function copyToClipboard(text) {
await ensureOffscreenDocument();
const response = await chrome.runtime.sendMessage({
type: "copy-to-clipboard",
text
});
if (!response?.ok) {
throw new Error(response?.error || "Clipboard copy failed.");
}
}
chrome.commands.onCommand.addListener(async (command) => {
if (command !== "copy-current-url") {
return;
}
try {
const url = await getActiveTabUrl();
if (!url) {
throw new Error("No active tab URL found.");
}
await copyToClipboard(url);
} catch (error) {
console.error("URL, Instant Copy:", error);
}
});