Greasy Fork

Fanatical Bundle Pricing

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

目前为 2025-03-12 提交的版本。查看 最新版本

// ==UserScript==
// @name        Fanatical Bundle Pricing
// @namespace   Violentmonkey Scripts
// @match       https://www.fanatical.com/en/bundle/*
// @grant       none
// @version     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;
}


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


function createButton() {
    btn.type = "button"
    btn.textContent = "Show Pricing"
    btn.addEventListener("click", handleShowPricing)
    const container = document.querySelector("section.container")
    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", main)