forked from fsjuhl/LedgerBluetoothBridgeExtension
-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
84 lines (73 loc) · 2.97 KB
/
background.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
let debug = true,
messagesToSend = [],
tabId,
tabPort,
connectionPromise,
modifiedScript
fetch("https://metamask.github.io/eth-ledger-bridge-keyring/bundle.js")
.then(res => res.text())
.then(async script => {
const modification = await fetch(chrome.runtime.getURL("modification.js"))
.then(res => res.text())
script = script
.replace("var _WebSocketTransport2 = _interopRequireDefault(_WebSocketTransport);", modification)
.replace("window.open('ledgerlive://bridge?appName=Ethereum');", "")
modifiedScript = { redirectUrl: "data:text/javascript," + encodeURIComponent(script) }
console.log("Successfully modified bundle")
})
const handleMessage = async message => {
if (typeof message == "string") return
if (message.type == "open") {
openConnectionTab()
} else if (message.type == "exchange") {
if (tabPort) {
tabPort.postMessage(message)
} else {
await new Promise(resolve => connectionPromise = resolve)
tabPort.postMessage(message)
}
}
}
const sendMessage = message => messagesToSend.push(message)
chrome.webRequest.onBeforeRequest.addListener(request => {
if (request.url == "https://metamask.github.io/eth-ledger-bridge-keyring/bundle.js") {
if (debug && request.frameId != 0) console.log(`Injected modified bundle`)
return request.frameId != 0 ? modifiedScript : { cancel: false }
} else {
const input = JSON.parse(atob(request.url.split(".com/")[1]))
input.forEach(handleMessage)
if (debug && input.length > 0 || messagesToSend.length > 0) console.log(`BluetoothBridgeTransport: Got messages ${JSON.stringify(input)} | Sending messages ${JSON.stringify(messagesToSend)}`)
const output = btoa(JSON.stringify(messagesToSend))
messagesToSend = []
return { redirectUrl: `data:text/plain;base64,${output}` }
}
}, { urls: [
"https://metamask.github.io/eth-ledger-bridge-keyring/bundle.js",
"https://ledgerbluetoothbridge.example.com/*"
] }, ["blocking"])
async function openConnectionTab() {
if (debug) console.log(`Opening Bridge tab`)
if (tabId) {
try {
await chrome.tabs.update(tabId, { active: true })
tabPort.postMessage({ type: "open" })
return
} catch (e) {
console.error("good error", e)
}
}
chrome.tabs.create({ url: chrome.extension.getURL("connect/index.html") }, tab => {
tabId = tab.id
})
}
chrome.runtime.onConnect.addListener(port => {
if (port.sender.tab.id == tabId) {
tabPort = port
port.onDisconnect.addListener(() => { tabPort = null; sendMessage({ type: "closed" }) })
port.onMessage.addListener(info => {
if (debug) console.log(`Message from Bridge tab: ${JSON.stringify(info)}`)
sendMessage(info)
if (connectionPromise) connectionPromise()
})
}
})