Google Maps Sort by Reviews

Sort Google Maps places by number of reviews

当前为 2024-04-26 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

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