diff options
Diffstat (limited to 'packages/service-preload')
-rw-r--r-- | packages/service-preload/package.json | 3 | ||||
-rw-r--r-- | packages/service-preload/src/index.ts | 57 |
2 files changed, 1 insertions, 59 deletions
diff --git a/packages/service-preload/package.json b/packages/service-preload/package.json index 4650f10..28d1129 100644 --- a/packages/service-preload/package.json +++ b/packages/service-preload/package.json | |||
@@ -10,8 +10,7 @@ | |||
10 | "dependencies": { | 10 | "dependencies": { |
11 | "@sophie/service-inject": "workspace:*", | 11 | "@sophie/service-inject": "workspace:*", |
12 | "@sophie/service-shared": "workspace:*", | 12 | "@sophie/service-shared": "workspace:*", |
13 | "color-string": "^1.9.0", | 13 | "electron": "^19.0.0-alpha.1" |
14 | "electron": "18.0.3" | ||
15 | }, | 14 | }, |
16 | "devDependencies": { | 15 | "devDependencies": { |
17 | "@types/color-string": "^1.5.2" | 16 | "@types/color-string": "^1.5.2" |
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 | ||
21 | import colorString from 'color-string'; | ||
22 | import { webFrame } from 'electron'; | 21 | import { 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. |
24 | import injectSource from 'sophie-src:@sophie/service-inject'; | 23 | import injectSource from 'sophie-src:@sophie/service-inject'; |
25 | 24 | ||
26 | const 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 | */ | ||
43 | function 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 | |||
62 | if (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 | * |