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 {
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) => {