Greasy Fork

Wishlist Steal Deal Checker

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

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

// ==UserScript==
// @name         Wishlist Steal Deal Checker
// @namespace    https://greasyfork.org/en/users/1019658-aayush-dutt
// @version      0.3
// @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";
  const getAllNodes = () => {
    return Array.from(document.querySelectorAll("ul#g-items > li[data-id]"));
  };

  const parseNodes = () => {
    const nodes = getAllNodes();

    const getPrice = (node) => {
      return parseInt(node.getAttribute("data-price"));
    };

    const getCommentNode = (node) => {
      return node.querySelector(
        'span[data-csa-c-element-id="list-desktop-wishlist-item-info-cqp-comment"]'
      );
    };

    const getCommentPrices = (node) => {
      const commentText = node.innerText.trim();
      if (!commentText) return [];
      return commentText.split(" ").map((e) => parseInt(e));
    };

    const getIsBestPrice = (price, commentPrices) => {
      if (!commentPrices || !commentPrices.length || !price) return false;

      for (let idx in commentPrices) {
        if (!commentPrices[idx]) continue;

        if (price > commentPrices[idx]) {
          return false;
        }
      }
      return true;
    };

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

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

  const updateBestNodes = (wishlistItems) => {
    wishlistItems.forEach(
      (item) => item.isBestPrice && (item.node.style.background = "#f1fff8")
    );
  };

  const targetNode = document.getElementById("g-items");
  const config = { childList: true };
  const callback = (mutationList, observer) => {
    console.log("[Wishlist Steal Deal Checker] list changed");
    const wishlistItems = parseNodes();
    updateBestNodes(wishlistItems);
  };

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