Repository: manusa/electronim
Pull request #412 (2024-05-14 11:18:28)

♻️ user-agent for webContents
Allows easier transition to BaseWindow and Electron 30

sonarcloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=manusa_electronim&pullRequest=412) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=manusa_electronim&pullRequest=412&resolved=false&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/component_measures?id=manusa_electronim&pullRequest=412&metric=new_accepted_issues&view=list)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=manusa_electronim&pullRequest=412&resolved=false&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [100.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=manusa_electronim&pullRequest=412&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=manusa_electronim&pullRequest=412&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarCloud](https://sonarcloud.io/dashboard?id=manusa_electronim&pullRequest=412)



Patch:
diff --git a/src/main/index.js b/src/main/index.js
index c201ef6..b843670 100644
--- a/src/main/index.js
+++ b/src/main/index.js
@@ -28,7 +28,7 @@ const {
 } = require('../spell-check');
 const tabManager = require('../tab-manager');
 const {initTray} = require('../tray');
-const {initBrowserVersions, userAgentForView} = require('../user-agent');
+const {initBrowserVersions, userAgentForWebContents} = require('../user-agent');
 
 const webPreferences = {
   contextIsolation: false,
@@ -248,7 +248,7 @@ const initGlobalListeners = () => {
 };
 
 const browserVersionsReady = () => {
-  app.userAgentFallback = userAgentForView(mainWindow);
+  app.userAgentFallback = userAgentForWebContents(mainWindow.webContents);
   tabContainer = newTabContainer();
   appMenu = newAppMenu();
   eventBus.emit(APP_EVENTS.trayInit);
diff --git a/src/tab-manager/index.js b/src/tab-manager/index.js
index 14d4966..4a62a0c 100644
--- a/src/tab-manager/index.js
+++ b/src/tab-manager/index.js
@@ -18,7 +18,7 @@ const path = require('path');
 const {APP_EVENTS} = require('../constants');
 const {loadSettings, updateSettings} = require('../settings');
 const {getEnabledDictionaries, getUseNativeSpellChecker} = require('../spell-check');
-const {userAgentForView, addUserAgentInterceptor} = require('../user-agent');
+const {userAgentForWebContents, addUserAgentInterceptor} = require('../user-agent');
 const {handleContextMenu} = require('./context-menu');
 const {handleRedirect, windowOpenHandler} = require('./redirect');
 
@@ -60,7 +60,7 @@ const handlePageFaviconUpdated = (browserView, ipcSender, tabId) => async (_e, f
 const setGlobalUserAgentFallback = userAgent => (app.userAgentFallback = userAgent);
 
 const cleanUserAgent = browserView => {
-  const validUserAgent = userAgentForView(browserView);
+  const validUserAgent = userAgentForWebContents(browserView.webContents);
   browserView.webContents.userAgent = validUserAgent;
   setGlobalUserAgentFallback(validUserAgent);
 };
diff --git a/src/user-agent/index.js b/src/user-agent/index.js
index 846ddaa..eaa8ceb 100644
--- a/src/user-agent/index.js
+++ b/src/user-agent/index.js
@@ -75,7 +75,7 @@ const firefoxUserAgent = userAgent => firefoxVersion => {
   return userAgent;
 };
 
-const userAgentForView = browserViewOrWindow => defaultUserAgent(browserViewOrWindow.webContents.userAgent);
+const userAgentForWebContents = webContents => defaultUserAgent(webContents.userAgent);
 
 const addUserAgentInterceptor = session => {
   if (!session.userAgentInterceptor) {
@@ -96,6 +96,6 @@ const addUserAgentInterceptor = session => {
 module.exports = {
   BROWSER_VERSIONS,
   initBrowserVersions,
-  userAgentForView,
+  userAgentForWebContents,
   addUserAgentInterceptor
 };


Test patch:
diff --git a/src/main/__tests__/init-browser-versions.test.js b/src/main/__tests__/init-browser-versions.test.js
index fa79f3e..5337275 100644
--- a/src/main/__tests__/init-browser-versions.test.js
+++ b/src/main/__tests__/init-browser-versions.test.js
@@ -27,7 +27,7 @@ describe('Main :: initBrowserVersions test suite', () => {
     jest.mock('electron', () => require('../../__tests__').mockElectronInstance());
     electron = require('electron');
     userAgent = require('../../user-agent');
-    jest.spyOn(userAgent, 'userAgentForView').mockImplementation(() => 'UserAgent String');
+    jest.spyOn(userAgent, 'userAgentForWebContents').mockImplementation(() => 'UserAgent String');
   });
   describe('throws error', () => {
     let show;
diff --git a/src/user-agent/__tests__/index.test.js b/src/user-agent/__tests__/index.test.js
index d1eb440..e2a1c5c 100644
--- a/src/user-agent/__tests__/index.test.js
+++ b/src/user-agent/__tests__/index.test.js
@@ -57,44 +57,38 @@ describe('User Agent module test suite', () => {
       expect(userAgent.BROWSER_VERSIONS.firefox).toBeNull();
     });
   });
-  describe('userAgentForView', () => {
+  describe('userAgentForWebContents', () => {
     test('default and chromium version not available, should remove non-standard tokens from user-agent header', () => {
       // Given
       userAgent.BROWSER_VERSIONS.chromium = null;
-      const browserView = {
-        webContents: {
-          userAgent: 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) ElectronIM/13.337.0 Chrome/79.0.1337.79 Electron/0.0.99 Safari/537.36'
-        }
+      const webContents = {
+        userAgent: 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) ElectronIM/13.337.0 Chrome/79.0.1337.79 Electron/0.0.99 Safari/537.36'
       };
       // When
-      const result = userAgent.userAgentForView(browserView);
+      const result = userAgent.userAgentForWebContents(webContents);
       // Then
       expect(result).toBe('Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) Chrome/79.0.1337.79 Safari/537.36');
     });
     test('default and chromium version available, should replace Chrome version in user-agent header', () => {
       // Given
       userAgent.BROWSER_VERSIONS.chromium = '1337.1337.1337';
-      const browserView = {
-        webContents: {
-          userAgent: 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) ElectronIM/13.337.0 Chrome/79.0.1337.79 Electron/0.0.99 Safari/537.36'
-        }
+      const webContents = {
+        userAgent: 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) ElectronIM/13.337.0 Chrome/79.0.1337.79 Electron/0.0.99 Safari/537.36'
       };
       // When
-      const result = userAgent.userAgentForView(browserView);
+      const result = userAgent.userAgentForWebContents(webContents);
       // Then
       expect(result).toBe('Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) Chrome/1337.1337.1337 Safari/537.36');
     });
     test('non-matching url provided and chromium version available, should replace Chrome version in user-agent header', () => {
       // Given
       userAgent.BROWSER_VERSIONS.chromium = '1337.1337.1337';
-      const browserView = {
-        webContents: {
-          userAgent: 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) ElectronIM/13.337.0 Chrome/79.0.1337.79 Electron/0.0.99 Safari/537.36'
-        }
+      const webContents = {
+        userAgent: 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) ElectronIM/13.337.0 Chrome/79.0.1337.79 Electron/0.0.99 Safari/537.36'
       };
       const nonMatchingUrl = 'https://some-url-com/google.com';
       // When
-      const result = userAgent.userAgentForView(browserView, nonMatchingUrl);
+      const result = userAgent.userAgentForWebContents(webContents, nonMatchingUrl);
       // Then
       expect(result).toBe('Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) Chrome/1337.1337.1337 Safari/537.36');
     });