-
Notifications
You must be signed in to change notification settings - Fork 13
/
background.js
85 lines (78 loc) · 2 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
85
// popup neuron web
let appId = null
// dapp
let dappId = null
_citaSDK = CITASDK()
// init _accounts
let _accounts = []
const loadAccounts = () => {
_citaSDK.base.accounts.wallet.load('')
_accounts = Array.from(_citaSDK.base.accounts.wallet).map(w => w.address)
_citaSDK.base.accounts.wallet.clear()
return _accounts
}
// load accounts
loadAccounts()
const sendMsgToApp = (action, data) => {
setTimeout(() => {
chrome.runtime.sendMessage({
action,
data: { ...data,
dappId,
appId
}
})
}, 500)
}
chrome.windows.onRemoved.addListener(id => {
if (id === appId) {
appId === null
}
})
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'open') {
chrome.tabs.query({
active: true,
currentWindow: true
}, tabs => {
if (tabs[0]) {
dappId = tabs[0].id
}
})
if (appId) {
chrome.windows.remove(appId)
}
chrome.windows.create({
url: "index.html#/transactions",
type: 'popup',
width: 600,
height: 700
}, res => {
appId = res.id
sendMsgToApp('confirm', message.data)
})
} else if (message.action === 'manifest') {
window.localStorage.setItem('manifest', JSON.stringify(message.data))
} else if (message.action === 'getAccounts') {
sendResponse(_accounts)
} else if (message.action === 'getDefaultAccount') {
sendResponse(_accounts[0])
} else if (['returnTransactionReceipt', 'returnSignedMessage'].includes(message.action)) {
chrome.windows.remove(appId)
chrome.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, message, res => {});
});
} else if (message.action === 'privateKeyChanged') {
// update accounts when privatekey changed
loadAccounts()
chrome.tabs.query({
active: true,
currentWindow: true
}, function (tabs) {
chrome.tabs.sendMessage(tabs[0].id, message, res => {});
});
}
})