Discogs - Sort by Total Price on Release Page

Sort by the italicized 'total' price on a Discogs release page instead of pre-shipping price

目前为 2023-03-09 提交的版本。查看 最新版本

// ==UserScript==
// @name         Discogs - Sort by Total Price on Release Page
// @namespace    https://gf.qytechs.cn/
// @version      0.5
// @description  Sort by the italicized 'total' price on a Discogs release page instead of pre-shipping price
// @author       Jon Uleis (@MovingToTheSun)
// @match        https://www.discogs.com/sell/*
// @icon         https://st.discogs.com/9d83c5b6b923bb0237d914210250e5a432a026de/images/favicon.ico
// @grant        none
// @license MIT 
// ==/UserScript==
/*jshint esversion: 11 */

(function () {
  "use strict";
  const priceSort = document.querySelector(".price_header .sortable_link_selected");
  const ascending = priceSort?.title.includes("ascending");

  function tableSort() {
    const rows = Array.from(document.querySelectorAll("tr[data-release-id]"));
    // continue only if we are sorting by price
    if (priceSort) {
      rows.sort((rowA, rowB) => {
        const priceA = getRowPrice(rowA);
        const priceB = getRowPrice(rowB);
        return ascending ? priceA - priceB : priceB - priceA;
      });
      // append back to table in new order
      rows.forEach((row) => row.parentNode.appendChild(row));
      // change title text
      priceSort.querySelector(".link-text").innerText = "Total Price";
    }
  }

  function getRowPrice(row) {
    // if there's no total price, get the original bold one
    const price = row.querySelector(".converted_price") || row.querySelector(".price");
    // if the item is unavailable (no Cart button), sort to bottom
    const weight = row.querySelector(".cart-button") ? 0 : 9999999;
    // strip everything else out of the price text 
    return parseFloat(price.textContent.replace(/[^0-9\.]/g, "")) + weight;
  }

  const tableBlock = document.querySelector(".table_block");
  const observer = new MutationObserver((mutationsList) => {
    for (let mutation of mutationsList) {
      // run function again if we're ajax loading in another table of items
      if (mutation.type === "childList" && mutation.addedNodes.length > 0 && mutation.addedNodes[0].nodeName === "TBODY") {
        tableSort();
      }
    }
  });
  observer.observe(tableBlock, { childList: true });

  // first run
  tableSort();
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址