-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
146 lines (132 loc) · 3.41 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
146
let GLOBAL_LIST_DOMAINS = {}
// https://bugs.chromium.org/p/chromium/issues/detail?id=1271154
let lifeline
keepAlive()
chrome.runtime.onConnect.addListener(port => {
if (port.name === 'keepAlive') {
lifeline = port
setTimeout(keepAliveForced, 295e3) // 5 minutes minus 5 seconds
port.onDisconnect.addListener(keepAliveForced)
}
})
function keepAliveForced() {
lifeline?.disconnect()
lifeline = null
keepAlive()
}
async function keepAlive() {
if (lifeline) return
for (const tab of await chrome.tabs.query({ url: '*://*/*' })) {
try {
await chrome.scripting.executeScript({
target: { tabId: tab.id },
function: () => chrome.runtime.connect({ name: 'keepAlive' }),
// `function` will become `func` in Chrome 93+
})
chrome.tabs.onUpdated.removeListener(retryOnTabUpdate)
return
} catch (e) {}
}
chrome.tabs.onUpdated.addListener(retryOnTabUpdate)
}
async function retryOnTabUpdate(tabId, info, tab) {
if (info.url && /^(file|https?):/.test(info.url)) {
keepAlive()
}
}
chrome.webRequest.onBeforeRequest.addListener(
function (info) {
const domain = info.url.match(
/[=\/]([a-z0-9-]{1,63}\.(ava|web3|dapp|ipfs|defi|gamefi))[\/&$]/i
)
if (domain) {
if (typeof GLOBAL_LIST_DOMAINS[domain[1]] !== 'undefined') {
if (!GLOBAL_LIST_DOMAINS[domain[1]]) return
chrome.storage.sync.get('ipfsGatawey', data => {
chrome.tabs.update(undefined, {
url: `https://${data.ipfsGatawey}/ipfs/${GLOBAL_LIST_DOMAINS[domain[1]]}`,
})
})
} else {
chrome.tabs.update(undefined, {
url: 'blank.html',
})
const loading = setTimeout(() => {
chrome.tabs.update(undefined, {
url: 'loading.html',
})
}, 2000)
const url =
'https://deep-index.moralis.io/api/v2/0xdD7a57EeF66251Bc77B8cC03faBFAcC026099b65/function?chain=avalanche%20testnet&function_name=getIPFS'
const myHeaders = new Headers()
myHeaders.append('accept', 'application/json')
myHeaders.append(
'X-API-Key',
'FXurtm7Fhf6M0YmkGlwakLNDZ8tpaFaajhR5nz2KcaZoj3Rz1dRgocNxBU3I33oK'
)
myHeaders.append('Content-Type', 'application/json')
const raw = JSON.stringify({
abi: [
{
inputs: [
{
internalType: 'string',
name: '_domain',
type: 'string',
},
],
name: 'getIPFS',
outputs: [
{
internalType: 'string',
name: '',
type: 'string',
},
],
stateMutability: 'view',
type: 'function',
},
],
params: {
_domain: domain[1],
},
})
const requestOptions = {
method: 'POST',
headers: myHeaders,
body: raw,
redirect: 'follow',
}
fetch(url, requestOptions)
.then(response => response.text())
.then(result => {
clearTimeout(loading)
if (!result) {
chrome.tabs.update(undefined, {
url: 'not.html',
})
} else {
result = result.toString().replace(/[^a-z0-9]/gi, '')
if (!result) {
chrome.tabs.update(undefined, {
url: 'not.html',
})
} else {
GLOBAL_LIST_DOMAINS[domain[1]] = result
chrome.storage.sync.get('ipfsGatawey', data => {
chrome.tabs.update(undefined, {
url: `https://${data.ipfsGatawey}/ipfs/${result}`,
})
})
}
}
})
.catch(error => console.log('error', error))
}
}
},
{
urls: ['<all_urls>'],
},
[]
)