diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-25 00:01:18 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-25 00:01:18 +0100 |
commit | e321534fbea9f09b139d440584f6b84ad0afb80f (patch) | |
tree | c4b4df109589475dd5f40a0d31f47a6aa9e43195 /packages/service-preload/src | |
parent | feat: Shim userAgentData in all frames and workers (diff) | |
download | sophie-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')
-rw-r--r-- | packages/service-preload/src/index.ts | 21 |
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 | ||
21 | import { ipcRenderer } from 'electron'; | 21 | import { ipcRenderer, webFrame } from 'electron'; |
22 | import { ServiceToMainIpcMessage } from '@sophie/service-shared'; | 22 | import { ServiceToMainIpcMessage, webSource } from '@sophie/service-shared'; |
23 | 23 | ||
24 | ipcRenderer.send(ServiceToMainIpcMessage.ApiExposedInMainWorld); | 24 | if (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 | |||
30 | const injectSource = webSource.safeParse(ipcRenderer.sendSync(ServiceToMainIpcMessage.ApiExposedInMainWorld)); | ||
31 | if (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 | } | ||