Fanatical Keys Backup

Displays a text area with game titles and keys so you can copy them out easily.

目前為 2020-05-15 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Fanatical Keys Backup
// @namespace    Lex@GreasyFork
// @version      0.1
// @description  Displays a text area with game titles and keys so you can copy them out easily.
// @author       Lex
// @match        https://www.fanatical.com/en/orders/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Formats games array to a string to be displayed
    // Games is an array [ [title, key], ... ]
    function formatGames(games) {
        // Ignore games which do not have keys revealed
        games = games.filter(e => e[1]);
        // Format the output as tab-separated
        games = games.map(e => e[0]+"\t"+e[1]);
        return games.join("\n");
    }

    function getGames(bundle) {
        let is = bundle.querySelectorAll(".order-item");
        return Array.prototype.map.call(is, i => {
            const gameTitleElement = i.getElementsByClassName("game-name");
            const gameTitle = gameTitleElement.length > 0 ? gameTitleElement[0].textContent.trim() : "";
            const keyElement = i.querySelector("[aria-label='reveal-key']");
            const gameKey = keyElement ? keyElement.value : "";
            return [gameTitle, gameKey];
        });
    }

    // Adds a textarea to the bottom of the games listing with all the titles and keys
    function handleBundle(bundle) {
        const bundleName = bundle.querySelector("h5.my-4") ? bundle.querySelector("h5.my-4").textContent.trim() : "No Title";
        let games = getGames(bundle);
        const gameCount = games.length;
        const keyCount = games.filter(e => e[1]).length;
        const gameStr = formatGames(games);

        let notify = document.createElement("div");
        notify.className = "ktt-notify";
        const color = gameCount == keyCount ? "" : "red";
        notify.innerHTML = `Dumping keys for ${bundleName}: Found ${gameCount} items and <span style="background-color:${color}">${keyCount} keys</span>.`;
        if (gameCount != keyCount) {
            notify.innerHTML += " Are some keys not revealed?";
        }
        bundle.append(notify);

        // Find an old textarea on the active bundle
        let area = bundle.querySelector("textarea.ktt");
        // otherwise make a new one
        if (!area) {
            area = document.createElement("textarea");
            area.className = "ktt";
            bundle.append(area);
        }

        area.value = gameStr;
        area.style.width = "100%";
        // Adjust the height so all the contents are visible
        area.style.height = "";
        area.style.height = area.scrollHeight + 20 + "px";
    }

    var loopCount = 0;
    function handleOrderPage() {
        // There can be more than one bundle in an order
        const bundles = document.querySelectorAll("hr.mb-4 ~ div");
        if (bundles.length > 0) {
            console.log(`Found ${bundles.length} bundle(s)`);
            bundles.forEach(handleBundle);
        } else {
            if (loopCount++ < 100) {
                setTimeout(handleOrderPage, 100);
            }
        }
    }

    handleOrderPage();
})();