-
Notifications
You must be signed in to change notification settings - Fork 7
/
launch.ts
61 lines (54 loc) · 2.24 KB
/
launch.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
53
54
55
56
57
58
59
60
61
// This script will be added as a content script at document_end to pages of the extension host https://pilot.gnosisguild.org/.
// It runs in the context of the extension, meaning it has access to all the chrome.* APIs.
// It cancels rendering that page and instead renders the extension page.
// make sure we have a <html lang="en"> element
const docEl = document.documentElement
while (docEl.attributes.length > 0)
docEl.removeAttribute(docEl.attributes[0].name)
docEl.setAttribute('lang', 'en')
// Make the extension public path available to the app (read via publicPath.ts)
docEl.dataset.publicPath = chrome.runtime.getURL('/').slice(0, -1)
// clear everything and initialize Pilot app
docEl.innerHTML = `
<head>
<meta charset="utf-8" />
<link rel="icon" type="image/png" href="${chrome.runtime.getURL(
'/zodiac32.png'
)}" sizes="32x32" />
<link rel="icon" type="image/png" href="${chrome.runtime.getURL(
'/zodiac128.png'
)}" sizes="128x128" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Simulate dApp interactions and record transactions"
/>
<title>Zodiac Pilot</title>
<link rel="stylesheet" href="${chrome.runtime.getURL('/build/app.css')}" />
</head>
<body>
<div id="root"></div>
</body>
`
// now we're ready to launch the app.
// Adding it as a script node makes it run in the context of the external Zodiac Pilot host
const node = document.createElement('script')
node.src = chrome.runtime.getURL('/build/app.js')
const parent = document.head || document.documentElement
parent.appendChild(node)
// Background scripts can communicate with content scripts via chrome.runtime.sendMessage.
// Background script cannot send messages to the foreign origin window, but the content script can.
// So here we are relaying chrome.runtime to window.
chrome.runtime.onMessage.addListener((message) => {
if (message.type === 'navigationDetected') {
window.postMessage(message, '*')
}
})
// Relay messages from the extension page to the background script
window.addEventListener('message', (event) => {
if (event.data.toBackground) {
chrome.runtime.sendMessage(event.data)
}
})
export {}