在哪里放置电子饼干?
Where to place electron-cookies?
Here 它基本上是说:在您的应用程序的渲染器代码中,只需要这个包....
我不知道该放在哪里。当我将其放入 main.js 时,我的应用程序将不再启动。我正确安装了它,因为它在我的 node_modules 文件夹中。有什么想法吗?
我的main.js:
'use strict';
const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
function createWindow () {
require('electron-cookies');
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600,nodeIntegration:false});
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/index.html');
// Open the DevTools.
mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
如前所述 -> 这个 main.js 不起作用,因为 require('electron-cookies')
首先你需要了解浏览器进程和渲染器进程之间的区别,阅读 Electron Quick Start, and a brief overview of the Electron architecture 应该会有帮助。
现在,main.js
在浏览器进程中运行,index.html
在渲染器进程中运行,所以要使用 electron-cookies
你必须直接或间接地在你的 index.html
。直接的方法是:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
require('electron-cookies');
</script>
</head>
<body>
</body>
我为此苦苦挣扎,不喜欢 electron-cookies
库,尤其是缺少打字稿支持。
对我有用的是像这样存储文档中提到的 cookie
session.defaultSession.cookies.set({
url: config.api,
path: '/',
name: 'race-manager-token',
value: params.get('token'),
expirationDate: new Date(
new Date().setFullYear(new Date().getFullYear() + 999)
).getTime(),
})
这当然只在主进程中存储 cookie,但要让您的前端代码发送 cookie,您可以这样做
session.defaultSession.webRequest.onBeforeSendHeaders({ urls: []}, async (details, callback) => {
await session.defaultSession.cookies.get({}).then(cookies => {
cookies.map(c => {
details.requestHeaders['Cookie'] = cookie.serialize(c.name, c.value, {})
})
callback({ requestHeaders: details.requestHeaders })
})
})
Here 它基本上是说:在您的应用程序的渲染器代码中,只需要这个包....
我不知道该放在哪里。当我将其放入 main.js 时,我的应用程序将不再启动。我正确安装了它,因为它在我的 node_modules 文件夹中。有什么想法吗?
我的main.js:
'use strict';
const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
function createWindow () {
require('electron-cookies');
// Create the browser window.
mainWindow = new BrowserWindow({width: 800, height: 600,nodeIntegration:false});
// and load the index.html of the app.
mainWindow.loadURL('file://' + __dirname + '/index.html');
// Open the DevTools.
mainWindow.webContents.openDevTools();
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On OS X it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
如前所述 -> 这个 main.js 不起作用,因为 require('electron-cookies')
首先你需要了解浏览器进程和渲染器进程之间的区别,阅读 Electron Quick Start, and a brief overview of the Electron architecture 应该会有帮助。
现在,main.js
在浏览器进程中运行,index.html
在渲染器进程中运行,所以要使用 electron-cookies
你必须直接或间接地在你的 index.html
。直接的方法是:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script>
require('electron-cookies');
</script>
</head>
<body>
</body>
我为此苦苦挣扎,不喜欢 electron-cookies
库,尤其是缺少打字稿支持。
对我有用的是像这样存储文档中提到的 cookie
session.defaultSession.cookies.set({
url: config.api,
path: '/',
name: 'race-manager-token',
value: params.get('token'),
expirationDate: new Date(
new Date().setFullYear(new Date().getFullYear() + 999)
).getTime(),
})
这当然只在主进程中存储 cookie,但要让您的前端代码发送 cookie,您可以这样做
session.defaultSession.webRequest.onBeforeSendHeaders({ urls: []}, async (details, callback) => {
await session.defaultSession.cookies.get({}).then(cookies => {
cookies.map(c => {
details.requestHeaders['Cookie'] = cookie.serialize(c.name, c.value, {})
})
callback({ requestHeaders: details.requestHeaders })
})
})