-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
66 lines (62 loc) · 1.98 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
const nets = [
{ content: "mainnet", description: "use mainnet", id: 1 },
{ content: "sepolia", description: "use sepolia", id: 11155111 },
];
chrome.runtime.onInstalled.addListener(() => {
console.log("web3url extension installed and service worker started.");
});
chrome.omnibox.onInputChanged.addListener(function (text, suggest) {
// 设置默认建议,显示在下拉菜单的最上面
chrome.omnibox.setDefaultSuggestion({
description: "please check net to jump",
});
// 在建议中包含额外的参数
suggest(
nets.map(({ content, description }) => ({
content: `${content}.${text}`,
description,
}))
);
});
chrome.omnibox.onInputEntered.addListener((input, disposition) => {
const [network, ...extraParam] = input.split(".");
const url = extraParam.join(".");
let networkId = nets.find(({ content }) => content === network)?.id;
let networkSuffix = `.${networkId}.w3link.io`;
if (!networkId) {
chrome.notifications.create({
type: "basic",
iconUrl: "icon.png",
title: "Alarm",
message: "not match chain,please try again",
});
return;
}
console.log("networkId: " + networkId);
console.log("networkSuffix: " + networkSuffix);
let newUrl = "";
const [address, ...otherParams] = url.split("/");
console.log("address: ", address);
console.log("otherParams: ", otherParams);
if (address.startsWith("0x")) {
newUrl = `https://${address}${networkSuffix}/` + otherParams.join("/");
} else {
newUrl =
`https://${address.replace(".eth", ".w3eth.io")}/` +
otherParams.join("/");
}
chrome.tabs.update({ url: newUrl });
// if (newUrl) {
// switch (disposition) {
// case "currentTab":
// chrome.tabs.update({ url: newUrl });
// break;
// case "newForegroundTab":
// chrome.tabs.create({ url: newUrl });
// break;
// case "newBackgroundTab":
// chrome.tabs.create({ url: newUrl, active: false });
// break;
// }
// }
});