-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
146 lines (126 loc) · 4.35 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
let waitingForRequestPage = true;
let tabId = -1;
let windowId = -1;
let requestTabId = -1;
let requestPopupWindow;
chrome.runtime.onConnect.addListener(function (port) {
}); // just accept connection
async function getCurrentTab() {
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
return tab;
}
chrome.windows.onFocusChanged.addListener(async (focusInfo) => {
var currentTab = await getCurrentTab();
if (currentTab) {
tabId = currentTab.id;
windowId = focusInfo;
}
});
chrome.tabs.onActivated.addListener((activeInfo) => {
if (activeInfo.tabId == requestTabId) {
return;
}
tabId = activeInfo.tabId;
windowId = activeInfo.windowId;
});
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === 'executeForeground') {
try {
chrome.scripting.executeScript({
target: { tabId: tabId },
files: ["./foreground.js"]
}).catch((error) => {
console.log(error);
})
} catch (error) {
//Some pages like chrome extensions block scripting, digest this exception.
console.log(error);
}
}
if (request.message === 'requestPageLoaded') {
waitingForRequestPage = false;
}
if (request.message === 'onNovioTxRequest' || request.message === 'onNovioSignRequest') {
if (requestPopupWindow) {
chrome.windows.remove(requestPopupWindow.id);
requestPopupWindow = null;
}
//when the background goes to sleep we lose track so we fetch it instead
if (windowId = -1) {
chrome.windows.getCurrent((currentWindow) => {
windowId = currentWindow.id;
openRequest(request, sendResponse);
})
} else {
openRequest(request, sendResponse);
}
}
return true;
});
function openRequest(request, sendResponse) {
chrome.windows.get(windowId, async (tabWindow) => {
let width = 417;
let height = 564;
let url = 'request/request.html';
if (request.message === 'onNovioSignRequest') {
url = 'request/sign.html';
height = 320;
}
const left = Math.round((tabWindow.width - width) * 0.5 + tabWindow.left)
const top = Math.round((tabWindow.height - height) * 0.5 + tabWindow.top)
waitingForRequestPage = true;
requestPopupWindow = await chrome.windows.create({
width: width,
height: height,
top: Math.round(top),
left: Math.round(left),
focused: true,
type: 'popup',
url: url,
});
requestTabId = requestPopupWindow.tabs[0].id;
setTimeout(async () => {
while (waitingForRequestPage) {
await sleep(50);
}
if (request.message === 'onNovioTxRequest') {
chrome.runtime.sendMessage({
message: "transactionRequestData",
data: request.data
}, (result) => {
if (chrome.runtime.lastError) {
sendResponse({
error: 'cancelled'
});
} else {
sendResponse(result);
}
requestPopupWindow = null;
});
}
else if (request.message === 'onNovioSignRequest') {
chrome.runtime.sendMessage({
message: "signRequestData",
data: request.data
}, (result) => {
if (chrome.runtime.lastError) {
sendResponse({
error: 'cancelled'
});
} else {
if (requestPopupWindow) {
chrome.windows.remove(requestPopupWindow.id);
requestPopupWindow = null;
}
sendResponse(result);
}
requestPopupWindow = null;
});
}
}, 50);
})
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}