Let's say I have two BrowserView
in the same BrowserWindow
and an UI button allowing the user to switch between showing bv1
or bv2
(like the "tab" system in browsers like Firefox, Chrome, that allows you to switch between the different pages):
browserWindow = new BrowserWindow({ width: 1200, height: 600 });
let bv1 = new BrowserView({ webPreferences: { nodeIntegration: false }});
bv1.setBounds({ x: 0, y: 0, width: 1200, height: 600 });
bv1.webContents.loadURL('https://www.twitter.com');
let bv2 = new BrowserView({ webPreferences: { nodeIntegration: false }});
bv2.setBounds({ x: 0, y: 0, width: 1200, height: 600 });
bv2.webContents.loadURL('https://www.twitter.com');
browserWindow.setBrowserView(bv1);
and when a button (like a "tab" in a browser) is pressed:
browserWindow.setBrowserView(bv2);
I noticed that these two BrowserView
:
share the same cookies/localStorage (which I don't want!), i.e. if the first is connected to an account, the second will be connected as well to the same account
keep history and cookies after restart of the Electron app (this is good and wanted indeed!)
Question: how to have the two BrowserView
totally isolated in terms of cookies/localStorage/history (and thus bv1
could be connected to one Twitter account and bv2
to another one)?
So, I managed to get this working but in a very, very, roundabout way. Effectively session hijacking your own session, saving and loading it on app close/open. Code below with some comments, prefaced with some useful links. This worked when running as dev, and when running with a build application.
You may need to look into possible security issues here with storing cookies locally like this.
The only thing I have not tackled in this answer is:
C:\Users\%user%\AppData\Roaming\%appname%\storage
.session.fromPartition
docs.