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
147
148
149
150
151
152
|
window.browser = window.browser || window.chrome
function getRandomInstance(instances) {
return instances[~~(instances.length * Math.random())]
}
function camelCase(str) {
return str.charAt(0).toUpperCase() + str.slice(1)
}
function protocolHost(url) {
if (url.username && url.password) return `${url.protocol}//${url.username}:${url.password}@${url.host}`
return `${url.protocol}//${url.host}`
}
function getConfig() {
return new Promise(resolve => {
fetch("/config.json")
.then(response => response.text())
.then(json => {
resolve(JSON.parse(json))
return
})
})
}
function getOptions() {
return new Promise(resolve =>
browser.storage.local.get("options", r => {
resolve(r.options)
})
)
}
function getBlacklist(options) {
return new Promise(resolve => {
let url
if (options.fetchInstances == 'github') {
url = 'https://raw.githubusercontent.com/libredirect/instances/main/blacklist.json'
}
else if (options.fetchInstances == 'codeberg') {
url = 'https://codeberg.org/LibRedirect/instances/raw/branch/main/blacklist.json'
}
else {
resolve('disabled')
return
}
const http = new XMLHttpRequest()
http.open("GET", url, true)
http.onreadystatechange = () => {
if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) {
resolve(JSON.parse(http.responseText))
return
}
}
http.onerror = () => {
resolve()
return
}
http.ontimeout = () => {
resolve()
return
}
http.send(null)
})
}
function getList(options) {
return new Promise(resolve => {
let url
if (options.fetchInstances == 'github') {
url = 'https://raw.githubusercontent.com/libredirect/instances/main/data.json'
}
else if (options.fetchInstances == 'codeberg') {
url = 'https://codeberg.org/LibRedirect/instances/raw/branch/main/data.json'
}
else {
resolve('disabled')
return
}
const http = new XMLHttpRequest()
http.open("GET", url, true)
http.onreadystatechange = () => {
if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) {
resolve(JSON.parse(http.responseText))
return
}
}
http.onerror = () => {
resolve()
return
}
http.ontimeout = () => {
resolve()
return
}
http.send(null)
})
}
function pingOnce(href) {
return new Promise(async resolve => {
let started
let http = new XMLHttpRequest()
http.timeout = 5000
http.ontimeout = () => resolve(5000)
http.onerror = () => resolve()
http.onreadystatechange = () => {
if (http.readyState == 2) {
if (http.status == 200) {
let ended = new Date().getTime()
http.abort()
resolve(ended - started)
} else {
resolve(5000 + http.status)
}
}
}
http.open("GET", `${href}?_=${new Date().getTime()}`, true)
started = new Date().getTime()
http.send(null)
})
}
function ping(href) {
return new Promise(async resolve => {
let average = 0
let time
for (let i = 0; i < 3; i++) {
time = await pingOnce(href)
if (i == 0) continue
if (time >= 5000) {
resolve(time)
return
}
average += time
}
average = parseInt(average / 3)
resolve(average)
})
}
export default {
getRandomInstance,
protocolHost,
getList,
getBlacklist,
camelCase,
getConfig,
getOptions,
ping,
}
|