forked from automerge/pushpin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
42 lines (34 loc) · 1.18 KB
/
index.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
import freezeDry from 'freeze-dry'
import { ipcRenderer } from 'electron'
import Queue from 'hypermerge/dist/Queue'
import FileServerClient from 'hypermerge/dist/FileServerClient'
import * as Stream from 'hypermerge/dist/StreamLogic'
import { FILE_SERVER_PATH } from '../renderer/constants'
const files = new FileServerClient()
files.setServerPath(FILE_SERVER_PATH)
type Msg = ReadyMsg
interface ReadyMsg {
type: 'Ready'
}
const messageQ = new Queue<Msg>('freeze-dry-preload:messageQ')
ipcRenderer.on('freeze-dry', (_event, msg: Msg) => {
messageQ.push(msg)
})
messageQ.first().then(capturePage)
async function capturePage() {
// TODO(jeff): Provide a custom `fetchResource(url: string): Response`
// option to prevent cors errors.
const html = await freezeDry(document)
const { url } = await files.write(
Stream.fromBuffer(Buffer.from(html, 'utf8')),
document.contentType
)
ipcRenderer.sendToHost('freeze-dry', url)
}
window.addEventListener('beforeunload', onBeforeUnload)
function onBeforeUnload(event: BeforeUnloadEvent) {
// TODO(jeff): I'm trying to prevent navigation here, but it doesn't work:
event.preventDefault()
event.returnValue = 'stop'
return 'stop'
}