TOPs Google Maps Sort by Reviews

Sort Google Maps places to find tops

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

Bạn sẽ cần cài đặt một tiện ích mở rộng như Tampermonkey hoặc Violentmonkey để cài đặt kịch bản này.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(Tôi đã có Trình quản lý tập lệnh người dùng, hãy cài đặt nó!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         TOPs Google Maps Sort by Reviews
// @name:zh-CN    TOPs Google Maps Sort by Reviews
// @namespace    https://github.com/new4u
// @version      1.3
// @description:  Sort Google Maps places to find TOPs,Unlock the Secret to Finding the Best Local Spots with Our Google Maps Hack! 
// @description:zh-CN  Sort Google Maps places to find TOPs,Unlock the Secret to Finding the Best Local Spots with Our Google Maps Hack! 
// @author       new4u
// @include    *://encrypted.google.*/search*
// @include    *://*.google*/search*
// @include    *://*.google*/webhp*
// @match        *www.google.com/maps*
// @grant        none
// @copyright    2015-2024, new4u
// @license      GPL-3.0-only
// @description Sort Google Maps places to find tops
// ==/UserScript==

(function () {
  'use strict';

  let restaurantNameSelector = ".qBF1Pd.fontHeadlineSmall";
  let ratingSelector = ".UY7F9";
  let ratingCountSelector = ".e4rVHe.fontBodyMedium";
  let priceSelector = ".e4rVHe.fontBodyMedium > span:nth-child(3)";
  let cuisineSelector = ".W4Efsd span:first-child";
  let openHoursSelector = ".W4Efsd span:last-child";
  let restaurantTypeSelector = ".ah5Ghc.fontBodyMedium";
  let titleSelector = "head > title";

  function extractData() {
    let data = {};

    let targetElements = document.querySelectorAll(".TFQHme + *");
    for (let i = 0; i < targetElements.length; i++) {
      let element = targetElements[i];

      let name = element.querySelector(restaurantNameSelector);
      if (name) {
        name = name.innerText;
      } else {
        name = null;
      }

      let rating = element.querySelector(ratingSelector);
      if (rating) {
        rating = rating.innerText;
      } else {
        rating = null;
      }

      let ratingCount = element.querySelector(ratingCountSelector);
      if (ratingCount) {
        ratingCount = ratingCount.innerText;
      } else {
        ratingCount = null;
      }

      let price = element.querySelector(priceSelector);
      if (price) {
        price = price.innerText;
      } else {
        price = null;
      }

      let cuisine = element.querySelector(cuisineSelector);
      if (cuisine) {
        cuisine = cuisine.innerText;
      } else {
        cuisine = null;
      }

      let openHours = element.querySelector(openHoursSelector);
      if (openHours) {
        openHours = openHours.innerText;
      } else {
        openHours = null;
      }

      let restaurantType = element.querySelector(restaurantTypeSelector);
      if (restaurantType) {
        restaurantType = restaurantType.innerText;
      } else {
        restaurantType = null;
      }

      data[name] = {
        rating: rating,
        ratingCount: ratingCount,
        price: price,
        cuisine: cuisine,
        openHours: openHours,
        restaurantType: restaurantType
      };
    }

    let title = document.querySelector(titleSelector).innerText
    console.log(data);
    console.log(title);
    console.log(`Extracted ☆★${Object.keys(data).length}★☆ Spots`);


    // Create an array of restaurant names and ratings
    let ratingsArray = [];
    for (let name in data) {
      let rating = data[name].rating;
      if (rating) {
        // Remove all non-numeric characters except the decimal point from the rating string
        rating = rating.replace(/[^0-9.]/g, '');
        // Convert the rating string to a number
        rating = Number(rating);
      } else {
        // Assign a zero rating to restaurants with no rating
        rating = 0;
      }
      ratingsArray.push([name, rating]);
    }

    // Sort the array by rating in descending order
    ratingsArray.sort(function (a, b) {
      return b[1] - a[1];
    });

    // Slice the first 10 elements of the array
    let top10 = ratingsArray.slice(0, 10);

    // Print the names and ratings of the top 10 restaurants
    for (let i = 0; i < top10.length; i++) {
      console.log(top10[i][0] + ": " + top10[i][1]);
    }
  }

  // Monitor the specific DOM element for updates
  let targetElement = document.querySelector("#QA0Szd > div > div > div.w6VYqd > div:nth-child(2)");
  let previousInnerHTML = targetElement.innerHTML;
  setInterval(function () {
    let currentInnerHTML = targetElement.innerHTML;
    if (currentInnerHTML !== previousInnerHTML) {
      console.log("↓↓↓↓DOM element updated↓↓↓↓");
      previousInnerHTML = currentInnerHTML;
      extractData(); // Call the extractData function when the DOM element updates
    }
  }, 3000); // Check every 1 second

  // Initial call to extractData
  extractData();
})();