Google Maps Sort by Reviews

Sort Google Maps places by number of reviews

As of 2024-04-26. See the latest version.

// ==UserScript==
// @name         Google Maps Sort by Reviews
// @namespace    new4u
// @version      0.1
// @description  Sort Google Maps places by number of reviews
// @author       Bing
// @include    *://encrypted.google.*/search*
// @include    *://*.google*/search*
// @include    *://*.google*/webhp*
// @match        *www.google.com/maps*
// @grant        none
// @license MIT 
// ==/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 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
    };
  }

  console.log(data);

  // 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]);
  }
})();