Repository navigation
Expand file tree
/
Copy pathbackground.js
More file actions
78 lines (64 loc) · 1.86 KB
/
Copy pathbackground.js
File metadata and controls
78 lines (64 loc) · 1.86 KB
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
const BLOCKSCOUT_HOST = "robinhoodchain.blockscout.com";
const ROBINSCAN_HOST = "robinscan.io";
const REDIRECT_RULES = {
tx: /\/tx\/([a-fA-F0-9x]+)/,
block: /\/block\/(\d+)/,
token: /\/token\/([a-fA-F0-9x]+)/,
address: /\/address\/([a-fA-F0-9x]+)/,
batches: /\/batches\/(\d+)/,
};
function getRedirectUrl(url) {
let parsed;
try {
parsed = new URL(url);
} catch {
return null;
}
if (parsed.hostname !== BLOCKSCOUT_HOST) return null;
for (const [type, pattern] of Object.entries(REDIRECT_RULES)) {
const match = parsed.pathname.match(pattern);
if (match) {
const newPath = parsed.pathname.replace(pattern, `/${type}/${match[1]}`);
return `${parsed.protocol}//${ROBINSCAN_HOST}${newPath}${parsed.search}${parsed.hash}`;
}
}
return null;
}
function isEnabled(type) {
return new Promise((resolve) => {
chrome.storage.sync.get("settings", (data) => {
const settings = data.settings || {};
resolve(settings[type] !== false);
});
});
}
async function handleNavigation(details) {
if (details.frameId !== 0) return;
const redirectUrl = getRedirectUrl(details.url);
if (!redirectUrl) return;
const type = Object.keys(REDIRECT_RULES).find((key) => {
const pattern = REDIRECT_RULES[key];
return pattern.test(new URL(details.url).pathname);
});
if (!type) return;
if (!await isEnabled(type)) return;
chrome.tabs.update(details.tabId, { url: redirectUrl });
}
chrome.webNavigation.onBeforeNavigate.addListener(handleNavigation, {
url: [{ hostEquals: BLOCKSCOUT_HOST }],
});
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.get("settings", (data) => {
if (!data.settings) {
chrome.storage.sync.set({
settings: {
tx: true,
block: true,
token: true,
address: true,
batches: true,
},
});
}
});
});