TOPs Google Maps Sort by Reviews

Sort Google Maps places to find tops

  1. // ==UserScript==
  2. // @name TOPs Google Maps Sort by Reviews
  3. // @name:zh-CN TOPs Google Maps Sort by Reviews
  4. // @namespace https://github.com/new4u
  5. // @version 1.3
  6. // @description: Sort Google Maps places to find TOPs,Unlock the Secret to Finding the Best Local Spots with Our Google Maps Hack!
  7. // @description:zh-CN Sort Google Maps places to find TOPs,Unlock the Secret to Finding the Best Local Spots with Our Google Maps Hack!
  8. // @author new4u
  9. // @include *://encrypted.google.*/search*
  10. // @include *://*.google*/search*
  11. // @include *://*.google*/webhp*
  12. // @match *www.google.com/maps*
  13. // @grant none
  14. // @copyright 2015-2024, new4u
  15. // @license GPL-3.0-only
  16. // @description Sort Google Maps places to find tops
  17. // ==/UserScript==
  18.  
  19. (function () {
  20. 'use strict';
  21.  
  22. let restaurantNameSelector = ".qBF1Pd.fontHeadlineSmall";
  23. let ratingSelector = ".UY7F9";
  24. let ratingCountSelector = ".e4rVHe.fontBodyMedium";
  25. let priceSelector = ".e4rVHe.fontBodyMedium > span:nth-child(3)";
  26. let cuisineSelector = ".W4Efsd span:first-child";
  27. let openHoursSelector = ".W4Efsd span:last-child";
  28. let restaurantTypeSelector = ".ah5Ghc.fontBodyMedium";
  29. let titleSelector = "head > title";
  30.  
  31. function extractData() {
  32. let data = {};
  33.  
  34. let targetElements = document.querySelectorAll(".TFQHme + *");
  35. for (let i = 0; i < targetElements.length; i++) {
  36. let element = targetElements[i];
  37.  
  38. let name = element.querySelector(restaurantNameSelector);
  39. if (name) {
  40. name = name.innerText;
  41. } else {
  42. name = null;
  43. }
  44.  
  45. let rating = element.querySelector(ratingSelector);
  46. if (rating) {
  47. rating = rating.innerText;
  48. } else {
  49. rating = null;
  50. }
  51.  
  52. let ratingCount = element.querySelector(ratingCountSelector);
  53. if (ratingCount) {
  54. ratingCount = ratingCount.innerText;
  55. } else {
  56. ratingCount = null;
  57. }
  58.  
  59. let price = element.querySelector(priceSelector);
  60. if (price) {
  61. price = price.innerText;
  62. } else {
  63. price = null;
  64. }
  65.  
  66. let cuisine = element.querySelector(cuisineSelector);
  67. if (cuisine) {
  68. cuisine = cuisine.innerText;
  69. } else {
  70. cuisine = null;
  71. }
  72.  
  73. let openHours = element.querySelector(openHoursSelector);
  74. if (openHours) {
  75. openHours = openHours.innerText;
  76. } else {
  77. openHours = null;
  78. }
  79.  
  80. let restaurantType = element.querySelector(restaurantTypeSelector);
  81. if (restaurantType) {
  82. restaurantType = restaurantType.innerText;
  83. } else {
  84. restaurantType = null;
  85. }
  86.  
  87. data[name] = {
  88. rating: rating,
  89. ratingCount: ratingCount,
  90. price: price,
  91. cuisine: cuisine,
  92. openHours: openHours,
  93. restaurantType: restaurantType
  94. };
  95. }
  96.  
  97. let title = document.querySelector(titleSelector).innerText
  98. console.log(data);
  99. console.log(title);
  100. console.log(`Extracted ☆★${Object.keys(data).length}★☆ Spots`);
  101.  
  102.  
  103. // Create an array of restaurant names and ratings
  104. let ratingsArray = [];
  105. for (let name in data) {
  106. let rating = data[name].rating;
  107. if (rating) {
  108. // Remove all non-numeric characters except the decimal point from the rating string
  109. rating = rating.replace(/[^0-9.]/g, '');
  110. // Convert the rating string to a number
  111. rating = Number(rating);
  112. } else {
  113. // Assign a zero rating to restaurants with no rating
  114. rating = 0;
  115. }
  116. ratingsArray.push([name, rating]);
  117. }
  118.  
  119. // Sort the array by rating in descending order
  120. ratingsArray.sort(function (a, b) {
  121. return b[1] - a[1];
  122. });
  123.  
  124. // Slice the first 10 elements of the array
  125. let top10 = ratingsArray.slice(0, 10);
  126.  
  127. // Print the names and ratings of the top 10 restaurants
  128. for (let i = 0; i < top10.length; i++) {
  129. console.log(top10[i][0] + ": " + top10[i][1]);
  130. }
  131. }
  132.  
  133. // Monitor the specific DOM element for updates
  134. let targetElement = document.querySelector("#QA0Szd > div > div > div.w6VYqd > div:nth-child(2)");
  135. let previousInnerHTML = targetElement.innerHTML;
  136. setInterval(function () {
  137. let currentInnerHTML = targetElement.innerHTML;
  138. if (currentInnerHTML !== previousInnerHTML) {
  139. console.log("↓↓↓↓DOM element updated↓↓↓↓");
  140. previousInnerHTML = currentInnerHTML;
  141. extractData(); // Call the extractData function when the DOM element updates
  142. }
  143. }, 3000); // Check every 1 second
  144.  
  145. // Initial call to extractData
  146. extractData();
  147. })();