about summary refs log tree commit diff stats
path: root/src/pages/popup/popup.js
blob: 942d0e28cb555e7904680eddbc93735c0ec4203d (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
"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").addEventListener("click", () => browser.runtime.openOptionsPage())

const allSites = document.getElementsByClassName("all_sites")[0]
const currSite = document.getElementsByClassName("current_site")[0]
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].current_toggle = currSite.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 })
	})
	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 })
	})
}

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

	// Set visibility of control buttons
	if (tabs[0].url) {
		url = new URL(tabs[0].url)
		servicesHelper.switchInstance(url).then(r => {
			if (r) {
				document.getElementById("change_instance_div").style.display = "block"
				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 = "block"
				document.getElementById("copy_original").addEventListener("click", () =>
					servicesHelper.copyRaw(url)
				)
			}
		})
		servicesHelper.reverse(url).then(r => {
			if (r) {
				document.getElementById("redirect_to_original_div").style.display = "block"
				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 = "block"
				document.getElementById("redirect").addEventListener("click", () =>
					browser.runtime.sendMessage("redirectTab")
				)
			}
		})
	}

	const options = await utils.getOptions()

	// Set visibility of all service buttons
	for (let i = 0; i < options.popupServices.length; ++i) {
		const service = options.popupServices[i]
		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 = "block"
		}
	}
})

for (const a of document.getElementsByTagName("a")) {
	a.addEventListener("click", e => {
		if (!a.classList.contains("prevent")) {
			browser.tabs.create({ url: a.getAttribute("href") })
			e.preventDefault()
		}
	})
}