about summary refs log tree commit diff stats
path: root/src/assets/javascripts/utils.js
blob: c8a771d965b2b1d203c787962ca480bb93a59eb1 (plain) (blame)
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
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() {
	return new Promise(resolve => {
		const http = new XMLHttpRequest()
		http.open("GET", "https://raw.githubusercontent.com/libredirect/instances/main/blacklist.json", true)
		http.onreadystatechange = () => {
			if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) {
				resolve(JSON.parse(http.responseText))
				return
			}
		}
		http.send(null)
	})
}

function getList() {
	return new Promise(resolve => {
		const http = new XMLHttpRequest()
		http.open("GET", "https://raw.githubusercontent.com/libredirect/instances/main/data.json", true)
		http.onreadystatechange = () => {
			if (http.status === 200 && http.readyState == XMLHttpRequest.DONE) {
				resolve(JSON.parse(http.responseText))
				return
			}
		}
		http.send(null)
	})

}

export default {
	getRandomInstance,
	protocolHost,
	getList,
	getBlacklist,
	camelCase,
	getConfig,
	getOptions
}