在处理electron的cookie设置时,发现有的cookie无法设置,提示Error: Failed to get cookie domain,这里搬运下stackoverflow的答案:
问题是因为cookie的url参数没有设置,直接将需要注入cookie的网页地址作为url即可。
例如:
const { app, BrowserWindow } = electron;
let win = new BrowserWindow({});// 此处忽略win窗口的创建
const { session } = win.webContents;
const templateUrl = "https://test.domain.com/dashboard";
const cks = [{"name":"ck1","value":"123456","url":"https://test.domain.com/dashboard","domain":".domain.com","path":"/","expires":1674572337,"httpOnly":true,"secure":true},{"name":"ck2","value":"123","domain":".domain.com","path":"/","expires":1674572337,"httpOnly":true,"secure":fakse}];
cks.forEach(function(cookie) {
  let ckUrl = cookie.url;
  if(!ckUrl) {
    // 第二个cookie对象是没有url的,可以补一下即可
    cookie.url = templateUrl;
  }
  session.cookies.set(cookie).then(() => {
      console.log(">>>>>>>>>>>>>>>>success");
    }, (error) => {
      console.error(">>>>>>>>>", cookie, error);
    });
});stackoverflow原文地址:
https://stackoverflow.com/questions/61520607/electron-cookies-error-failed-to-get-cookie-domain









