Optimized the getList and getBlacklist functions
This commit is contained in:
parent
91528e2192
commit
486b92af5f
@ -624,7 +624,7 @@ function processUpdate() {
|
||||
fetch("/config.json")
|
||||
.then(response => response.text())
|
||||
.then(configData => {
|
||||
browser.storage.local.get(["options", "targets"], r => {
|
||||
browser.storage.local.get(["options", "targets"], async r => {
|
||||
let redirects = JSON.parse(data)
|
||||
let options = r.options
|
||||
let targets = r.targets
|
||||
@ -647,7 +647,7 @@ function processUpdate() {
|
||||
if (!options[frontend]) {
|
||||
options[frontend] = []
|
||||
if (network == "clearnet") {
|
||||
for (const blacklist of getBlacklist()) {
|
||||
for (const blacklist of await utils.getBlacklist()) {
|
||||
for (const instance of blacklist) {
|
||||
let i = options[frontend].clearnet.enabled.indexOf(instance)
|
||||
if (i > -1) options[frontend].clearnet.enabled.splice(i, 1)
|
||||
|
@ -71,17 +71,32 @@ function switchInstance(test) {
|
||||
}
|
||||
|
||||
function getBlacklist() {
|
||||
let http = new XMLHttpRequest()
|
||||
http.open("GET", "https://codeberg.org/LibRedirect/libredirect/raw/branch/master/src/instances/blacklist.json", false)
|
||||
http.send(null)
|
||||
return JSON.parse(http.responseText)
|
||||
return new Promise(resolve => {
|
||||
const http = new XMLHttpRequest()
|
||||
http.open("GET", "https://codeberg.org/LibRedirect/libredirect/raw/branch/master/src/instances/blacklist.json", true)
|
||||
http.onreadystatechange = () => {
|
||||
if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) {
|
||||
resolve(JSON.parse(http.responseText))
|
||||
return
|
||||
}
|
||||
}
|
||||
http.send(null)
|
||||
})
|
||||
}
|
||||
|
||||
function getList() {
|
||||
let http = new XMLHttpRequest()
|
||||
http.open("GET", "https://codeberg.org/LibRedirect/libredirect/raw/branch/master/src/instances/data.json", false)
|
||||
http.send(null)
|
||||
return JSON.parse(http.responseText)
|
||||
return new Promise(resolve => {
|
||||
const http = new XMLHttpRequest()
|
||||
http.open("GET", "https://codeberg.org/LibRedirect/libredirect/raw/branch/master/src/instances/data.json", true)
|
||||
http.onreadystatechange = () => {
|
||||
if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) {
|
||||
resolve(JSON.parse(http.responseText))
|
||||
return
|
||||
}
|
||||
}
|
||||
http.send(null)
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
export default {
|
||||
|
@ -86,13 +86,23 @@ function loadPage(path) {
|
||||
|
||||
for (const frontend in config.services[service].frontends) {
|
||||
if (config.services[service].frontends[frontend].instanceList) {
|
||||
processDefaultCustomInstances(frontend, config.networks, document)
|
||||
processDefaultCustomInstances(frontend, document)
|
||||
}
|
||||
}
|
||||
|
||||
!async function () {
|
||||
const blacklist = await utils.getBlacklist()
|
||||
const redirects = await utils.getList()
|
||||
for (const frontend in config.services[service].frontends) {
|
||||
if (config.services[service].frontends[frontend].instanceList) {
|
||||
createList(frontend, config.networks, document, redirects, blacklist)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
async function processDefaultCustomInstances(frontend, networks, document) {
|
||||
async function processDefaultCustomInstances(frontend, document) {
|
||||
let customInstances = []
|
||||
let options
|
||||
await new Promise(async resolve =>
|
||||
@ -103,42 +113,6 @@ async function processDefaultCustomInstances(frontend, networks, document) {
|
||||
})
|
||||
)
|
||||
|
||||
!async function () {
|
||||
const blacklist = utils.getBlacklist()
|
||||
const redirects = utils.getList()
|
||||
|
||||
for (const network in networks) {
|
||||
if (redirects[frontend][network].length > 0) {
|
||||
document.getElementById(frontend).getElementsByClassName(network)[0].getElementsByClassName("checklist")[0].innerHTML = [
|
||||
`
|
||||
<div class="some-block option-block">
|
||||
<h4>${utils.camelCase(network)}</h4>
|
||||
</div>
|
||||
`,
|
||||
...redirects[frontend][network]
|
||||
.sort((a, b) =>
|
||||
(blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b))
|
||||
||
|
||||
(blacklist.authenticate.includes(a) && !blacklist.authenticate.includes(b))
|
||||
)
|
||||
.map(x => {
|
||||
const cloudflare = blacklist.cloudflare.includes(x) ? ' <span style="color:red;">cloudflare</span>' : ""
|
||||
const authenticate = blacklist.authenticate.includes(x) ? ' <span style="color:orange;">authenticate</span>' : ""
|
||||
|
||||
let warnings = [cloudflare, authenticate].join(" ")
|
||||
return `
|
||||
<div>
|
||||
<x>
|
||||
<a href="${x}" target="_blank">${x}</a>${warnings}
|
||||
</x>
|
||||
</div>`
|
||||
}),
|
||||
'<br>'
|
||||
].join("\n<hr>\n")
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
localise.localisePage()
|
||||
|
||||
function calcCustomInstances() {
|
||||
@ -191,6 +165,40 @@ async function processDefaultCustomInstances(frontend, networks, document) {
|
||||
})
|
||||
}
|
||||
|
||||
function createList(frontend, networks, document, redirects, blacklist) {
|
||||
for (const network in networks) {
|
||||
if (redirects[frontend][network].length > 0) {
|
||||
document.getElementById(frontend).getElementsByClassName(network)[0].getElementsByClassName("checklist")[0].innerHTML = [
|
||||
`
|
||||
<div class="some-block option-block">
|
||||
<h4>${utils.camelCase(network)}</h4>
|
||||
</div>
|
||||
`,
|
||||
...redirects[frontend][network]
|
||||
.sort((a, b) =>
|
||||
(blacklist.cloudflare.includes(a) && !blacklist.cloudflare.includes(b))
|
||||
||
|
||||
(blacklist.authenticate.includes(a) && !blacklist.authenticate.includes(b))
|
||||
)
|
||||
.map(x => {
|
||||
const cloudflare = blacklist.cloudflare.includes(x) ? ' <span style="color:red;">cloudflare</span>' : ""
|
||||
const authenticate = blacklist.authenticate.includes(x) ? ' <span style="color:orange;">authenticate</span>' : ""
|
||||
|
||||
let warnings = [cloudflare, authenticate].join(" ")
|
||||
return `
|
||||
<div>
|
||||
<x>
|
||||
<a href="${x}" target="_blank">${x}</a>${warnings}
|
||||
</x>
|
||||
</div>`
|
||||
}),
|
||||
'<br>'
|
||||
].join("\n<hr>\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
const r = window.location.href.match(/#(.*)/)
|
||||
if (r) loadPage(r[1])
|
||||
else loadPage("general")
|
||||
else loadPage("general")
|
Loading…
x
Reference in New Issue
Block a user