blob: d357badcc4b77e5e6beb66d962c8c7675824cc55 [file] [log] [blame]
Adrià Vilanova Martínez27c69962021-07-17 23:32:51 +02001import {createExtBadge} from './utils/common.js';
Adrià Vilanova Martínez3465e772021-07-11 19:18:41 +02002
3export function injectDarkModeButton(rightControl, previousDarkModeOption) {
4 var darkThemeSwitch = document.createElement('material-button');
5 darkThemeSwitch.classList.add('TWPT-dark-theme', 'TWPT-btn--with-badge');
6 darkThemeSwitch.setAttribute('button', '');
7 darkThemeSwitch.setAttribute(
8 'title', chrome.i18n.getMessage('inject_ccdarktheme_helper'));
9
10 darkThemeSwitch.addEventListener('click', e => {
11 chrome.storage.sync.get(null, currentOptions => {
12 currentOptions.ccdarktheme_switch_status = !previousDarkModeOption;
13 chrome.storage.sync.set(currentOptions, _ => {
14 location.reload();
15 });
16 });
17 });
18
19 var switchContent = document.createElement('div');
20 switchContent.classList.add('content');
21
22 var icon = document.createElement('material-icon');
23
24 var i = document.createElement('i');
25 i.classList.add('material-icon-i', 'material-icons-extended');
26 i.textContent = 'brightness_4';
27
28 icon.appendChild(i);
29 switchContent.appendChild(icon);
30 darkThemeSwitch.appendChild(switchContent);
31
32 var badgeContent = createExtBadge();
33
34 darkThemeSwitch.appendChild(badgeContent);
35
36 rightControl.style.width =
37 (parseInt(window.getComputedStyle(rightControl).width) + 58) + 'px';
38 rightControl.insertAdjacentElement('afterbegin', darkThemeSwitch);
39}
40
41export function isDarkThemeOn(options) {
42 if (!options.ccdarktheme) return false;
43
44 if (options.ccdarktheme_mode == 'switch')
45 return options.ccdarktheme_switch_status;
46
47 return window.matchMedia &&
48 window.matchMedia('(prefers-color-scheme: dark)').matches;
49}