forked from hplush/slowreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.ts
More file actions
34 lines (31 loc) · 913 Bytes
/
background.ts
File metadata and controls
34 lines (31 loc) · 913 Bytes
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
import type { AppMessage, ExtensionMessage } from './api.js'
import { config } from './config.js'
const FETCH_TIMEOUT_MS = 30000
function sendMessage(
port: chrome.runtime.Port,
message: ExtensionMessage
): void {
port.postMessage(message)
}
chrome.runtime.onConnectExternal.addListener(port => {
if (port.sender?.origin === config.HOST) {
sendMessage(port, { type: 'connected' })
port.onMessage.addListener(async (message: AppMessage) => {
try {
let response = await fetch(message.url, {
...message.options,
signal: AbortSignal.timeout(FETCH_TIMEOUT_MS)
})
let data = await response.text()
sendMessage(port, { data, type: 'fetched' })
} catch (error) {
if (error instanceof Error) {
sendMessage(port, {
error: error.toString(),
type: 'error'
})
}
}
})
}
})