Skip to content

Instantly share code, notes, and snippets.

@missionmike
Last active March 6, 2021 05:52
Show Gist options
  • Save missionmike/667bf157d7a0c11a17f097383a21b535 to your computer and use it in GitHub Desktop.
Save missionmike/667bf157d7a0c11a17f097383a21b535 to your computer and use it in GitHub Desktop.
Get CID from Google My Business Table
/**
* Instructions:
*
* 1. Visit your Google My Business admin section to view the "Businesses" table.
* If there is a "See your profile" button on the far right, this script might
* help you scrape for the Google Maps CIDs.
*
* 2. Open console, copy/paste this script there and execute. Data should get
* copied to the clipboard at the end of script.
*
* 3. Open Excel or Google Sheets, etc., and paste resulting data. Last two columns
* should include CID and Maps Link.
*/
let rows = [];
document.querySelectorAll("tr").forEach(($tr) => {
// Don't proceed if <tr> doesn't contain a relevant <a>
if (!$tr.querySelector(`a[href*="place?id="]`)) return;
let tr = [];
$tr.querySelectorAll("td").forEach(($td) => {
if ($td.textContent) tr.push($td.textContent);
});
// Populate cells for CID based on link value
let $a = $tr.querySelector(`a[href*="place?id"]`),
url = $a.getAttribute("href"),
cid = url.split("?id=")[1].split("&")[0];
tr.push(url, cid, `https://www.google.com/maps?cid=${cid}`);
rows.push(tr);
});
// Copy tab-delineated values to clipboard
let $textarea = document.createElement("textarea");
$textarea.setAttribute("readonly", "");
$textarea.style = { position: "absolute", left: "-9999px" };
$textarea.value = rows
.map(function (d) {
return d.join("\t");
})
.join("\n");
document.body.appendChild($textarea);
$textarea.select();
document.execCommand("copy");
document.body.removeChild($textarea);
// Debug output
console.log("\n%cCopied tab-delineated text data to clipboard.", "color:green; font-size:20px;");
console.log(
`%cTotal columns: %c${rows[rows.length - 1].length}`,
"font-size:20px",
"color:green; font-size:20px;"
);
console.log(`%cTotal rows: %c${rows.length}`, "font-size:20px", "color:green; font-size:20px;");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment