aboutsummaryrefslogtreecommitdiffstats
path: root/packages/service-preload/src/index.ts
diff options
context:
space:
mode:
authorLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-25 00:01:18 +0100
committerLibravatar Kristóf Marussy <kristof@marussy.com>2021-12-25 00:01:18 +0100
commite321534fbea9f09b139d440584f6b84ad0afb80f (patch)
treec4b4df109589475dd5f40a0d31f47a6aa9e43195 /packages/service-preload/src/index.ts
parentfeat: Shim userAgentData in all frames and workers (diff)
downloadsophie-e321534fbea9f09b139d440584f6b84ad0afb80f.tar.gz
sophie-e321534fbea9f09b139d440584f6b84ad0afb80f.tar.zst
sophie-e321534fbea9f09b139d440584f6b84ad0afb80f.zip
refactor: Simplify script injection
Inject CSS and main world scripts synchronously to avoid race conditions with page loading. Don't try to miming userAgentData for now, since it won't bypass google's checks. However, simply omitting chrome from the user agent does bypass them, at least for now.
Diffstat (limited to 'packages/service-preload/src/index.ts')
-rw-r--r--packages/service-preload/src/index.ts21
1 files changed, 18 insertions, 3 deletions
diff --git a/packages/service-preload/src/index.ts b/packages/service-preload/src/index.ts
index 3f54c0b..e42c406 100644
--- a/packages/service-preload/src/index.ts
+++ b/packages/service-preload/src/index.ts
@@ -18,7 +18,22 @@
18 * SPDX-License-Identifier: AGPL-3.0-only 18 * SPDX-License-Identifier: AGPL-3.0-only
19 */ 19 */
20 20
21import { ipcRenderer } from 'electron'; 21import { ipcRenderer, webFrame } from 'electron';
22import { ServiceToMainIpcMessage } from '@sophie/service-shared'; 22import { ServiceToMainIpcMessage, webSource } from '@sophie/service-shared';
23 23
24ipcRenderer.send(ServiceToMainIpcMessage.ApiExposedInMainWorld); 24if (webFrame.parent === null) {
25 // Inject CSS to simulate `browserView.setBackgroundColor`.
26 // This is injected before the page loads, so the styles from the website will overwrite it.
27 webFrame.insertCSS('html { background-color: #fff; }');
28}
29
30const injectSource = webSource.safeParse(ipcRenderer.sendSync(ServiceToMainIpcMessage.ApiExposedInMainWorld));
31if (injectSource.success) {
32 webFrame.executeJavaScriptInIsolatedWorld(0, [
33 injectSource.data,
34 ]).catch((err) => {
35 console.log('Failed to inject source:', err);
36 });
37} else {
38 console.log('Invalid source to inject:', injectSource.error);
39}