From bd4e867db1784f78ed1e1990000f341f35ae4751 Mon Sep 17 00:00:00 2001 From: Svallinn <41585298+Svallinn@users.noreply.github.com> Date: Wed, 16 Jun 2021 04:50:30 +0100 Subject: [PATCH] Main: Change `window.webContents.session` to `session.defaultSession` Previously, the code misleadingly lead developers to believe that each 'BrowserWindow' had its own isolated session. This commit clarifies the fact that a global session is shared by all 'BrowserWindow's by default. --- src/main/index.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index 04675674..7703beef 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -1,6 +1,6 @@ import { - app, BrowserWindow, dialog, Menu, - ipcMain, powerSaveBlocker, screen, shell + app, BrowserWindow, dialog, Menu, ipcMain, + powerSaveBlocker, screen, session, shell } from 'electron' import Datastore from 'nedb' import path from 'path' @@ -188,19 +188,20 @@ function runApp() { }) if (useProxy) { - newWindow.webContents.session.setProxy({ + session.defaultSession.setProxy({ proxyRules: proxyUrl }) } // Set CONSENT cookie on reasonable domains - [ + const consentCookieDomains = [ 'http://www.youtube.com', 'https://www.youtube.com', 'http://youtube.com', 'https://youtube.com' - ].forEach(url => { - newWindow.webContents.session.cookies.set({ + ] + consentCookieDomains.forEach(url => { + session.defaultSession.cookies.set({ url: url, name: 'CONSENT', value: 'YES+' @@ -264,8 +265,8 @@ function runApp() { newWindow.on('close', () => { // Clear cache and storage if it's the last window if (openedWindows.length === 1) { - newWindow.webContents.session.clearCache() - newWindow.webContents.session.clearStorageData({ + session.defaultSession.clearCache() + session.defaultSession.clearStorageData({ storages: [ 'appcache', 'cookies', @@ -326,15 +327,15 @@ function runApp() { createWindow() }) - ipcMain.on('enableProxy', (event, url) => { + ipcMain.on('enableProxy', (_, url) => { console.log(url) - mainWindow.webContents.session.setProxy({ + session.defaultSession.setProxy({ proxyRules: url }) }) ipcMain.on('disableProxy', () => { - mainWindow.webContents.session.setProxy({}) + session.defaultSession.setProxy({}) }) ipcMain.on('openExternalLink', (_, url) => {