-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
35 lines (32 loc) · 905 Bytes
/
background.js
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
const showToast = (tabId, message, type) => {
chrome.tabs.sendMessage(tabId, {
action: "showToast",
message,
type,
})
}
chrome.commands.onCommand.addListener((command) => {
if (command === "copy-url") {
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
// Get current tab's URL
const activeTab = tabs[0]
const url = activeTab.url
// Copy to clipboard
try {
chrome.scripting.executeScript({
target: { tabId: activeTab.id },
func: (text) => {
navigator.clipboard.writeText(text)
},
args: [url],
})
// Show success toast
showToast(activeTab.id, "Copied Current URL", "success")
} catch (error) {
// Show error toast
showToast(activeTab.id, "Copying URL failed", "error")
console.error(error)
}
})
}
})