[Chrome Extension] Get Authentication Cookies of gphotosuploader


gphotosuploader is a tool that helps you upload photos/videos to Google Photos. It uses WebDrivers Protocol to get authentication cookies and user id to upload to Google Photos. I write a Chrome extension to get authentication cookies instead of using WebDrivers. The following is my code:

manifest.json:

{
  "manifest_version": 2,

  "name": "gpcookies",
  "description": "Get authentication cookies of gphotosuploader",
  "version": "0.1",

  "browser_action": {
    "default_title": "export gphotosuploader auth.json",
    "default_popup": "popup.html"
  },
  "permissions": [
    "cookies",
    "tabs",
    "<all_urls>"
  ]
}

popup.html:

<script src="popup.js"></script>

popup.js:

chrome.tabs.query({
  active: true,
  currentWindow: true
}, function(tabs) {
  var tab = tabs[0];

  chrome.cookies.getAll({}, function (cookies) {

    var cookieNames = ["OTZ", "CONSENT", "SID", "APISID", "SAPISID", "HSID", "NID", "SSID"];
    var cookieDomains = [".google.com", "photos.google.com"];

    var auth = {
      "cookies": [],
      "persistantParameters": {
        "userId": ""
      }
    };

    document.write("<pre>");
    for (var i in cookies) {

      var cookie = cookies[i];
      if (cookieNames.indexOf(cookie.name) == -1) {
        continue;
      }
      if (cookieDomains.indexOf(cookie.domain) == -1) {
        continue;
      }

      var cookieAuth = {};
      cookieAuth["Name"] = cookie.name;
      cookieAuth["Value"] = cookie.value;
      cookieAuth["Domain"] = cookie.domain;
      cookieAuth["HttpOnly"] = cookie.httpOnly;
      cookieAuth["Secure"] = cookie.secure;
      cookieAuth["Path"] = cookie.path;

      auth["cookies"].push(cookieAuth);

    }
    document.write(JSON.stringify(auth, null, 2));
    document.write("</pre>");
  });
});

References:

[1]GitHub - simonedegiacomi/gphotosuploader: Unofficial Google Photos uploader and Go library
[2]chrome.cookies - Google Chrome
[3]webextensions-examples/list-cookies at master · mdn/webextensions-examples · GitHub