-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontent.js
54 lines (46 loc) · 1.61 KB
/
content.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
const isJSON = (json) => {
if (Object.prototype.toString.call(json) !== "[object String]") return false
try{
return JSON.parse(json)
}catch (e){ return false}
}
document.addEventListener('xianWalletGetInfo', (event) => {
getWalletInfo()
});
document.addEventListener('xianWalletSendTx', (event) => {
xianWalletSendTx(event.detail)
});
document.addEventListener('xianWalletSignMsg', (event) => {
xianWalletSignMsg(event.detail)
});
const xianWalletSendTx = (detail) => {
chrome.runtime.sendMessage({type: 'dAppSendTransaction', data: detail}, (response) => {
if(!chrome.runtime.lastError || response !== 'ok'){
document.dispatchEvent(new CustomEvent('xianWalletTxStatus', {detail: response}));
handleFocus();
}
});
}
const getWalletInfo = () => {
chrome.runtime.sendMessage({type: 'getWalletInfo'}, (response) => {
if(!chrome.runtime.lastError || response !== 'ok'){
document.dispatchEvent(new CustomEvent('xianWalletInfo', {detail: response}));
}
});
}
const xianWalletSignMsg = (detail) => {
chrome.runtime.sendMessage({type: 'dAppSignMessage', data: detail}, (response) => {
if(!chrome.runtime.lastError || response !== 'ok'){
document.dispatchEvent(new CustomEvent('xianWalletSignMsgResponse', {detail: response}));
handleFocus();
}
});
}
const handleFocus = () => {
window.blur();
setTimeout(() => {
window.focus();
}, 100);
};
// Dispatch xianReady event when the content script is loaded and ready
document.dispatchEvent(new CustomEvent('xianReady'));