[Chrome Extension] Get Google Account Id from Google Photos


This Chrome extension helps you get your Google account/user id from Google Photos. There is already an easy way [1] to get Google account id. This post provides another way to get it.

The idea is that there is a variable window.WIZ_global_data in the HTML source of https://photos.google.com/. The "S06Grb" field of the variable contains Google account id. We will find the string of the variable in the HTML source and call JSON.parse method to convert it to a regular variable, and read the id from the variable.

The following is source code for above idea.

manifest.json:

{
  "manifest_version": 2,

  "name": "guserid",
  "description": "Get Google user id",
  "version": "0.1",

  "browser_action": {
    "default_title": "get Google id",
    "default_popup": "popup.html"
  },

  "permissions": [
    "tabs",
    "<all_urls>"
  ]
}

popup.html:

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

popup.js:

chrome.runtime.onMessage.addListener(function(request, sender) {
  document.write(request.id);
});

chrome.tabs.executeScript(null, {file: "getid.js"});

getid.js:

function find_WIZ_global_data(elm) {
  if (elm.nodeType == Node.ELEMENT_NODE || elm.nodeType == Node.DOCUMENT_NODE) {
    for (var i=0; i < elm.childNodes.length; i++) {
      // recursively call self
      find_WIZ_global_data(elm.childNodes[i]);
    }
  }

  if (elm.nodeType == Node.TEXT_NODE) {
    if (elm.nodeValue.startsWith("window.WIZ_global_data")) {
      var jsonString = elm.nodeValue.replace("window.WIZ_global_data = ", "");
      jsonString = jsonString.slice(0, -1);
      var wiz = JSON.parse(jsonString);
      chrome.runtime.sendMessage({id: wiz["S06Grb"]});
    }
  }
}

find_WIZ_global_data(document);

References:

[1]How to get your Google account/user ID · GitHub
[2]
[3]Message Passing - Google Chrome
[4]Getting the source HTML of the current page from chrome extension - Stack Overflow
[5][JavaScript] Traverse DOM Tree
[6]