Greasy Fork

Wishlist Steal Deal Checker

A user script to check if the wishlist item has best price

当前为 2023-06-21 提交的版本,查看 最新版本

// ==UserScript==
// @name         Wishlist Steal Deal Checker
// @namespace    https://greasyfork.org/en/users/1019658-aayush-dutt
// @version      0.4
// @description  A user script to check if the wishlist item has best price
// @author       aayushdutt

// @match        https://www.amazon.com/hz/wishlist/ls/*
// @match        https://www.amazon.in/hz/wishlist/ls/*

// @match        https://www.amazon.de/hz/wishlist/ls/*
// @match        https://www.amazon.fr/hz/wishlist/ls/*
// @match        https://www.amazon.it/hz/wishlist/ls/*
// @match        https://www.amazon.es/hz/wishlist/ls/*
// @match        https://www.amazon.nl/hz/wishlist/ls/*
// @match        https://www.amazon.se/hz/wishlist/ls/*
// @match        https://www.amazon.co.jp/hz/wishlist/ls/*
// @match        https://www.amazon.co.uk/hz/wishlist/ls/*
// @match        https://www.amazon.com.mx/hz/wishlist/ls/*
// @match        https://www.amazon.com.au/hz/wishlist/ls/*
// @match        https://www.amazon.com.be/hz/wishlist/ls/*

// @grant        none
// @link         https://greasyfork.org/en/scripts/468955-wishlist-steal-deal-checker
// @license      MIT
// ==/UserScript==

(function () {
  "use strict";
  /**
   * Retrieves all nodes matching the specified selector.
   * @returns {HTMLElement[]} An array of nodes.
   */
  const getAllNodes = () => {
    return Array.from(document.querySelectorAll("ul#g-items > li[data-id]"));
  };

  /**
   * Parses the nodes and extracts relevant information.
   * @returns {Object[]} An array of parsed node objects.
   */
  const parseNodes = () => {
    /**
     * Retrieves the price attribute value from the given node.
     * @param {HTMLElement} node - The node element.
     * @returns {number} The price value.
     */
    const getPrice = (node) => {
      return parseInt(node.getAttribute("data-price"));
    };

    /**
     * Retrieves the comment node associated with the given node.
     * @param {HTMLElement} node - The node element.
     * @returns {HTMLElement|null} The comment node or null if not found.
     */
    const getCommentNode = (node) => {
      return node.querySelector(
        'span[data-csa-c-element-id="list-desktop-wishlist-item-info-cqp-comment"]'
      );
    };

    /**
     * Extracts the comment prices from the comment node.
     * @param {HTMLElement|null} node - The comment node.
     * @returns {number[]} An array of comment prices.
     */
    const getCommentPrices = (node) => {
      const commentText = node ? node.innerText.trim() : "";
      if (!commentText) return [];
      return commentText.split(" ").map((e) => parseInt(e));
    };

    /**
     * Checks if the given price is the best price among the comment prices.
     * @param {number} price - The price to compare.
     * @param {number[]} commentPrices - An array of comment prices.
     * @returns {number} -1 if bad price, 0 if equal to prev best, 1 if the price beats all comment prices
     */
    const getIsBestPrice = (price, commentPrices) => {
      if (!commentPrices || !commentPrices.length || !price) return false;

      const minCommentPrice = Math.min(...commentPrices);
      if (price === minCommentPrice) {
        return 0;
      }
      if (price > minCommentPrice) return -1;
      if (price < minCommentPrice) return 1;
    };

    const nodes = getAllNodes();

    return nodes.map((node) => {
      const price = getPrice(node);
      const commentNode = getCommentNode(node);
      const commentPrices = getCommentPrices(commentNode);
      const priceComparison = getIsBestPrice(price, commentPrices);

      return {
        node,
        price,
        commentPrices,
        priceComparison,
        commentNode,
      };
    });
  };

  /**
   * Updates the style of the best price nodes.
   * @param {Object[]} wishlistItems - An array of wishlist items.
   */
  const updateBestNodes = (wishlistItems) => {
    wishlistItems.forEach((item) => {
      if (item.priceComparison === 0) {
        // Equal to prev best
        item.node.style.background = "#f1fffe";
      } else if (item.priceComparison === 1) {
        // Better than prev best
        item.node.style.background = "#d0fbe4";
      }
    });
  };

  const targetNode = document.getElementById("g-items");
  const config = { childList: true };

  /**
   * The mutation callback function.
   * @param {MutationRecord[]} mutationList - The list of mutations.
   * @param {MutationObserver} observer - The MutationObserver instance.
   */
  const callback = (mutationList, observer) => {
    console.log("[Wishlist Steal Deal Checker] list changed");
    const wishlistItems = parseNodes();
    updateBestNodes(wishlistItems);
  };

  callback([], null);
  const observer = new MutationObserver(callback);
  observer.observe(targetNode, config);
})();