From a8253a9f6e5cd30760c3fcbf05767e7276b191c1 Mon Sep 17 00:00:00 2001 From: Vijay Raghavan Aravamudhan Date: Thu, 17 Jun 2021 14:17:37 +0000 Subject: User agent as a user-preference (#1535) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Introduced a global and a service-specific 'user-agent' value that can be set by the user from the preference section. The global override is in Advanced, while the service override was moved to the bottom of the service editor form (and now occupies the full width of the form). Show 'restart reqd' prompt for user-agent setting. The order of the user-agent is the following: 1. Any request header overrides in the recipe will still override the user agent string. 2. If the user has set a user agent override for the service, use that. 3. If the recipe has a predefined user agent, use that. 4. If the user has set a global user agent override, use that. 5. Otherwise, use the user agent string corresponding to the underlying Electron version. If the current webpage is the Google login form, we still remove the Chrome version number from the user agent string. The value that would be used if no custom override is set (recipe-provided value or global override for the service, current Electron instance for the global override) is shown as a placeholder in the input box on the corresponding setting screen. Co-authored-by: Kristóf Marussy --- src/models/Service.js | 18 ++++++++++++++++-- src/models/UserAgent.js | 35 +++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 4 deletions(-) (limited to 'src/models') diff --git a/src/models/Service.js b/src/models/Service.js index d0c6a7103..50115f605 100644 --- a/src/models/Service.js +++ b/src/models/Service.js @@ -107,6 +107,8 @@ export default class Service { return null; } + this.userAgentModel = new UserAgent(recipe.overrideUserAgent); + this.id = data.id || this.id; this.name = data.name || this.name; this.team = data.team || this.team; @@ -141,6 +143,8 @@ export default class Service { this.spellcheckerLanguage = data.spellcheckerLanguage !== undefined ? data.spellcheckerLanguage : this.spellcheckerLanguage; + this.userAgentPref = data.userAgentPref !== undefined ? data.userAgentPref : this.userAgentPref; + this.isHibernationEnabled = data.isHibernationEnabled !== undefined ? data.isHibernationEnabled : this.isHibernationEnabled; this.recipe = recipe; @@ -156,8 +160,6 @@ export default class Service { this.isHibernating = true; } - this.userAgentModel = new UserAgent(recipe.overrideUserAgent); - autorun(() => { if (!this.isEnabled) { this.webview = null; @@ -243,6 +245,18 @@ export default class Service { return this.userAgentModel.userAgent; } + @computed get userAgentPref() { + return this.userAgentModel.userAgentPref; + } + + set userAgentPref(pref) { + this.userAgentModel.userAgentPref = pref; + } + + @computed get defaultUserAgent() { + return this.userAgentModel.defaultUserAgent; + } + @computed get partition() { return this.recipe.partition || `persist:service-${this.id}`; } diff --git a/src/models/UserAgent.js b/src/models/UserAgent.js index f51f2e5a6..f1d08e306 100644 --- a/src/models/UserAgent.js +++ b/src/models/UserAgent.js @@ -18,7 +18,9 @@ export default class UserAgent { @observable chromelessUserAgent = false; - @observable getUserAgent = defaultUserAgent; + @observable userAgentPref = null; + + @observable getUserAgent = null; constructor(overrideUserAgent = null) { if (typeof overrideUserAgent === 'function') { @@ -36,8 +38,37 @@ export default class UserAgent { }); } + @computed get defaultUserAgent() { + if (typeof this.getUserAgent === 'function') { + return this.getUserAgent(); + } + const globalPref = window.ferdi.stores.settings.all.app.userAgentPref; + if (typeof globalPref === 'string') { + const trimmed = globalPref.trim(); + if (trimmed !== '') { + return trimmed; + } + } + return defaultUserAgent(); + } + + @computed get userAgentWithChromeVersion() { + if (typeof this.userAgentPref === 'string') { + const trimmed = this.userAgentPref.trim(); + if (trimmed !== '') { + return trimmed; + } + } + return this.defaultUserAgent; + } + + @computed get userAgentWithoutChromeVersion() { + const withChrome = this.userAgentWithChromeVersion; + return withChrome.replace(/Chrome\/[0-9.]+/, 'Chrome'); + } + @computed get userAgent() { - return this.chromelessUserAgent ? defaultUserAgent(true) : this.getUserAgent(); + return this.chromelessUserAgent ? this.userAgentWithoutChromeVersion : this.userAgentWithChromeVersion; } @action setWebviewReference(webview) { -- cgit v1.2.3-54-g00ecf