Extract Google Business Data (v12)

Extracts and displays Google Business data with a slide-out panel for logging and information display

  1. // ==UserScript==
  2. // @name Extract Google Business Data (v12)
  3. // @namespace https://example.com/
  4. // @version 12.0
  5. // @description Extracts and displays Google Business data with a slide-out panel for logging and information display
  6. // @author sharmanhall
  7. // @match https://www.google.com/search*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=google.com
  9. // @grant GM_addStyle
  10. // @grant GM_setValue
  11. // @grant GM_getValue
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // Add styles for the slide-out panel and popup
  18. GM_addStyle(`
  19. #google-panel-wrapper {
  20. position: fixed;
  21. top: 0;
  22. right: -300px;
  23. width: 300px;
  24. height: 100%;
  25. background-color: #1d1d1d;
  26. color: #fff;
  27. transition: right 0.3s ease;
  28. z-index: 9999;
  29. font-family: 'Arial', sans-serif;
  30. box-shadow: -2px 0 5px rgba(0,0,0,0.2);
  31. }
  32. #google-panel-toggle {
  33. position: absolute;
  34. left: -30px;
  35. top: 50%;
  36. width: 30px;
  37. height: 60px;
  38. background-color: #F14E13;
  39. cursor: pointer;
  40. display: flex;
  41. align-items: center;
  42. justify-content: center;
  43. border-top-left-radius: 5px;
  44. border-bottom-left-radius: 5px;
  45. }
  46. #google-panel-content {
  47. padding: 15px;
  48. height: 100%;
  49. overflow-y: auto;
  50. }
  51. #google-panel-content h3 {
  52. color: #F14E13;
  53. border-bottom: 1px solid #F14E13;
  54. padding-bottom: 10px;
  55. margin-bottom: 15px;
  56. }
  57. .google-info-item {
  58. margin-bottom: 15px;
  59. }
  60. .google-info-item strong {
  61. display: block;
  62. margin-bottom: 5px;
  63. }
  64. .google-info-item button {
  65. background-color: #F14E13;
  66. color: #fff;
  67. border: none;
  68. padding: 5px 10px;
  69. cursor: pointer;
  70. border-radius: 3px;
  71. margin-top: 5px;
  72. }
  73. #google-log {
  74. background-color: #2d2d2d;
  75. border: 1px solid #F14E13;
  76. padding: 10px;
  77. margin-top: 15px;
  78. height: 200px;
  79. overflow-y: auto;
  80. font-family: monospace;
  81. font-size: 12px;
  82. }
  83. #google-panel-credits {
  84. margin-top: 20px;
  85. font-size: 12px;
  86. text-align: center;
  87. color: #888;
  88. }
  89. #google-popup {
  90. position: fixed;
  91. top: 20px;
  92. left: 50%;
  93. transform: translateX(-50%);
  94. background-color: #F14E13;
  95. color: #fff;
  96. padding: 10px 20px;
  97. border-radius: 5px;
  98. z-index: 10000;
  99. opacity: 0;
  100. transition: opacity 0.3s ease-in-out;
  101. }
  102. #google-rescan-button {
  103. background-color: #F14E13;
  104. color: #fff;
  105. border: none;
  106. padding: 10px 20px;
  107. border-radius: 3px;
  108. cursor: pointer;
  109. transition: background-color 0.3s;
  110. box-shadow: 0 0 10px rgba(241, 78, 19, 0.5);
  111. }
  112. #google-rescan-button:hover {
  113. background-color: #ff6a3c;
  114. }
  115. `);
  116.  
  117. // Create the slide-out panel
  118. const panelHTML = `
  119. <div id="google-panel-wrapper">
  120. <div id="google-panel-toggle">📊</div>
  121. <div id="google-panel-content">
  122. <h3>Business Information</h3>
  123. <button id="google-rescan-button">Rescan Page</button>
  124. <div id="google-business-info"></div>
  125. <h3>Console Log</h3>
  126. <div id="google-log"></div>
  127. <div id="google-panel-credits">
  128. <small>v11.0 | by sharmanhall</small>
  129. </div>
  130. </div>
  131. </div>
  132. `;
  133.  
  134. // Append the panel to the body
  135. document.body.insertAdjacentHTML('beforeend', panelHTML);
  136.  
  137. // Toggle panel visibility
  138. document.getElementById('google-panel-toggle').addEventListener('click', function() {
  139. const wrapper = document.getElementById('google-panel-wrapper');
  140. wrapper.style.right = wrapper.style.right === '0px' ? '-300px' : '0px';
  141. });
  142.  
  143. // Function to log to both console and panel
  144. function logToPanel() {
  145. const args = Array.from(arguments);
  146. const logElement = document.getElementById('google-log');
  147. logElement.innerHTML += args.join(' ') + '<br><br>';
  148. logElement.scrollTop = logElement.scrollHeight;
  149. }
  150.  
  151. // Popup notification system
  152. let popupQueue = [];
  153. let isShowingPopup = false;
  154.  
  155. function showPopup(message, duration = 3000) {
  156. popupQueue.push({ message, duration });
  157. if (!isShowingPopup) {
  158. displayNextPopup();
  159. }
  160. }
  161.  
  162. function displayNextPopup() {
  163. if (popupQueue.length === 0) {
  164. isShowingPopup = false;
  165. return;
  166. }
  167. isShowingPopup = true;
  168. const { message, duration } = popupQueue.shift();
  169. const popup = document.createElement('div');
  170. popup.id = 'google-popup';
  171. popup.textContent = message;
  172. document.body.appendChild(popup);
  173. // Fade in
  174. setTimeout(() => {
  175. popup.style.opacity = '1';
  176. }, 10);
  177. // Fade out and remove
  178. setTimeout(() => {
  179. popup.style.opacity = '0';
  180. setTimeout(() => {
  181. document.body.removeChild(popup);
  182. displayNextPopup(); // Show next popup in queue
  183. }, 300);
  184. }, duration);
  185. }
  186.  
  187. // Function to update business information in the panel
  188. function updateBusinessInfo(info) {
  189. const infoElement = document.getElementById('google-business-info');
  190. infoElement.innerHTML = '';
  191. for (const [key, value] of Object.entries(info)) {
  192. const itemDiv = document.createElement('div');
  193. itemDiv.className = 'google-info-item';
  194. itemDiv.innerHTML = `<strong>${key}:</strong> ${value}`;
  195. const copyButton = document.createElement('button');
  196. copyButton.textContent = 'Copy';
  197. copyButton.onclick = () => {
  198. navigator.clipboard.writeText(value).then(() => {
  199. showPopup(`${key} copied to clipboard!`);
  200. }, () => {
  201. showPopup(`Failed to copy ${key}`, 5000);
  202. });
  203. };
  204. itemDiv.appendChild(copyButton);
  205. infoElement.appendChild(itemDiv);
  206. }
  207. }
  208.  
  209. // Function to extract data attributes
  210. function extractDataAttributes(element) {
  211. const attributes = {};
  212. for (let attr of element.attributes) {
  213. if (attr.name.startsWith('data-')) {
  214. attributes[attr.name] = attr.value;
  215. }
  216. }
  217. return attributes;
  218. }
  219.  
  220. // Main script logic
  221. function extractBusinessInfo() {
  222. let businessInfo = {};
  223.  
  224. // Extract business name (original method)
  225. let businessNameElement = document.querySelector('h2[data-attrid="title"]');
  226. if (!businessNameElement) {
  227. // New method for multi-location businesses
  228. businessNameElement = document.querySelector('.PZPZlf[data-attrid="title"]');
  229. }
  230. if (businessNameElement) {
  231. businessInfo['Business Name'] = businessNameElement.textContent.trim();
  232. console.log('%cBusiness name:', 'font-size: 16px; font-weight: bold; color:green', businessInfo['Business Name']);
  233. logToPanel('Business name:', businessInfo['Business Name']);
  234. showPopup('Business information found!');
  235. } else {
  236. console.error("Could not find the business name element on the page");
  237. logToPanel("Could not find the business name element on the page");
  238. showPopup('Failed to find business information', 5000);
  239. }
  240.  
  241. // Extract PID (keep original method)
  242. let reviewButton = document.querySelector("#wrkpb");
  243. if (reviewButton) {
  244. businessInfo['Place ID'] = reviewButton.getAttribute("data-pid");
  245. businessInfo['Place ID URL'] = `https://www.google.com/maps/place/?q=place_id:${businessInfo['Place ID']}`;
  246. businessInfo['Write A Review URL'] = `https://search.google.com/local/writereview?placeid=${businessInfo['Place ID']}`;
  247. console.log('%cPlace ID:', 'font-size: 16px; font-weight: bold; color:green', businessInfo['Place ID']);
  248. console.log('%cPlace ID URL:', 'font-size: 16px; font-weight: bold; color:green', businessInfo['Place ID URL']);
  249. console.log('%cWrite A Review URL:', 'font-size: 16px; font-weight: bold; color:green', businessInfo['Write A Review URL']);
  250. logToPanel('Place ID:', businessInfo['Place ID']);
  251. logToPanel('Place ID URL:', businessInfo['Place ID URL']);
  252. logToPanel('Write A Review URL:', businessInfo['Write A Review URL']);
  253.  
  254. let dataPidElement = document.createElement('div');
  255. dataPidElement.innerText = `PID: ${businessInfo['Place ID']}`;
  256. dataPidElement.style.fontSize = "14px";
  257. dataPidElement.style.color = "red";
  258. businessNameElement.append(dataPidElement);
  259.  
  260. let pidButton = document.createElement('div');
  261. pidButton.className = "QqG1Sd";
  262. pidButton.innerHTML = `<a class="ab_button" href="${businessInfo['Place ID URL']}" role="button" target="_blank"><div>PlaceID</div></a>`;
  263. businessNameElement.append(pidButton);
  264.  
  265. let writeReviewButton = document.createElement('div');
  266. writeReviewButton.className = "QqG1Sd";
  267. writeReviewButton.innerHTML = `<a class="ab_button" href="${businessInfo['Write A Review URL']}" role="button" target="_blank"><div>Write Review</div></a>`;
  268. businessNameElement.append(writeReviewButton);
  269. } else {
  270. console.error("Could not find the 'Write a Review' button on the page");
  271. logToPanel("Could not find the 'Write a Review' button on the page");
  272. showPopup('Failed to find PID information', 5000);
  273. }
  274.  
  275. // Extract CID (multiple methods)
  276. let cid;
  277. let searchResultLink = document.querySelector('a[jscontroller="wuU7pb"]');
  278. if (!searchResultLink) {
  279. searchResultLink = document.querySelector('a[href*="ludocid"]');
  280. }
  281. if (searchResultLink) {
  282. if (searchResultLink.hasAttribute("data-rc_ludocids")) {
  283. cid = searchResultLink.getAttribute("data-rc_ludocids");
  284. } else {
  285. const href = searchResultLink.getAttribute("href");
  286. const match = href.match(/ludocid=(\d+)/);
  287. if (match) {
  288. cid = match[1];
  289. }
  290. }
  291. }
  292. // New method: search for any "a.fl" selector
  293. if (!cid) {
  294. const flLinks = document.querySelectorAll('a.fl');
  295. for (let link of flLinks) {
  296. const href = link.getAttribute("href");
  297. const match = href.match(/ludocid=(\d+)/);
  298. if (match) {
  299. cid = match[1];
  300. break;
  301. }
  302. }
  303. }
  304. if (cid) {
  305. businessInfo['CID'] = cid;
  306. businessInfo['Local Google URL'] = `https://local.google.com/place?id=${businessInfo['CID']}&use=srp`;
  307. businessInfo['Maps Google URL'] = `https://maps.google.com/maps?cid=${businessInfo['CID']}`;
  308. businessInfo['Google Maps URL'] = `https://www.google.com/maps?cid=${businessInfo['CID']}`;
  309. console.log('%cCID:', 'font-size: 16px; font-weight: bold; color:green', businessInfo['CID']);
  310. console.log('%cLocal Google URL:', 'font-size: 16px; font-weight: bold; color:green', businessInfo['Local Google URL']);
  311. console.log('%cMaps Google URL:', 'font-size: 16px; font-weight: bold; color:green', businessInfo['Maps Google URL']);
  312. console.log('%cGoogle Maps URL:', 'font-size: 16px; font-weight: bold; color:green', businessInfo['Google Maps URL']);
  313. logToPanel('CID:', businessInfo['CID']);
  314. logToPanel('Local Google URL:', businessInfo['Local Google URL']);
  315. logToPanel('Maps Google URL:', businessInfo['Maps Google URL']);
  316. logToPanel('Google Maps URL:', businessInfo['Google Maps URL']);
  317.  
  318. // Extract and log all data attributes
  319. if (searchResultLink) {
  320. const dataAttributes = extractDataAttributes(searchResultLink);
  321. console.log('%cData Attributes:', 'font-size: 16px; font-weight: bold; color:green', dataAttributes);
  322. logToPanel('Data Attributes:', JSON.stringify(dataAttributes, null, 2));
  323. }
  324.  
  325. let dataCidElement = document.createElement('div');
  326. dataCidElement.innerText = `CID: ${businessInfo['CID']}`;
  327. dataCidElement.style.fontSize = "14px";
  328. dataCidElement.style.color = "blue";
  329. businessNameElement.append(dataCidElement);
  330.  
  331. let cidButton1 = document.createElement('div');
  332. cidButton1.className = "QqG1Sd";
  333. cidButton1.innerHTML = `<a class="ab_button" href="${businessInfo['Maps Google URL']}" role="button" target="_blank"><div>maps.google</div></a>`;
  334. businessNameElement.append(cidButton1);
  335.  
  336. let cidButton2 = document.createElement('div');
  337. cidButton2.className = "QqG1Sd";
  338. cidButton2.innerHTML = `<a class="ab_button" href="${businessInfo['Local Google URL']}" role="button" target="_blank"><div>local.google</div></a>`;
  339. businessNameElement.append(cidButton2);
  340.  
  341. let cidButton3 = document.createElement('div');
  342. cidButton3.className = "QqG1Sd";
  343. cidButton3.innerHTML = `<a class="ab_button" href="${businessInfo['Google Maps URL']}" role="button" target="_blank"><div>google.com/maps</div></a>`;
  344. businessNameElement.append(cidButton3);
  345. } else {
  346. console.error("Could not find the CID on the page");
  347. logToPanel("Could not find the CID on the page");
  348. showPopup('Failed to find CID information', 5000);
  349. }
  350.  
  351. // Update business info in the panel
  352. updateBusinessInfo(businessInfo);
  353. }
  354.  
  355. // Add event listener for the rescan button
  356. document.getElementById('google-rescan-button').addEventListener('click', function() {
  357. showPopup('Rescanning page...');
  358. extractBusinessInfo();
  359. });
  360.  
  361. // Initial extraction on page load
  362. window.addEventListener("load", extractBusinessInfo);
  363.  
  364. // Observe DOM changes to detect dynamic content loading
  365. const observer = new MutationObserver((mutations) => {
  366. mutations.forEach((mutation) => {
  367. if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
  368. // Check if the added nodes contain relevant business information
  369. mutation.addedNodes.forEach((node) => {
  370. if (node.nodeType === Node.ELEMENT_NODE &&
  371. (node.querySelector('[data-attrid="title"]') ||
  372. node.querySelector('.PZPZlf[data-attrid="title"]') ||
  373. node.querySelector('#wrkpb') ||
  374. node.querySelector('a[jscontroller="wuU7pb"]') ||
  375. node.querySelector('a[href*="ludocid"]'))) {
  376. extractBusinessInfo();
  377. observer.disconnect(); // Stop observing once we've found and processed the information
  378. }
  379. });
  380. }
  381. });
  382. });
  383.  
  384. // Start observing the document body for changes
  385. observer.observe(document.body, { childList: true, subtree: true });
  386. })();