ability to create a new session for every BrowserWindow object?

时间:2019-03-19 15:10:19

标签: node.js google-chrome session electron

In my JS class constructer I have a variable.

this.cookieBrowser = new BrowserWindow({ show: true, webPreferences: { webSecurity: false } });

I later then call that variable in a function to create a new window... this works just fine... However, for each task (child process) that I am running, the cookies / session data are the SAME - but I need each browser to have a different session.

I was wondering if there was any way to complete this task as each object is currently returning the exact same set of cookies.

I have tried clearing the session every time I call the function using:

    that.cookieBrowser.webContents.session.clearStorageData([], await function (data) {

    })

This seems to just clear the cookies temporarily - all cookies are still the same in the long run.

1 个答案:

答案 0 :(得分:1)

您应该为每个浏览器窗口指定一个不同的(持久性或非持久性)分区。

    this.cookieBrowser = new BrowserWindow({
        show: true,
        webPreferences: {
            webSecurity: false,
            partition: 'unique_random_path' // OR  'persist:unique_random_path' to save session on disk 
        } 
    });

    this.cookieBrowser2 = new BrowserWindow({
        show: true,
        webPreferences: {
            webSecurity: false,
            partition: 'unique_random_path2' // OR  'persist:unique_random_path2' to save session on disk 
        } 
    });

通过将session object传递到webPreferences,可以获得相同的结果。

请参见here所有browserWindow选项以及sessionpartition属性之间的区别。