diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-06-03 19:01:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-03 19:01:01 +0200 |
commit | 2ad39ffb1cb0d0e5f79d6948f798ca79ed73c76c (patch) | |
tree | dcb679119cf4963126a3520b7c62ae4b032e0225 /src/models/UserAgent.js | |
parent | Upgraded electron to '13.1.0'. (diff) | |
download | ferdium-app-2ad39ffb1cb0d0e5f79d6948f798ca79ed73c76c.tar.gz ferdium-app-2ad39ffb1cb0d0e5f79d6948f798ca79ed73c76c.tar.zst ferdium-app-2ad39ffb1cb0d0e5f79d6948f798ca79ed73c76c.zip |
* Expose Chrome version to todos webview (fix #1211)
The TickTick todo service fails to load if the Chrome version number
does not appear in the User-Agent string. However, login to Google Tasks
is prevented by the same.
We adopt the "chromeless" User-Agent logic from the service webview,
which selectively exposes the Chrome version everywhere except the
Google login screen. The common logic was moved into the
userAgent-helpers module.
* Refactor user agent switching
* "Chromeless" user agent switching is extracted into a separate model
* Both the service and the todos webview uses the same model
Diffstat (limited to 'src/models/UserAgent.js')
-rw-r--r-- | src/models/UserAgent.js | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/models/UserAgent.js b/src/models/UserAgent.js new file mode 100644 index 000000000..f51f2e5a6 --- /dev/null +++ b/src/models/UserAgent.js | |||
@@ -0,0 +1,80 @@ | |||
1 | import { | ||
2 | action, | ||
3 | computed, | ||
4 | observe, | ||
5 | observable, | ||
6 | } from 'mobx'; | ||
7 | |||
8 | import defaultUserAgent, { isChromeless } from '../helpers/userAgent-helpers'; | ||
9 | |||
10 | const debug = require('debug')('Ferdi:UserAgent'); | ||
11 | |||
12 | export default class UserAgent { | ||
13 | _willNavigateListener = null; | ||
14 | |||
15 | _didNavigateListener = null; | ||
16 | |||
17 | @observable.ref webview = null; | ||
18 | |||
19 | @observable chromelessUserAgent = false; | ||
20 | |||
21 | @observable getUserAgent = defaultUserAgent; | ||
22 | |||
23 | constructor(overrideUserAgent = null) { | ||
24 | if (typeof overrideUserAgent === 'function') { | ||
25 | this.getUserAgent = 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 userAgent() { | ||
40 | return this.chromelessUserAgent ? defaultUserAgent(true) : this.getUserAgent(); | ||
41 | } | ||
42 | |||
43 | @action setWebviewReference(webview) { | ||
44 | this.webview = webview; | ||
45 | } | ||
46 | |||
47 | @action _handleNavigate(url, forwardingHack = false) { | ||
48 | if (isChromeless(url)) { | ||
49 | if (!this.chromelessUserAgent) { | ||
50 | debug('Setting user agent to chromeless for url', url); | ||
51 | this.chromelessUserAgent = true; | ||
52 | this.webview.userAgent = this.userAgent; | ||
53 | if (forwardingHack) { | ||
54 | this.webview.loadURL(url); | ||
55 | } | ||
56 | } | ||
57 | } else if (this.chromelessUserAgent) { | ||
58 | debug('Setting user agent to contain chrome for url', url); | ||
59 | this.chromelessUserAgent = false; | ||
60 | this.webview.userAgent = this.userAgent; | ||
61 | } | ||
62 | } | ||
63 | |||
64 | _addWebviewEvents(webview) { | ||
65 | debug('Adding event handlers'); | ||
66 | |||
67 | this._willNavigateListener = event => this._handleNavigate(event.url, true); | ||
68 | webview.addEventListener('will-navigate', this._willNavigateListener); | ||
69 | |||
70 | this._didNavigateListener = event => this._handleNavigate(event.url); | ||
71 | webview.addEventListener('did-navigate', this._didNavigateListener); | ||
72 | } | ||
73 | |||
74 | _removeWebviewEvents(webview) { | ||
75 | debug('Removing event handlers'); | ||
76 | |||
77 | webview.removeEventListener('will-navigate', this._willNavigateListener); | ||
78 | webview.removeEventListener('did-navigate', this._didNavigateListener); | ||
79 | } | ||
80 | } | ||