about summary refs log tree commit diff stats
path: root/src/pages/options/init.js
blob: 07da1859ae5d0ab5af71fa567b0913dd98bc67b0 (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
window.browser = window.browser || window.chrome

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

if (!(await utils.getOptions())) {
  await servicesHelper.initDefaults()
}

async function changeTheme() {
  switch ((await utils.getOptions()).theme) {
    case "dark":
      document.body.classList.add("dark-theme")
      document.body.classList.remove("light-theme")
      for (const element of document.body.getElementsByClassName("dark")) {
        element.style.display = "none"
      }
      break
    case "light":
      document.body.classList.add("light-theme")
      document.body.classList.remove("dark-theme")
      for (const element of document.body.getElementsByClassName("light")) {
        element.style.display = "none"
      }
      break
    default:
      if (matchMedia("(prefers-color-scheme: light)").matches) {
        document.body.classList.add("light-theme")
        document.body.classList.remove("dark-theme")
        for (const element of document.body.getElementsByClassName("light")) {
          element.style.display = "none"
        }
      } else {
        document.body.classList.add("dark-theme")
        document.body.classList.remove("light-theme")
        for (const element of document.body.getElementsByClassName("dark")) {
          element.style.display = "none"
        }
      }
  }
}

changeTheme()
if (["ar", "iw", "ku", "fa", "ur"].includes(browser.i18n.getUILanguage())) {
  document.getElementsByTagName("body")[0].classList.add("rtl")
  document.getElementsByTagName("body")[0].dir = "rtl"
}
localise.localisePage()

window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", changeTheme)