summaryrefslogtreecommitdiffstats
path: root/src/models/UserAgent.ts
blob: d54286ee8845565309cf39b08c26695f2d532b0e (plain) (blame)
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
112
113
114
115
116
117
118
import { action, computed, makeObservable, observable, observe } from 'mobx';

import ElectronWebView from 'react-electron-web-view';
import defaultUserAgent from '../helpers/userAgent-helpers';

const debug = require('../preload-safe-debug')('Ferdium:UserAgent');

export default class UserAgent {
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  _willNavigateListener = (_event: any): void => {};

  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  _didNavigateListener = (_event: any): void => {};

  @observable.ref webview: ElectronWebView = null;

  @observable chromelessUserAgent: boolean = false;

  @observable userAgentPref: string | null = null;

  @observable overrideUserAgent = (): string => '';

  constructor(overrideUserAgent: any = null) {
    makeObservable(this);

    if (typeof overrideUserAgent === 'function') {
      this.overrideUserAgent = overrideUserAgent;
    }

    observe(this, 'webview', change => {
      const { oldValue, newValue } = change;
      if (oldValue !== null) {
        this._removeWebviewEvents(oldValue);
      }
      if (newValue !== null) {
        this._addWebviewEvents(newValue);
      }
    });
  }

  @computed get defaultUserAgent(): string {
    const replacedUserAgent = this.overrideUserAgent();
    if (replacedUserAgent.length > 0) {
      return replacedUserAgent;
    }

    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(): string | null {
    if (typeof this.userAgentPref === 'string') {
      const trimmed = this.userAgentPref.trim();
      if (trimmed !== '') {
        return trimmed;
      }
    }
    return null;
  }

  @computed get userAgentWithoutChromeVersion(): string {
    const withChrome = this.defaultUserAgent;
    return withChrome.replace(/Chrome\/[\d.]+/, 'Chrome');
  }

  @computed get userAgent(): string {
    return (
      this.serviceUserAgentPref ||
      (this.chromelessUserAgent
        ? this.userAgentWithoutChromeVersion
        : this.defaultUserAgent)
    );
  }

  @action setWebviewReference(webview: ElectronWebView): void {
    this.webview = webview;
  }

  @action _handleNavigate(url: string, forwardingHack: boolean = false): void {
    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: ElectronWebView): void {
    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: ElectronWebView): void {
    debug('Removing event handlers');

    webview.removeEventListener('will-navigate', this._willNavigateListener);
    webview.removeEventListener('did-navigate', this._didNavigateListener);
  }
}