diff options
Diffstat (limited to 'packages')
-rw-r--r-- | packages/main/src/stores/MainStore.ts | 2 | ||||
-rw-r--r-- | packages/service-preload/package.json | 4 | ||||
-rw-r--r-- | packages/service-preload/src/index.ts | 53 |
3 files changed, 57 insertions, 2 deletions
diff --git a/packages/main/src/stores/MainStore.ts b/packages/main/src/stores/MainStore.ts index bf351d7..21d7a63 100644 --- a/packages/main/src/stores/MainStore.ts +++ b/packages/main/src/stores/MainStore.ts | |||
@@ -87,7 +87,7 @@ const MainStore = types | |||
87 | self.settings.setShowLocationBar(action.showLocationBar); | 87 | self.settings.setShowLocationBar(action.showLocationBar); |
88 | break; | 88 | break; |
89 | case 'reload-all-services': | 89 | case 'reload-all-services': |
90 | // TODO | 90 | self.services.forEach((service) => service.reload()); |
91 | break; | 91 | break; |
92 | case 'dispatch-service-action': { | 92 | case 'dispatch-service-action': { |
93 | const { serviceId, serviceAction } = action; | 93 | const { serviceId, serviceAction } = action; |
diff --git a/packages/service-preload/package.json b/packages/service-preload/package.json index 1cfa74c..2090cf9 100644 --- a/packages/service-preload/package.json +++ b/packages/service-preload/package.json | |||
@@ -9,6 +9,10 @@ | |||
9 | }, | 9 | }, |
10 | "dependencies": { | 10 | "dependencies": { |
11 | "@sophie/service-shared": "workspace:*", | 11 | "@sophie/service-shared": "workspace:*", |
12 | "color-string": "^1.9.0", | ||
12 | "electron": "17.1.0" | 13 | "electron": "17.1.0" |
14 | }, | ||
15 | "devDependencies": { | ||
16 | "@types/color-string": "^1" | ||
13 | } | 17 | } |
14 | } | 18 | } |
diff --git a/packages/service-preload/src/index.ts b/packages/service-preload/src/index.ts index fb19107..a49a3a4 100644 --- a/packages/service-preload/src/index.ts +++ b/packages/service-preload/src/index.ts | |||
@@ -19,12 +19,63 @@ | |||
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { ServiceToMainIpcMessage, WebSource } from '@sophie/service-shared'; | 21 | import { ServiceToMainIpcMessage, WebSource } from '@sophie/service-shared'; |
22 | import colorString from 'color-string'; | ||
22 | import { ipcRenderer, webFrame } from 'electron'; | 23 | import { ipcRenderer, webFrame } from 'electron'; |
23 | 24 | ||
25 | const DEFAULT_BG_COLOR = '#fff'; | ||
26 | |||
27 | /** | ||
28 | * Styles a HTML element such that its background is opaque. | ||
29 | * | ||
30 | * If there is an existing background color, it will be made maximally opaque. | ||
31 | * | ||
32 | * If there is a background image, transparent areas will be colored `DEFAULT_BG_COLOR`. | ||
33 | * | ||
34 | * If the element was completely transparent, the function returns `false` instead. | ||
35 | * This allows leaving a `html` element transparent to let the background of the `body` | ||
36 | * element render in its palce. | ||
37 | * | ||
38 | * @param element The HTML element to style. | ||
39 | * @returns `true` if the background was made opaque. | ||
40 | * @see https://www.w3.org/TR/css-backgrounds-3/#body-background | ||
41 | */ | ||
42 | function tryMakeOpaque(element: HTMLElement): boolean { | ||
43 | const style = getComputedStyle(element); | ||
44 | const bgColor = colorString.get.rgb(style.backgroundColor); | ||
45 | if (bgColor[3] > 0) { | ||
46 | if (bgColor[3] < 1) { | ||
47 | bgColor[3] = 1; | ||
48 | // eslint-disable-next-line no-param-reassign -- Deliberately add element style. | ||
49 | element.style.backgroundColor = colorString.to.rgb(bgColor); | ||
50 | } | ||
51 | return true; | ||
52 | } | ||
53 | if (style.backgroundImage !== 'none') { | ||
54 | // eslint-disable-next-line no-param-reassign -- Deliberately add element style. | ||
55 | element.style.backgroundColor = DEFAULT_BG_COLOR; | ||
56 | return true; | ||
57 | } | ||
58 | return false; | ||
59 | } | ||
60 | |||
24 | if (webFrame.parent === null) { | 61 | if (webFrame.parent === null) { |
25 | // Inject CSS to simulate `browserView.setBackgroundColor`. | 62 | // Inject CSS to simulate `browserView.setBackgroundColor`. |
26 | // This is injected before the page loads, so the styles from the website will overwrite it. | 63 | // This is injected before the page loads, so the styles from the website will overwrite it. |
27 | webFrame.insertCSS('body { background-color: #fff; }'); | 64 | document.addEventListener('DOMContentLoaded', () => { |
65 | if ( | ||
66 | document.documentElement.style.contain === '' && | ||
67 | document.body.style.contain === '' | ||
68 | ) { | ||
69 | if ( | ||
70 | !tryMakeOpaque(document.documentElement) && | ||
71 | !tryMakeOpaque(document.body) | ||
72 | ) { | ||
73 | document.body.style.backgroundColor = DEFAULT_BG_COLOR; | ||
74 | } | ||
75 | } else if (!tryMakeOpaque(document.documentElement)) { | ||
76 | document.documentElement.style.backgroundColor = DEFAULT_BG_COLOR; | ||
77 | } | ||
78 | }); | ||
28 | } | 79 | } |
29 | 80 | ||
30 | /** | 81 | /** |