Skip to content

Commit

Permalink
bugfix: #7 User agent cleaned also for Service Workers
Browse files Browse the repository at this point in the history
  • Loading branch information
manusa committed Jan 15, 2020
1 parent 707b946 commit 4ed3cfb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/tab-manager/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe('Tab Manager module test suite', () => {
mockMenu = {};
jest.resetModules();
jest.mock('electron', () => ({
app: {},
BrowserView: jest.fn(() => mockBrowserView),
Menu: jest.fn(() => mockMenu),
MenuItem: jest.fn(() => mockMenuItem)
Expand Down Expand Up @@ -90,7 +91,8 @@ describe('Tab Manager module test suite', () => {
// When
const result = tabManager.getTab(1337).webContents.userAgent;
// Then
expect(result).toBe('Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) Chrome/79 Safari/537.36');
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');
expect(require('electron').app.userAgentFallback).toBe('Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/1337.36 (KHTML, like Gecko) Chrome/79.0.1337.79 Safari/537.36');
});
describe('Event listeners', () => {
let events;
Expand Down
11 changes: 6 additions & 5 deletions src/tab-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
const {BrowserView, Menu, MenuItem} = require('electron');
const {app, BrowserView, Menu, MenuItem} = require('electron');
const {APP_EVENTS} = require('../constants');
const settings = require('../settings');
const {contextMenuHandler} = require('../spell-check');
Expand Down Expand Up @@ -63,13 +63,14 @@ const handleContextMenu = browserView => async (event, params) => {
menu.popup({x, y});
};

// Required for Service Workers -> https://github.com/electron/electron/issues/16196
const setGlobalUserAgentFallback = userAgent => (app.userAgentFallback = userAgent);

const cleanUserAgent = browserView => {
const currentChromeMajorVersion = /Chrome\/(?<version>[0-9]+)./g.exec(browserView.webContents.userAgent)
.groups.version;
browserView.webContents.userAgent = browserView.webContents.userAgent
.replace(/ElectronIM\/.*? /g, '')
.replace(/Electron\/.*? /g, '')
.replace(/Chrome\/(\S+)/g, `Chrome/${currentChromeMajorVersion}`);
.replace(/Electron\/.*? /g, '');
setGlobalUserAgentFallback(browserView.webContents.userAgent);
};

const addTabs = ipcSender => tabsMetadata => {
Expand Down

0 comments on commit 4ed3cfb

Please sign in to comment.