From c5a6623e0f320b2a789e0d2667ccfe2e1bd4335f Mon Sep 17 00:00:00 2001 From: ManeraKai Date: Mon, 30 May 2022 01:02:59 +0300 Subject: Fixed settings theme and translation flashing #286 --- src/pages/options/init.js | 56 ++++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 27 deletions(-) (limited to 'src/pages/options/init.js') diff --git a/src/pages/options/init.js b/src/pages/options/init.js index c9bf2f00..daea2963 100644 --- a/src/pages/options/init.js +++ b/src/pages/options/init.js @@ -1,36 +1,38 @@ window.browser = window.browser || window.chrome; -import utils from "../../assets/javascripts/helpers/utils.js"; + +import localise from "../../assets/javascripts/localise.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 { + return new Promise(resolve => { + browser.storage.local.get( + "theme", + r => { + switch (r.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"); + } + } + resolve(); } - } - ) + ) + }) } -changeTheme() - -browser.storage.onChanged.addListener(changeTheme) - -window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", changeTheme) +changeTheme(); +if (["ar", "iw", "ku", "fa", "ur"].includes(browser.i18n.getUILanguage())) document.getElementsByTagName("body")[0].classList.add("rtl"); +localise.localisePage(); -if (utils.isRtl()) document.getElementsByTagName("body")[0].classList.add("rtl"); \ No newline at end of file +window.matchMedia("(prefers-color-scheme: dark)").addEventListener("change", changeTheme) \ No newline at end of file -- cgit 1.4.1