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.js | |
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.js')
-rw-r--r-- | src/models/UserAgent.js | 111 |
1 files changed, 0 insertions, 111 deletions
diff --git a/src/models/UserAgent.js b/src/models/UserAgent.js deleted file mode 100644 index 3e1394b45..000000000 --- a/src/models/UserAgent.js +++ /dev/null | |||
@@ -1,111 +0,0 @@ | |||
1 | import { action, computed, observe, observable } from 'mobx'; | ||
2 | |||
3 | import defaultUserAgent from '../helpers/userAgent-helpers'; | ||
4 | |||
5 | const debug = require('../preload-safe-debug')('Ferdium:UserAgent'); | ||
6 | |||
7 | export default class UserAgent { | ||
8 | _willNavigateListener = null; | ||
9 | |||
10 | _didNavigateListener = null; | ||
11 | |||
12 | @observable.ref webview = null; | ||
13 | |||
14 | @observable chromelessUserAgent = false; | ||
15 | |||
16 | @observable userAgentPref = null; | ||
17 | |||
18 | @observable getUserAgent = null; | ||
19 | |||
20 | constructor(overrideUserAgent = null) { | ||
21 | if (typeof overrideUserAgent === 'function') { | ||
22 | this.getUserAgent = overrideUserAgent; | ||
23 | } | ||
24 | |||
25 | observe(this, 'webview', change => { | ||
26 | const { oldValue, newValue } = change; | ||
27 | if (oldValue !== null) { | ||
28 | this._removeWebviewEvents(oldValue); | ||
29 | } | ||
30 | if (newValue !== null) { | ||
31 | this._addWebviewEvents(newValue); | ||
32 | } | ||
33 | }); | ||
34 | } | ||
35 | |||
36 | @computed get defaultUserAgent() { | ||
37 | if (typeof this.getUserAgent === 'function') { | ||
38 | return this.getUserAgent(); | ||
39 | } | ||
40 | const globalPref = window['ferdium'].stores.settings.all.app.userAgentPref; | ||
41 | if (typeof globalPref === 'string') { | ||
42 | const trimmed = globalPref.trim(); | ||
43 | if (trimmed !== '') { | ||
44 | return trimmed; | ||
45 | } | ||
46 | } | ||
47 | return defaultUserAgent(); | ||
48 | } | ||
49 | |||
50 | @computed get serviceUserAgentPref() { | ||
51 | if (typeof this.userAgentPref === 'string') { | ||
52 | const trimmed = this.userAgentPref.trim(); | ||
53 | if (trimmed !== '') { | ||
54 | return trimmed; | ||
55 | } | ||
56 | } | ||
57 | return null; | ||
58 | } | ||
59 | |||
60 | @computed get userAgentWithoutChromeVersion() { | ||
61 | const withChrome = this.defaultUserAgent; | ||
62 | return withChrome.replace(/Chrome\/[\d.]+/, 'Chrome'); | ||
63 | } | ||
64 | |||
65 | @computed get userAgent() { | ||
66 | return ( | ||
67 | this.serviceUserAgentPref || | ||
68 | (this.chromelessUserAgent | ||
69 | ? this.userAgentWithoutChromeVersion | ||
70 | : this.defaultUserAgent) | ||
71 | ); | ||
72 | } | ||
73 | |||
74 | @action setWebviewReference(webview) { | ||
75 | this.webview = webview; | ||
76 | } | ||
77 | |||
78 | @action _handleNavigate(url, forwardingHack = false) { | ||
79 | if (url.startsWith('https://accounts.google.com')) { | ||
80 | if (!this.chromelessUserAgent) { | ||
81 | debug('Setting user agent to chromeless for url', url); | ||
82 | this.chromelessUserAgent = true; | ||
83 | this.webview.userAgent = this.userAgent; | ||
84 | if (forwardingHack) { | ||
85 | this.webview.loadURL(url); | ||
86 | } | ||
87 | } | ||
88 | } else if (this.chromelessUserAgent) { | ||
89 | debug('Setting user agent to contain chrome for url', url); | ||
90 | this.chromelessUserAgent = false; | ||
91 | this.webview.userAgent = this.userAgent; | ||
92 | } | ||
93 | } | ||
94 | |||
95 | _addWebviewEvents(webview) { | ||
96 | debug('Adding event handlers'); | ||
97 | |||
98 | this._willNavigateListener = event => this._handleNavigate(event.url, true); | ||
99 | webview.addEventListener('will-navigate', this._willNavigateListener); | ||
100 | |||
101 | this._didNavigateListener = event => this._handleNavigate(event.url); | ||
102 | webview.addEventListener('did-navigate', this._didNavigateListener); | ||
103 | } | ||
104 | |||
105 | _removeWebviewEvents(webview) { | ||
106 | debug('Removing event handlers'); | ||
107 | |||
108 | webview.removeEventListener('will-navigate', this._willNavigateListener); | ||
109 | webview.removeEventListener('did-navigate', this._didNavigateListener); | ||
110 | } | ||
111 | } | ||