-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.ts
89 lines (83 loc) · 2.45 KB
/
background.ts
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
import type { PlasmoCSConfig } from "plasmo"
import {
ACTION_TYPES,
COMMANDS,
DefaultOptions,
storage,
STORE_KEYS,
type OptionType
} from "~constant/index"
export const config: PlasmoCSConfig = {
matches: ["<all_urls>"]
}
function handleCommand(command: string) {
chrome.tabs.query({ active: true, currentWindow: true }, async (tabs) => {
if (tabs[0]) {
const url = new URL(tabs[0].url ?? "")
if (url.protocol.startsWith("chrome")) {
return
}
const tabInfo = {
title: tabs[0].title,
url: url.href,
urlNoParams: `${url.origin}${url.pathname}`,
hostname: url.hostname,
pathname: url.pathname,
hash: url.hash,
params: url.search,
protocol: url.protocol
}
let text = ""
switch (command) {
case COMMANDS.COPY_TAB_INFO:
text = `${tabInfo.title}\n${tabInfo.url}`
break
case COMMANDS.COPY_TAB_INFO_MARKDOWN:
text = `[${tabInfo.title}](${tabInfo.url})`
break
case COMMANDS.COPY_TAB_INFO_CUSTOM:
const options = await storage.get<OptionType>(STORE_KEYS)
let result = []
if (options.protocol) result.push(`${tabInfo.protocol}//`)
if (options.hostname) result.push(tabInfo.hostname)
if (options.pathname) result.push(tabInfo.pathname)
if (options.hash) result.push(tabInfo.hash)
if (options.params) result.push(tabInfo.params)
if (options.title) text = `${tabInfo.title}\n${result.join("")}`
else text = `${result.join("")}`
copyTextToClipboard(tabs[0].id, text)
return // Exit early since this is asynchronous
default:
return
}
copyTextToClipboard(tabs[0].id, text)
}
})
}
chrome.commands.onCommand.addListener(handleCommand)
function copyTextToClipboard(tabId: number, text: string) {
if (!text) {
return
}
chrome.scripting.executeScript(
{
target: { tabId: tabId },
func: (copyText) => {
const textArea = document.createElement("textarea")
textArea.value = copyText
document.body.appendChild(textArea)
textArea.select()
document.execCommand("copy")
document.body.removeChild(textArea)
},
args: [text]
},
() => {
if (chrome.runtime.lastError) {
console.error(
"Script execution failed: " + chrome.runtime.lastError.message
)
}
}
)
}