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.
This commit is contained in:
Svallinn 2021-06-16 04:50:30 +01:00
parent 0551ce44f2
commit bd4e867db1
No known key found for this signature in database
GPG Key ID: 09FB527F34037CCA
1 changed files with 12 additions and 11 deletions

View File

@ -1,6 +1,6 @@
import { import {
app, BrowserWindow, dialog, Menu, app, BrowserWindow, dialog, Menu, ipcMain,
ipcMain, powerSaveBlocker, screen, shell powerSaveBlocker, screen, session, shell
} from 'electron' } from 'electron'
import Datastore from 'nedb' import Datastore from 'nedb'
import path from 'path' import path from 'path'
@ -188,19 +188,20 @@ function runApp() {
}) })
if (useProxy) { if (useProxy) {
newWindow.webContents.session.setProxy({ session.defaultSession.setProxy({
proxyRules: proxyUrl proxyRules: proxyUrl
}) })
} }
// Set CONSENT cookie on reasonable domains // Set CONSENT cookie on reasonable domains
[ const consentCookieDomains = [
'http://www.youtube.com', 'http://www.youtube.com',
'https://www.youtube.com', 'https://www.youtube.com',
'http://youtube.com', 'http://youtube.com',
'https://youtube.com' 'https://youtube.com'
].forEach(url => { ]
newWindow.webContents.session.cookies.set({ consentCookieDomains.forEach(url => {
session.defaultSession.cookies.set({
url: url, url: url,
name: 'CONSENT', name: 'CONSENT',
value: 'YES+' value: 'YES+'
@ -264,8 +265,8 @@ function runApp() {
newWindow.on('close', () => { newWindow.on('close', () => {
// Clear cache and storage if it's the last window // Clear cache and storage if it's the last window
if (openedWindows.length === 1) { if (openedWindows.length === 1) {
newWindow.webContents.session.clearCache() session.defaultSession.clearCache()
newWindow.webContents.session.clearStorageData({ session.defaultSession.clearStorageData({
storages: [ storages: [
'appcache', 'appcache',
'cookies', 'cookies',
@ -326,15 +327,15 @@ function runApp() {
createWindow() createWindow()
}) })
ipcMain.on('enableProxy', (event, url) => { ipcMain.on('enableProxy', (_, url) => {
console.log(url) console.log(url)
mainWindow.webContents.session.setProxy({ session.defaultSession.setProxy({
proxyRules: url proxyRules: url
}) })
}) })
ipcMain.on('disableProxy', () => { ipcMain.on('disableProxy', () => {
mainWindow.webContents.session.setProxy({}) session.defaultSession.setProxy({})
}) })
ipcMain.on('openExternalLink', (_, url) => { ipcMain.on('openExternalLink', (_, url) => {