forked from xian-network/xian-web-wallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
75 lines (65 loc) · 2.4 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
let appTabId = null;
// Listener for the extension icon click
chrome.action.onClicked.addListener(function(tab) {
const url = chrome.runtime.getURL('index.html');
findTab();
// Check if the tab is still open
if (appTabId !== null) {
chrome.tabs.get(appTabId, function(existingTab) {
if (chrome.runtime.lastError || !existingTab) {
// If the tab is no longer open or an error occurred, create a new tab
createTab();
} else {
// Focus on the existing tab
chrome.tabs.update(appTabId, {active: true});
}
});
} else {
// If no tab ID is stored, create a new tab
createTab();
}
});
// Function to create a new tab
function createTab() {
const url = chrome.runtime.getURL('index.html');
chrome.tabs.create({url: url}, function(newTab) {
// Update the global variable with the new tab ID
appTabId = newTab.id;
});
}
function findTab() {
chrome.tabs.query({url: chrome.runtime.getURL('index.html')}, function(tabs) {
if (tabs.length > 0) {
appTabId = tabs[0].id;
}
});
}
// Listener for messages from the content script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'dAppSendTransaction' || message.type === 'getWalletInfo' || message.type === 'dAppSignMessage') {
if(appTabId === null) {
findTab(); // Try to find the tab if the reference was lost
}
if (appTabId === null && message.type === 'getWalletInfo') { // If the extension is not open, return an empty response
sendResponse({address: '', locked: true, chainId: ''});
return;
}
// Forward the message to the extension window
chrome.tabs.sendMessage(appTabId, message, sendResponse);
}
// Make sure to return true to indicate that you will send a response asynchronously
return true;
});
// Listener for when a tab is removed
chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
if (tabId === appTabId) {
appTabId = null;
}
});
// Listener for when a tab is updated
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (tabId === appTabId && changeInfo.status === 'complete') {
// Update appTabId if needed, such as reloading the tab
appTabId = tabId;
}
});