-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinjector.js
122 lines (103 loc) · 3.37 KB
/
injector.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* @name ScaleWallet - Everscale browser wallet and injector
* @copyright SVOI.dev Labs - https://svoi.dev
* @license Apache-2.0
* @version 1.0
*/
const CONFIG_REQUEST_ID = Math.random();
//Message proxy
window.addEventListener("message", function (event) {
// We only accept messages from ourselves
if(event.source != window) {
return;
}
//Outcome RPC to page
if(event.data.method /*&& (event.data.type == "FROM_PAGE")*/) {
browser.runtime.sendMessage({...event.data, sender: 'page', senderMore: {url: window.location}});
}
});
//Income from extension
browser.runtime.onMessage.addListener(async (msg, sender) => {
if(msg.target === '*') {
window.postMessage(msg, "*");
}
//If we receive config response
if(msg.requestId && msg.requestId === CONFIG_REQUEST_ID) {
evalScript(msg.result);
}
});
/**
* Inject script by url
* @param urlExt
*/
function injectScriptUrl(urlExt) {
try {
const container = document.head || document.documentElement;
const scriptTag = document.createElement('script');
scriptTag.setAttribute('async', 'false');
scriptTag.setAttribute('src', urlExt);
container.insertBefore(scriptTag, container.children[0]);
container.removeChild(scriptTag);
} catch (error) {
console.error('EverscaleWallet: injector failed', error);
}
}
/**
* Inject script by url
* @param urlExt
*/
function injectModuleUrl(urlExt) {
try {
const container = document.head || document.documentElement;
const scriptTag = document.createElement('script');
scriptTag.setAttribute('async', 'false');
scriptTag.setAttribute('src', urlExt);
scriptTag.setAttribute('type', 'module');
container.insertBefore(scriptTag, container.children[0]);
container.removeChild(scriptTag);
} catch (error) {
console.error('EverscaleWallet: injector failed', error);
}
}
/**
* Eval script on page
* @param source
*/
function evalScript(source) {
try {
const container = document.head || document.documentElement;
const scriptTag = document.createElement('script');
scriptTag.setAttribute('async', 'false');
scriptTag.textContent = source;
container.insertBefore(scriptTag, container.children[0]);
container.removeChild(scriptTag);
} catch (error) {
console.error('EverscaleWallet: injector failed', error);
}
}
//Inject TONCLient
injectScriptUrl(browser.extension.getURL("ton-client/main.js"));
//Inject new TON lib
injectScriptUrl(browser.extension.getURL("ever-sdk-js/main.js"));
//Set Everscale binary url
evalScript(`window.tonWasmUrl = "${browser.extension.getURL("ton-client/tonclient.wasm")}"`);
//Set new Everscale binary url
evalScript(`window.tonNewWasmUrl = "${browser.extension.getURL("ever-sdk-js/eversdk.wasm")}"`);
//Thridrparty modules
injectModuleUrl(browser.extension.getURL("modules/thirdparty/eventemitter3.min.js"));
//Start Everscale provider
injectModuleUrl(browser.extension.getURL("everscaleProvider.js"));
//Get configuring script
try {
browser.runtime.sendMessage({
rpc: true,
requestId: CONFIG_REQUEST_ID,
target: 'background',
method: 'main_getConfigScript',
params: [],
sender: 'page',
senderMore: {url: window.location}
});
} catch (e) {
console.log(e);
}