Greasy Fork

Greasy Fork is available in English.

Fanatical Bundle Pricing

3/12/2025, 7:04:09 AM

当前为 2025-03-12 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        Fanatical Bundle Pricing
// @namespace   Violentmonkey Scripts
// @match       https://www.fanatical.com/en/bundle/*
// @match       https://www.fanatical.com/en/pick-and-mix/*
// @grant       none
// @version     1.1.0
// @author      Lex
// @description 3/12/2025, 7:04:09 AM
// ==/UserScript==

const btn = document.createElement("button")
const textArea = document.createElement("textarea")

function convertPriceObjectToTable(priceObj) {
    const currencies = Object.keys(priceObj)
    let table = `  | ${currencies.join(" | ")}\n`
    table += `:- | ${currencies.map(() => ":-:").join(" | ")}\n`
    const row = `Tier 1 | ` + currencies.map(currency => (priceObj[currency] / 100).toFixed(2)).join(" | ")
    return table + row;
}

function convertTierListToTable(tiers) {
    if (tiers.length === 0) return "No data available";
    const currencies = Object.keys(tiers[0].price);
    let table = `Quantity | ${currencies.join(" | ")}\n`;
    table += `:-: | ${currencies.map(() => ":-:").join(" | ")}\n`;
    let rows = tiers.map(tier => {
        return `${tier.quantity} | ` + currencies.map(currency => (tier.price[currency] / 100).toFixed(2)).join(" | ");
    }).join("\n");
    return table + rows;
}

async function handleShowPricing() {
    console.log("Showing pricing")
    const slug = window.preloadedSlug.slice(1, -1)
    const isPickAndMix = Boolean(document.querySelector("div.PickAndMixProductPage__content__shortDesc"))
    const productType = isPickAndMix ? "pick-and-mix" : "products-group"
    const url = `https://www.fanatical.com/api/${productType}/${slug}/en`
    const response = await fetch(url)
    const json = await response.json()
    console.log(json)
    btn.style.display = "none"
    document.querySelector("section.container,div.PickAndMixProductPage__content__shortDesc").append(textArea)
    textArea.style.width = "600px"
    textArea.style.height = "150px"
    if (isPickAndMix)
        textArea.textContent = convertTierListToTable(json.tiers)
    else
        textArea.textContent = convertPriceObjectToTable(json.bundles[0].price)
}


function createButton() {
    btn.type = "button"
    btn.textContent = "Show Pricing"
    btn.addEventListener("click", handleShowPricing)
    const container = document.querySelector("section.container,div.PickAndMixProductPage__content__shortDesc")
    container.append(btn)
}


function main() {
    createButton()
}

function waitForLoad(query, callback) {
    if (document.querySelector(query)) {
        callback()
    } else {
        setTimeout(waitForLoad.bind(null, query, callback), 100)
    }
}

waitForLoad("section.container,div.PickAndMixProductPage__content__shortDesc", main)