-
Notifications
You must be signed in to change notification settings - Fork 24
/
main.ts
52 lines (47 loc) · 1.14 KB
/
main.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
import { sendMessage } from 'webext-bridge'
import { History } from 'webextension-polyfill'
// only on dev mode
if (import.meta.hot) {
// @ts-expect-error for background HMR
import('/@vite/client')
// load latest content script
import('./contentScriptHMR')
}
const removeDuplicates = (histories: History.HistoryItem[]) => {
return Array.from(
histories
.reduce(
(map, currentItem) => map.set(currentItem.title!, currentItem),
new Map<string, History.HistoryItem>(),
)
.values(),
)
}
const addHostname = (histories: History.HistoryItem[]) => {
return histories.map((item) => {
const url = new URL(item.url!)
return {
...item,
hostname: url.hostname,
}
},
)
}
browser.commands.onCommand.addListener(async() => {
const result = await browser.history.search({
text: '',
maxResults: 10000,
})
const [tab] = await browser.tabs.query({
active: true,
currentWindow: true,
})
await sendMessage(
'history-search',
{ result: JSON.stringify(addHostname(removeDuplicates(result))) },
{
context: 'content-script',
tabId: tab.id!,
},
)
})