aboutsummaryrefslogtreecommitdiffstats
path: root/packages/service-preload/src/index.ts
diff options
context:
space:
mode:
Diffstat (limited to 'packages/service-preload/src/index.ts')
-rw-r--r--packages/service-preload/src/index.ts57
1 files changed, 0 insertions, 57 deletions
diff --git a/packages/service-preload/src/index.ts b/packages/service-preload/src/index.ts
index 99d02ec..eee8551 100644
--- a/packages/service-preload/src/index.ts
+++ b/packages/service-preload/src/index.ts
@@ -18,67 +18,10 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import colorString from 'color-string';
22import { webFrame } from 'electron'; 21import { webFrame } from 'electron';
23// eslint-disable-next-line import/no-unresolved -- Synthetic import provided by an eslint plugin. 22// eslint-disable-next-line import/no-unresolved -- Synthetic import provided by an eslint plugin.
24import injectSource from 'sophie-src:@sophie/service-inject'; 23import injectSource from 'sophie-src:@sophie/service-inject';
25 24
26const DEFAULT_BG_COLOR = '#fff';
27
28/**
29 * Styles a HTML element such that its background is opaque.
30 *
31 * If there is an existing background color, it will be made maximally opaque.
32 *
33 * If there is a background image, transparent areas will be colored `DEFAULT_BG_COLOR`.
34 *
35 * If the element was completely transparent, the function returns `false` instead.
36 * This allows leaving a `html` element transparent to let the background of the `body`
37 * element render in its palce.
38 *
39 * @param element The HTML element to style.
40 * @returns `true` if the background was made opaque.
41 * @see https://www.w3.org/TR/css-backgrounds-3/#body-background
42 */
43function tryMakeOpaque(element: HTMLElement): boolean {
44 const style = getComputedStyle(element);
45 const bgColor = colorString.get.rgb(style.backgroundColor);
46 if (bgColor[3] > 0) {
47 if (bgColor[3] < 1) {
48 bgColor[3] = 1;
49 // eslint-disable-next-line no-param-reassign -- Deliberately add element style.
50 element.style.backgroundColor = colorString.to.rgb(bgColor);
51 }
52 return true;
53 }
54 if (style.backgroundImage !== 'none') {
55 // eslint-disable-next-line no-param-reassign -- Deliberately add element style.
56 element.style.backgroundColor = DEFAULT_BG_COLOR;
57 return true;
58 }
59 return false;
60}
61
62if (webFrame.parent === null) {
63 // Inject CSS to simulate `browserView.setBackgroundColor`.
64 // This is injected before the page loads, so the styles from the website will overwrite it.
65 document.addEventListener('DOMContentLoaded', () => {
66 if (
67 document.documentElement.style.contain === '' &&
68 document.body.style.contain === ''
69 ) {
70 if (
71 !tryMakeOpaque(document.documentElement) &&
72 !tryMakeOpaque(document.body)
73 ) {
74 document.body.style.backgroundColor = DEFAULT_BG_COLOR;
75 }
76 } else if (!tryMakeOpaque(document.documentElement)) {
77 document.documentElement.style.backgroundColor = DEFAULT_BG_COLOR;
78 }
79 });
80}
81
82/** 25/**
83 * Executes the service inject script in the isolated world. 26 * Executes the service inject script in the isolated world.
84 * 27 *