cookie字符串转对象

从浏览器copy出来的cookie是一段字符串,转为puppeteer可以识别的对象呢?,比较简单:

let cookieStr = `
__editor_pc_driver_show__=true; shshshfpa=29e7a6c2-25d6-df76-d181-7986cdd4a5e4-1553562310; shshshfpb=g%207BzK%2FEySratP1DZuDSKRA%3D%3D; ipLoc-djd=12-904-3377-0; ceshi3.com=000; pinId=LnE8W11RU_bkTOqe4otrdw; pin=%E9%9D%92%E5%B9%B4%E9%A6%86%E5%AD%99%E7%91%BE; unick=%E9%9D%92%E5%B9%B4%E9%A6%86%E5%AD%99%E7%91%BE
`;  //省略部分
function cookieToJson(str) {
    let cookieArr = str.split(";");

    let cookieArray = [];
    cookieArr.forEach((i) => {
        let obj = {}
        let arr = i.split("=");
        //obj[arr[0].trim()] = arr[1].trim();
        obj.name = arr[0].trim();
        obj.value = arr[1].trim();
        obj.domain = "jshopx.jd.com";
        obj.httpOnly = false;
        obj.secure = false;
        cookieArray.push(obj);
    });
    return cookieArray;
}

let cookies = cookieToJson(cookieStr);

//await page.setCookie(...cookies);    

关键点:

1、识别cookie格式,特别是puppeteer的cookie格式。

你可能感兴趣的:(JavaScript)