Change: detect media change in content script

This commit is contained in:
eight 2019-09-25 14:29:48 +08:00
parent a0004bb6fd
commit f5e8666c23
3 changed files with 15 additions and 7 deletions

View File

@ -1,6 +1,7 @@
/* global download prefs openURL FIREFOX CHROME VIVALDI /* global download prefs openURL FIREFOX CHROME VIVALDI
debounce URLS ignoreChromeError getTab debounce URLS ignoreChromeError getTab
styleManager msg navigatorUtil iconUtil workerUtil contentScripts */ styleManager msg navigatorUtil iconUtil workerUtil contentScripts
colorScheme */
'use strict'; 'use strict';
// eslint-disable-next-line no-var // eslint-disable-next-line no-var
@ -63,7 +64,9 @@ window.API_METHODS = Object.assign(window.API_METHODS || {}, {
return browser.runtime.openOptionsPage() return browser.runtime.openOptionsPage()
.then(() => new Promise(resolve => setTimeout(resolve, 100))) .then(() => new Promise(resolve => setTimeout(resolve, 100)))
.then(() => msg.broadcastExtension({method: 'optionsCustomizeHotkeys'})); .then(() => msg.broadcastExtension({method: 'optionsCustomizeHotkeys'}));
} },
updateSystemPreferDark: colorScheme.updateSystemPreferDark
}); });
// eslint-disable-next-line no-var // eslint-disable-next-line no-var

View File

@ -8,9 +8,6 @@ const colorScheme = (() => {
let timePreferDark = false; let timePreferDark = false;
const changeListeners = new Set(); const changeListeners = new Set();
const media = window.matchMedia('(prefers-color-scheme: dark)');
media.addListener(updateSystemPreferDark);
const checkTime = ['schemeSwitcher.nightStart', 'schemeSwitcher.nightEnd']; const checkTime = ['schemeSwitcher.nightStart', 'schemeSwitcher.nightEnd'];
prefs.subscribe(checkTime, (key, value) => { prefs.subscribe(checkTime, (key, value) => {
updateTimePreferDark(); updateTimePreferDark();
@ -29,7 +26,7 @@ const colorScheme = (() => {
updateSystemPreferDark(); updateSystemPreferDark();
updateTimePreferDark(); updateTimePreferDark();
return {shouldIncludeStyle, onChange}; return {shouldIncludeStyle, onChange, updateSystemPreferDark};
function createAlarm(key, value) { function createAlarm(key, value) {
const date = new Date(); const date = new Date();
@ -63,10 +60,11 @@ const colorScheme = (() => {
function updateSystemPreferDark() { function updateSystemPreferDark() {
const oldValue = systemPreferDark; const oldValue = systemPreferDark;
systemPreferDark = media.matches; systemPreferDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (systemPreferDark !== oldValue) { if (systemPreferDark !== oldValue) {
emitChange(); emitChange();
} }
return true;
} }
function updateTimePreferDark() { function updateTimePreferDark() {

View File

@ -46,6 +46,13 @@ const APPLY = (() => {
prefs.subscribe(['exposeIframes'], updateExposeIframes); prefs.subscribe(['exposeIframes'], updateExposeIframes);
} }
// detect media change in content script
// FIXME: move this to background page when following bugs are fixed:
// https://bugzilla.mozilla.org/show_bug.cgi?id=1561546
// https://bugs.chromium.org/p/chromium/issues/detail?id=968651
const media = window.matchMedia('(prefers-color-scheme: dark)');
media.addListener(() => API.updateSystemPreferDark().catch(console.error));
function onInjectorUpdate() { function onInjectorUpdate() {
if (!IS_OWN_PAGE && styleInjector.list.length) { if (!IS_OWN_PAGE && styleInjector.list.length) {
docRewriteObserver.start(); docRewriteObserver.start();