gpt4 book ai didi

cookies - 持久性 cookies 在Electron 5

转载 作者:行者123 更新时间:2023-12-03 12:37:54 26 4
gpt4 key购买 nike

我正在Electron中加载网页URL,但当应用程序关闭时,cookies在再次打开后为空。我无法更新,因为已删除了插件支持,所以我停留在5.x版本。我已经尝试了几种不同的方法而没有运气。在Electron 5中获取持久性cookie的最佳方法是什么?
以下是我尝试过的解决方法。但是,它没有用。

const {electron, app, BrowserWindow} = require('electron');
const path = require('path')
const fs = require('fs');
const dataPath = app.getPath('userData')
const filePath = path.join(dataPath, 'config');

mainWindow = new BrowserWindow({
webPreferences: {
plugins: true,
nodeIntegration: false
}
});

try {
console.log(fs.readFileSync(filePath, 'utf8'))
mainWindow.webContents.executeJavaScript(
"document.cookie = '" + fs.readFileSync(filePath, 'utf8') + "';"
)
} catch (e) {
console.log(e)
}

mainWindow.loadURL('https://www.example.com/')

mainWindow.on('close', function () {
mainWindow.webContents
.executeJavaScript('document.cookie;')
.then(cookies => {
console.log(cookies)
fs.writeFileSync(filePath, cookies)
})
})

最佳答案

尝试引用以下文档:https://www.electronjs.org/docs/api/cookies

const { session } = require('electron')

// Query all cookies.
session.defaultSession.cookies.get({})
.then((cookies) => {
console.log(cookies)
}).catch((error) => {
console.log(error)
})

// Query all cookies associated with a specific url.
session.defaultSession.cookies.get({ url: 'http://www.github.com' })
.then((cookies) => {
console.log(cookies)
}).catch((error) => {
console.log(error)
})

// Set a cookie with the given cookie data;
// may overwrite equivalent cookies if they exist.
const cookie = { url: 'http://www.github.com', name: 'dummy_name', value: 'dummy' }
session.defaultSession.cookies.set(cookie)
.then(() => {
// success
}, (error) => {
console.error(error)
})

关于cookies - 持久性 cookies 在Electron 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65712071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com