Search code examples
cookiesnw.js

How can I get and set cookies inside webview tag of nwjs?


So my app just open a new window of a local html file with a webview tag of some webpage. But when I tried to get the cookies, I cannot get anything.I think the reason is the cookie is bounded to the url of the webview tag but now my local file but when I can only get the window of the local file.How can I solve this problem?


Solution

  • it might be late but i will answer this for future visitors :P

    I think the best and the direct way to set and get cookies without any workarounds is to use webview request interceptors and change the request and response headers this is more secure and reliable and will work in nodewebkit, electron and chromium extensions and apps:

    So to set headers you can use onBeforeSendHeaders, for example lets say you want to change a cookie called "connect.sid" that used as session id in expressjs you can do the following:

    var new_sessionId = "s%randskbksbdfmnsdbf345k345h34k5";
    var $webview = $("#my-webview");
    $webview.get(0).request.onBeforeSendHeaders.addListener(
        function (details) {
            details.requestHeaders.forEach(function (header) {
                if (header.name === "Cookie") {
                    var cookies = header.value.split("; ");
                    var valid_cookies = cookies.filter(function (cookie) {
                        return cookie && cookie.indexOf("connect.sid") < 0;
                    });
                    valid_cookies.push("connect.sid=" + new_sessionId);
                    header.value = valid_cookies.join("; ");
                }
            });
            return {requestHeaders: details.requestHeaders};
        },
        {urls: ["<all_urls>"]},
        ["blocking", "requestHeaders"]
    );
    $webview.attr("src","http://example.com");
    

    and to read headers you can use onHeadersReceived

    var $webview = $("#my-webview");
    $webview.get(0).request.onHeadersReceived.addListener(function (details) {
            details.responseHeaders.forEach(function (header) {
                if (header.name === "set-cookie") {
                    var cookies = header.value.split("; ");
                    var sessionCookie = cookies.find(function (cookie) {
                        return cookie && cookie.indexOf("connect.sid") === 0;
                    });
                    if (sessionCookie) {
                        var sessionId = sessionCookie.split("=")[1];
                        console.log(sessionId);
                    }
                }
            });
        },
        {urls: ["<all_urls>"]},
        ["blocking", "responseHeaders"]
    );
    $webview.attr("src","http://example.com");
    

    Note: you can also set and get cookies for your main window using this method but instead of intercepting webview.request you can intercept chrome.webRequest or just use chrome.cookies.set and chrome.cookies.get, i found all these things in chromium source code ;)