/
embed.js
58 lines (52 loc) · 1.89 KB
/
embed.js
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
export const embed = async () => {
try {
const baseURL = new URL("https://lunet.link/companion/")
setStatusMessage(
"⚙️ Setting application to serve you even without interent."
)
let registration = null
if (!navigator.serviceWorker.controller) {
registration = await navigator.serviceWorker.register(
"./lunet.link.companion.service.js?_=3",
{
scope: new URL(location).pathname
}
)
}
// try {
// await registration.update()
// } catch (error) {
// console.warn("Failed to perform update", error)
// }
setStatusMessage("⚙️ Setting up a bridge with lunet.link.")
const frame = document.createElement("iframe")
frame.src = new URL("bridge.html", baseURL)
frame.style.display = "none"
const loaded = onLoad(frame)
document.head.appendChild(frame)
await loaded
var { port1, port2 } = new MessageChannel()
frame.contentWindow.postMessage("connect", baseURL, [port1])
const ready = await navigator.serviceWorker.ready
setStatusMessage("⚙️ Bridging with lunet.link.")
ready.active.postMessage("connect", [port2])
await onMessage(navigator.serviceWorker)
setStatusMessage("🎉 All set! Will be there for you any time")
const request = await fetch(location.href)
const content = await request.text()
const parser = new DOMParser()
const { documentElement } = parser.parseFromString(content, "text/html")
document.documentElement.replaceWith(documentElement)
} catch (error) {
setStatusMessage(`☹️ Ooops, Something went wrong`)
console.error({ message: error.message, stack: error.stack })
}
}
const when = type => target =>
new Promise(resolve => target.addEventListener(type, resolve))
const onLoad = when("load")
const onMessage = when("message")
const setStatusMessage = message => {
document.body.textContent = message
}
embed()