about summary refs log tree commit diff stats
path: root/src/pages/options/init.js
blob: c9bf2f001d83db09107a648fe77d85270db0cbc0 (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
window.browser = window.browser || window.chrome;
import utils from "../../assets/javascripts/helpers/utils.js";

function changeTheme() {
    browser.storage.local.get(
        "theme",
        result => {
            switch (result.theme) {
                case "dark":
                    document.body.classList.add("dark-theme");
                    document.body.classList.remove("light-theme");
                    break;
                case "light":
                    document.body.classList.add("light-theme");
                    document.body.classList.remove("dark-theme");
                    break;
                default:
                    if (matchMedia("(prefers-color-scheme: light)").matches) {
                        document.body.classList.add("light-theme");
                        document.body.classList.remove("dark-theme");
                    } else {
                        document.body.classList.add("dark-theme");
                        document.body.classList.remove("light-theme");
                    }
            }
        }
    )
}

changeTheme()

browser.storage.onChanged.addListener(changeTheme)

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

if (utils.isRtl()) document.getElementsByTagName("body")[0].classList.add("rtl");