diff options
author | Ricardo Cino <ricardo@cino.io> | 2022-06-23 18:10:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-23 16:10:39 +0000 |
commit | 6b2c2b8dfb86245a1747bf7977159f5129461863 (patch) | |
tree | 28944f62a962d8a658262ea902f8554d4419fa9e /src/models/UserAgent.ts | |
parent | chore: featureStore and GlobalErrorStore JS => TS (diff) | |
download | ferdium-app-6b2c2b8dfb86245a1747bf7977159f5129461863.tar.gz ferdium-app-6b2c2b8dfb86245a1747bf7977159f5129461863.tar.zst ferdium-app-6b2c2b8dfb86245a1747bf7977159f5129461863.zip |
chore: servicesStore + models into typescript (#344)
Diffstat (limited to 'src/models/UserAgent.ts')
-rw-r--r-- | src/models/UserAgent.ts | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/models/UserAgent.ts b/src/models/UserAgent.ts new file mode 100644 index 000000000..1d06d72b0 --- /dev/null +++ b/src/models/UserAgent.ts | |||
@@ -0,0 +1,116 @@ | |||
1 | import { action, computed, observe, observable } from 'mobx'; | ||
2 | |||
3 | import ElectronWebView from 'react-electron-web-view'; | ||
4 | import defaultUserAgent from '../helpers/userAgent-helpers'; | ||
5 | |||
6 | const debug = require('../preload-safe-debug')('Ferdium:UserAgent'); | ||
7 | |||
8 | export default class UserAgent { | ||
9 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
10 | _willNavigateListener = (_event: any): void => {}; | ||
11 | |||
12 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
13 | _didNavigateListener = (_event: any): void => {}; | ||
14 | |||
15 | @observable.ref webview: ElectronWebView = null; | ||
16 | |||
17 | @observable chromelessUserAgent: boolean = false; | ||
18 | |||
19 | @observable userAgentPref: string | null = null; | ||
20 | |||
21 | @observable overrideUserAgent = (): string => ''; | ||
22 | |||
23 | constructor(overrideUserAgent: any = null) { | ||
24 | if (typeof overrideUserAgent === 'function') { | ||
25 | this.overrideUserAgent = overrideUserAgent; | ||
26 | } | ||
27 | |||
28 | observe(this, 'webview', change => { | ||
29 | const { oldValue, newValue } = change; | ||
30 | if (oldValue !== null) { | ||
31 | this._removeWebviewEvents(oldValue); | ||
32 | } | ||
33 | if (newValue !== null) { | ||
34 | this._addWebviewEvents(newValue); | ||
35 | } | ||
36 | }); | ||
37 | } | ||
38 | |||
39 | @computed get defaultUserAgent(): string { | ||
40 | const replacedUserAgent = this.overrideUserAgent(); | ||
41 | if (replacedUserAgent.length > 0) { | ||
42 | return replacedUserAgent; | ||
43 | } | ||
44 | |||
45 | const globalPref = window['ferdium'].stores.settings.all.app.userAgentPref; | ||
46 | if (typeof globalPref === 'string') { | ||
47 | const trimmed = globalPref.trim(); | ||
48 | if (trimmed !== '') { | ||
49 | return trimmed; | ||
50 | } | ||
51 | } | ||
52 | return defaultUserAgent(); | ||
53 | } | ||
54 | |||
55 | @computed get serviceUserAgentPref(): string | null { | ||
56 | if (typeof this.userAgentPref === 'string') { | ||
57 | const trimmed = this.userAgentPref.trim(); | ||
58 | if (trimmed !== '') { | ||
59 | return trimmed; | ||
60 | } | ||
61 | } | ||
62 | return null; | ||
63 | } | ||
64 | |||
65 | @computed get userAgentWithoutChromeVersion(): string { | ||
66 | const withChrome = this.defaultUserAgent; | ||
67 | return withChrome.replace(/Chrome\/[\d.]+/, 'Chrome'); | ||
68 | } | ||
69 | |||
70 | @computed get userAgent(): string { | ||
71 | return ( | ||
72 | this.serviceUserAgentPref || | ||
73 | (this.chromelessUserAgent | ||
74 | ? this.userAgentWithoutChromeVersion | ||
75 | : this.defaultUserAgent) | ||
76 | ); | ||
77 | } | ||
78 | |||
79 | @action setWebviewReference(webview: ElectronWebView): void { | ||
80 | this.webview = webview; | ||
81 | } | ||
82 | |||
83 | @action _handleNavigate(url: string, forwardingHack: boolean = false): void { | ||
84 | if (url.startsWith('https://accounts.google.com')) { | ||
85 | if (!this.chromelessUserAgent) { | ||
86 | debug('Setting user agent to chromeless for url', url); | ||
87 | this.chromelessUserAgent = true; | ||
88 | this.webview.userAgent = this.userAgent; | ||
89 | if (forwardingHack) { | ||
90 | this.webview.loadURL(url); | ||
91 | } | ||
92 | } | ||
93 | } else if (this.chromelessUserAgent) { | ||
94 | debug('Setting user agent to contain chrome for url', url); | ||
95 | this.chromelessUserAgent = false; | ||
96 | this.webview.userAgent = this.userAgent; | ||
97 | } | ||
98 | } | ||
99 | |||
100 | _addWebviewEvents(webview: ElectronWebView): void { | ||
101 | debug('Adding event handlers'); | ||
102 | |||
103 | this._willNavigateListener = event => this._handleNavigate(event.url, true); | ||
104 | webview.addEventListener('will-navigate', this._willNavigateListener); | ||
105 | |||
106 | this._didNavigateListener = event => this._handleNavigate(event.url); | ||
107 | webview.addEventListener('did-navigate', this._didNavigateListener); | ||
108 | } | ||
109 | |||
110 | _removeWebviewEvents(webview: ElectronWebView): void { | ||
111 | debug('Removing event handlers'); | ||
112 | |||
113 | webview.removeEventListener('will-navigate', this._willNavigateListener); | ||
114 | webview.removeEventListener('did-navigate', this._didNavigateListener); | ||
115 | } | ||
116 | } | ||