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