-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmyscript.js
57 lines (52 loc) · 1.9 KB
/
myscript.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
function getBitmexTab() {
return new Promise((resolve, reject) => {
chrome.tabs.getAllInWindow(tabs => {
const bitmexTab = tabs.filter(tab => tab.url.includes('bitmex.com/app/trade/'))[0];
return bitmexTab !== undefined ? resolve(bitmexTab) : reject();
});
});
}
function uuidv4() {
return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
)
}
function makeMessage(message){
return {
message,
extensionId: chrome.runtime.id
};
}
window.addEventListener('load', () => {
// chrome.runtime.onMessage.addListener(message => {
// console.log(message);
// switch (message.type) {
// case 'notification':
// chrome.notifications.create(uuidv4(), message.content);
// break;
// }
// });
document.getElementById('start').addEventListener('click', () => {
getBitmexTab()
.then(bitmexTab => {
chrome.tabs.onUpdated
.addListener((tabId, changeInfo) => {
if (tabId === bitmexTab.id && changeInfo.status === 'complete') {
console.log(changeInfo);
chrome.tabs.executeScript(bitmexTab.id, {
file: 'execute_script.js'
}, () => {
chrome.tabs.sendMessage(bitmexTab.id, makeMessage('start'));
});
}
});
chrome.tabs.reload(bitmexTab.id);
});
});
document.getElementById('stop').addEventListener('click', () => {
getBitmexTab()
.then(bitmexTab => {
chrome.tabs.sendMessage(bitmexTab.id, makeMessage('stop'));
});
});
});