Vikas Bansal Vikas Bansal - 6 months ago 89
Node.js Question

ElectronJS: how to manipulate cookies from rendered Process(browser window)?

I am able to create persistent cookies from main process (app.js)

code:

const session = require('electron').session;
const ses = session.fromPartition('persist:name');

debugger;
// Query all cookies associated with a specific url.
ses.cookies.get({}, (error, cookies) => {

console.log(cookies); /// cookies values should be printed next time the applciation is launched.
});

// 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',
expirationDate: 2093792393999
};
ses.cookies.set(cookie, (error) => {
debugger;

if (error)
console.error(error);
});


But when I am doing the same in Browser window, cookies are not being created and the
session
is undefined.

Please suggest what should I do to save persistent data in the browser.

Answer

Built-in modules that are not directly available in the renderer process (such as session) can be accessed via IPC, the remote module provides a simple way to do that (but you should be aware of its limitations). So you should be able to get your code working in the renderer process by simply changing the first line to:

const session = require('electron').remote.session;
Comments