about summary refs log tree commit diff stats
path: root/src/pages/options/index.js
blob: 6fb8e452454b07e56053590ef371a342d1ffa569 (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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import utils from "../../assets/javascripts/utils.js"
import localise from "../../assets/javascripts/localise.js"

let config,
	options,
	divs = {}

for (const a of document.getElementById("links").getElementsByTagName("a")) {
	a.addEventListener("click", e => {
		const path = a.getAttribute("href").replace("#", "")
		loadPage(path)
		e.preventDefault()
	})
}

await new Promise(resolve => {
	fetch("/config.json").then(response => response.text())
		.then(data => {
			config = JSON.parse(data)
			resolve()
		})
})
await new Promise(resolve => {
	browser.storage.local.get("options", r => {
		options = r.options
		resolve()
	})
})

function changeFrontendsSettings(service) {
	for (const frontend in config.services[service].frontends) {
		if (config.services[service].frontends[frontend].instanceList) {
			const frontendDiv = document.getElementById(frontend)
			if (typeof divs[service].frontend !== "undefined") {
				if (frontend == divs[service].frontend.value) {
					frontendDiv.style.display = "block"
				} else {
					frontendDiv.style.display = "none"
				}
			}
		}
	}
}

function loadPage(path) {
	for (const section of document.getElementById("pages").getElementsByTagName("section")) section.style.display = "none"
	document.getElementById(`${path}_page`).style.display = "block"

	for (const a of document.getElementById("links").getElementsByTagName("a"))
		if (a.getAttribute("href") == `#${path}`) a.classList.add("selected")
		else a.classList.remove("selected")

	let stateObj = { id: "100" }
	window.history.pushState(stateObj, "Page 2", `/pages/options/index.html#${path}`)

	if (path != 'general' && path != 'about') {
		const service = path;

		divs[service] = {}
		for (const option in config.services[service].options) {
			divs[service][option] = document.getElementById(`${service}-${option}`)
			if (typeof config.services[service].options[option] == "boolean") divs[service][option].checked = options[service][option]
			else divs[service][option].value = options[service][option]

			divs[service][option].addEventListener("change", () => {
				browser.storage.local.get("options", r => {
					let options = r.options
					if (typeof config.services[service].options[option] == "boolean") options[service][option] = divs[service][option].checked
					else options[service][option] = divs[service][option].value
					browser.storage.local.set({ options })
					changeFrontendsSettings(service)
				})
			})
		}

		if (Object.keys(config.services[service].frontends).length > 1) {
			changeFrontendsSettings(service)
		}

		for (const frontend in config.services[service].frontends) {
			if (config.services[service].frontends[frontend].instanceList) {
				processDefaultCustomInstances(frontend, config.networks, document)
			}
		}
	}
}


async function processDefaultCustomInstances(frontend, networks, document) {
	let customInstances = []
	let options
	await new Promise(async resolve =>
		browser.storage.local.get(["options"], r => {
			customInstances = r.options[frontend]
			options = r.options
			resolve()
		})
	)

	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() {
		document.getElementById(frontend).getElementsByClassName("custom-checklist")[0].innerHTML = customInstances
			.map(
				x => `
				<div>
                	${x}
					<button class="add clear-${x}">
						<svg xmlns="https://www.w3.org/2000/svg" height="20px" viewBox="0 0 24 24" width="20px" fill="currentColor">
							<path d="M19 6.41L17.59 5 12 10.59 6.41 5 5 6.41 10.59 12 5 17.59 6.41 19 12 13.41 17.59 19 19 17.59 13.41 12 19 6.41z" />
						</svg>
					</button>
              	</div>
              	<hr>`
			)
			.join("\n")

		for (const item of customInstances) {
			document.getElementById(frontend).getElementsByClassName(`clear-${item}`)[0].addEventListener("click", async () => {
				let index = customInstances.indexOf(item)
				if (index > -1) customInstances.splice(index, 1)
				options[frontend] = customInstances
				browser.storage.local.set({ options }, () => calcCustomInstances())
			})
		}
	}
	calcCustomInstances()
	document.getElementById(frontend).getElementsByClassName("custom-instance-form")[0].addEventListener("submit", async event => {
		event.preventDefault();
		event.preventDefault()
		let frontendCustomInstanceInput = document.getElementById(frontend).getElementsByClassName("custom-instance")[0]
		let url
		try {
			url = new URL(frontendCustomInstanceInput.value)
		} catch (error) {
			return
		}
		let protocolHostVar = utils.protocolHost(url)
		if (frontendCustomInstanceInput.validity.valid) {
			if (!customInstances.includes(protocolHostVar)) {
				customInstances.push(protocolHostVar)
				options[frontend] = customInstances
				browser.storage.local.set({ options }, () => {
					frontendCustomInstanceInput.value = ""
					calcCustomInstances()
				})
			}
		}
	})
}

const r = window.location.href.match(/#(.*)/)
if (r) loadPage(r[1])
else loadPage("general")