Skip to content

Commit eda730a

Browse files
Merge pull request #12625 from Snuffleupagus/viewerCssTheme-option
Add a new preference, `viewerCssTheme`, to allow forcing the use of the light/dark viewer CSS themes (issue 12290)
2 parents f39d87b + 40a4d53 commit eda730a

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

extensions/chromium/preferences_schema.json

+9
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@
198198
2
199199
],
200200
"default": -1
201+
},
202+
"viewerCssTheme": {
203+
"type": "integer",
204+
"enum": [
205+
0,
206+
1,
207+
2
208+
],
209+
"default": 0
201210
}
202211
}
203212
}

web/app.js

+47
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ const ViewOnLoad = {
9090
INITIAL: 1,
9191
};
9292

93+
const ViewerCssTheme = {
94+
AUTOMATIC: 0, // Default value.
95+
LIGHT: 1,
96+
DARK: 2,
97+
};
98+
9399
// Keep these in sync with mozilla-central's Histograms.json.
94100
const KNOWN_VERSIONS = [
95101
"1.0",
@@ -256,6 +262,7 @@ const PDFViewerApplication = {
256262

257263
await this._readPreferences();
258264
await this._parseHashParameters();
265+
this._forceCssTheme();
259266
await this._initializeL10n();
260267

261268
if (
@@ -396,6 +403,46 @@ const PDFViewerApplication = {
396403
document.getElementsByTagName("html")[0].dir = dir;
397404
},
398405

406+
/**
407+
* @private
408+
*/
409+
_forceCssTheme() {
410+
const cssTheme = AppOptions.get("viewerCssTheme");
411+
if (
412+
cssTheme === ViewerCssTheme.AUTOMATIC ||
413+
!Object.values(ViewerCssTheme).includes(cssTheme)
414+
) {
415+
return;
416+
}
417+
try {
418+
const styleSheet = document.styleSheets[0];
419+
const cssRules = styleSheet?.cssRules || [];
420+
for (let i = 0, ii = cssRules.length; i < ii; i++) {
421+
const rule = cssRules[i];
422+
if (
423+
rule instanceof CSSMediaRule &&
424+
rule.media?.[0] === "(prefers-color-scheme: dark)"
425+
) {
426+
if (cssTheme === ViewerCssTheme.LIGHT) {
427+
styleSheet.deleteRule(i);
428+
return;
429+
}
430+
// cssTheme === ViewerCssTheme.DARK
431+
const darkRules = /^@media \(prefers-color-scheme: dark\) {\n\s*([\w\s-.,:;/\\{}()]+)\n}$/.exec(
432+
rule.cssText
433+
);
434+
if (darkRules?.[1]) {
435+
styleSheet.deleteRule(i);
436+
styleSheet.insertRule(darkRules[1], i);
437+
}
438+
return;
439+
}
440+
}
441+
} catch (reason) {
442+
console.error(`_forceCssTheme: "${reason?.message}".`);
443+
}
444+
},
445+
399446
/**
400447
* @private
401448
*/

web/app_options.js

+5
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ const defaultOptions = {
154154
value: false,
155155
kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
156156
},
157+
viewerCssTheme: {
158+
/** @type {number} */
159+
value: 0,
160+
kind: OptionKind.VIEWER + OptionKind.PREFERENCE,
161+
},
157162
viewOnLoad: {
158163
/** @type {boolean} */
159164
value: 0,

0 commit comments

Comments
 (0)