forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsidepanel.ts
More file actions
38 lines (36 loc) · 1.05 KB
/
Copy pathsidepanel.ts
File metadata and controls
38 lines (36 loc) · 1.05 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
// types/chrome.d.ts
declare namespace chrome {
namespace runtime {
const id: string | undefined
}
namespace tabs {
interface Tab {
id?: number
index: number
windowId: number
}
function query(queryOptions: { active: boolean, lastFocusedWindow: boolean }): Promise<[chrome.tabs.Tab]>
function sendMessage(tabId: number, message: any): void
}
}
const isInExtension = typeof chrome !== `undefined` && chrome.runtime && chrome.runtime.id
async function getCurrentTab() {
const queryOptions = { active: true, lastFocusedWindow: true }
if (typeof browser !== `undefined` && browser.tabs && browser.tabs.query) {
const [tab] = await browser.tabs.query(queryOptions)
return tab
}
const [tab] = await chrome.tabs.query(queryOptions)
return tab
}
window.addEventListener(`copyToMp`, (e) => {
const customEvent = e as CustomEvent
if (!isInExtension)
return
getCurrentTab().then((tab) => {
chrome.tabs.sendMessage(tab.id!, {
type: `copyToMp`,
content: customEvent.detail.content,
})
})
})