-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
115 lines (103 loc) · 3.58 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
const replacements = [
// Ethereum
{
pattern: /^https:\/\/etherscan\.io\/$/,
replacement: 'https://app.ondora.xyz'
},
{
pattern: /https:\/\/etherscan\.io\/block\/(\d+)/,
replacement: 'https://app.ondora.xyz/network/ethereum/blocks/$1'
},
{
pattern: /https:\/\/etherscan\.io\/tx\/(.+)/,
replacement: 'https://app.ondora.xyz/network/ethereum/interactions/$1'
},
{
pattern: /https:\/\/etherscan\.io\/address\/(.+)/,
replacement: 'https://app.ondora.xyz/network/ethereum/accounts/$1'
},
// Base-Testnet
{
pattern: /^https:\/\/goerli\.basescan\.org\/$/,
replacement: 'https://app.ondora.xyz/network/base-testnet/'
}, {
pattern: /https:\/\/goerli\.basescan\.org\/block\/(\d+)/,
replacement: 'https://app.ondora.xyz/network/base-testnet/blocks/$1'
}, {
pattern: /https:\/\/goerli\.basescan\.org\/tx\/(.+)/,
replacement: 'https://app.ondora.xyz/network/base-testnet/interactions/$1'
}, {
pattern: /https:\/\/goerli\.basescan\.org\/address\/(.+)/,
replacement: 'https://app.ondora.xyz/network/base-testnet/accounts/$1'
},
// Gnosis
{
pattern: /^https:\/\/gnosisscan\.io\/$/,
replacement: 'https://app.ondora.xyz/network/gnosis'
}, {
pattern: /https:\/\/gnosisscan\.io\/block\/(\d+)/,
replacement: 'https://app.ondora.xyz/network/gnosis/blocks/$1'
}, {
pattern: /https:\/\/gnosisscan\.io\/tx\/(.+)/,
replacement: 'https://app.ondora.xyz/network/gnosis/interactions/$1'
}, {
pattern: /https:\/\/gnosisscan\.io\/address\/(.+)/,
replacement: 'https://app.ondora.xyz/network/gnosis/accounts/$1'
},
// Scroll
{
pattern: /^https:\/\/scrollscan\.co\/$/,
replacement: 'https://app.ondora.xyz/network/scroll-zkp'
}, {
pattern: /https:\/\/scrollscan\.co\/blocks\/(\d+)/,
replacement: 'https://app.ondora.xyz/network/scroll-zkp/blocks/$1'
}, {
pattern: /https:\/\/scrollscan\.co\/tx\/(.+)/,
replacement: 'https://app.ondora.xyz/network/scroll-zkp/interactions/$1'
}, {
pattern: /https:\/\/scrollscan\.co\/address\/(.+)/,
replacement: 'https://app.ondora.xyz/network/scroll-zkp/accounts/$1'
}
];
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({ isEnabled: true });
chrome.action.setBadgeText({ text: 'Y' });
chrome.action.setBadgeBackgroundColor({ color: '#0F0' });
});
function replaceURL(tabId, url) {
chrome.storage.sync.get(['isEnabled'], function(result) {
if (result.isEnabled) {
for (const { pattern, replacement } of replacements) {
if (pattern.test(url)) {
const newUrl = url.replace(pattern, replacement);
chrome.tabs.update(tabId, { url: newUrl });
return;
}
}
}
});
}
chrome.runtime.onMessage.addListener((message, sender) => {
if (message.url) {
replaceURL(sender.tab.id, message.url);
}
});
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url && tab.active) {
replaceURL(tabId, changeInfo.url);
}
});
chrome.action.onClicked.addListener(() => {
chrome.storage.sync.get(['isEnabled'], function(result) {
const newIsEnabled = !result.isEnabled;
chrome.storage.sync.set({ isEnabled: newIsEnabled });
// Set the badge text
chrome.action.setBadgeText({
text: newIsEnabled ? 'Y' : 'N'
});
// Set the badge background color
chrome.action.setBadgeBackgroundColor({
color: newIsEnabled ? '#0F0' : '#F00'
});
});
});