about summary refs log tree commit diff stats
path: root/src/pages/popup/popup.js
blob: cfb7be277a3232223945f8864cc460574afac670 (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
"use strict"
window.browser = window.browser || window.chrome

import servicesHelper from "../../assets/javascripts/services.js"
import utils from "../../assets/javascripts/utils.js"

document.getElementById("more-options").href = browser.runtime.getURL("pages/options/index.html")
document.getElementById("more-options").setAttribute('target', '_blank')

await browser.runtime.getPlatformInfo(r => {
	switch (r.os) {
		case "fuchsia":
		case "ios":
		case "android": {
			document.getElementsByTagName("html")[0].classList.add("mobile")
		}
	}
}
)


const allSites = document.getElementById("all_sites")
const currSite = document.getElementById("current_site")
const currentSiteDivider = document.getElementById("current_site_divider")

const config = await utils.getConfig()
const divs = {}

for (const service in config.services) {
	divs[service] = {}

	divs[service].all = allSites.getElementsByClassName(service)[0]
	divs[service].current = currSite.getElementsByClassName(service)[0]

	divs[service].all_toggle = allSites.getElementsByClassName(`${service}-enabled`)[0]
	divs[service].all_toggle.addEventListener("change", async () => {
		const options = await utils.getOptions()
		options[service].enabled = divs[service].all_toggle.checked
		browser.storage.local.set({ options })
	})

	allSites.getElementsByClassName(`${service}-change_instance`)[0].addEventListener("click", () => {
		browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
			if (tabs[0].url) {
				const url = new URL(tabs[0].url)
				browser.tabs.update({ url: await servicesHelper.switchInstance(url, service) })
			}
		})
	})

	divs[service].current_toggle = currSite.getElementsByClassName(`${service}-enabled`)[0]
	divs[service].current_toggle.addEventListener("change", async () => {
		const options = await utils.getOptions()
		options[service].enabled = divs[service].current_toggle.checked
		browser.storage.local.set({ options })
	})

	currSite.getElementsByClassName(`${service}-change_instance`)[0].addEventListener("click", () => {
		browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
			if (tabs[0].url) {
				const url = new URL(tabs[0].url)
				browser.tabs.update({ url: await servicesHelper.switchInstance(url, service) })
			}
		})
	})
}

browser.tabs.query({ active: true, currentWindow: true }, async tabs => {
	let url;

	// Set visibility of control buttons
	if (tabs[0].url) {
		const hr = document.getElementById("hr")
		url = new URL(tabs[0].url)
		servicesHelper.switchInstance(url).then(r => {
			if (r) {
				document.getElementById("change_instance_div").style.display = ""
				hr.style.display = ""
				document.getElementById("change_instance").addEventListener("click", async () =>
					browser.tabs.update({ url: await servicesHelper.switchInstance(url) })
				)
			}
		})
		servicesHelper.copyRaw(url, true).then(r => {
			if (r) {
				document.getElementById("copy_original_div").style.display = ""
				hr.style.display = ""
				document.getElementById("copy_original").addEventListener("click", () =>
					servicesHelper.copyRaw(url)
				)
			}
		})
		servicesHelper.reverse(url).then(r => {
			if (r) {
				document.getElementById("redirect_to_original_div").style.display = ""
				hr.style.display = ""
				document.getElementById("redirect_to_original").addEventListener("click", () =>
					browser.runtime.sendMessage("reverseTab")
				)
			}
		})
		servicesHelper.redirectAsync(url, "main_frame", null, true).then(r => {
			if (r) {
				document.getElementById("redirect_div").style.display = ""
				hr.style.display = ""
				document.getElementById("redirect").addEventListener("click", () =>
					browser.runtime.sendMessage("redirectTab")
				)
			}
		})
	}

	const options = await utils.getOptions()

	// Set visibility of all service buttons
	for (const service of options.popupServices) {
		divs[service].all.classList.remove("hide")
		divs[service].all_toggle.checked = options[service].enabled
	}

	// Set visibility of current page service button
	if (url) {
		const service = await servicesHelper.computeService(url)
		if (service) {
			divs[service].all.classList.add("hide")
			divs[service].current.classList.remove("hide")
			divs[service].current_toggle.checked = options[service].enabled
			currentSiteDivider.style.display = ""
		}
	}
})