Card_Kingdom_Hotlist_Display

Security through obfuscation is bad, m'kay.

当前为 2022-06-09 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Card_Kingdom_Hotlist_Display
// @namespace    https://*cardkingdom.com/
// @version      0.10
// @description  Security through obfuscation is bad, m'kay.
// @author       Halt_I_m_Reptar (MtG Cabal Cast)
// @match        https://*.cardkingdom.com/
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    createDataRequestButton();

    function createDataRequestButton() {
        const div = document.createElement('div');
        div.innerHTML = ('<button class="dataRequestButton" style="position:fixed;top:0;left:0;z-index:9999;width:auto;height:20px;padding:0 5px 0 0;background:#00b;color:#fff;font-weight:bold;" onclick="beginDisplay()">Gather Sales Data</button>');
        document.body.prepend(div);
    }

    //write toggle button
    const createHotlistToggleButton = () => {
        const div = document.createElement('div');
        div.innerHTML = ('<button class="hotlistToggle" style="position:fixed;top:20px;left:0;z-index:999;width:120px;height:20px;padding:0 5px 0 0;background:#0b0;color:#fff;font-weight:bold;" onclick="toggleHotlistDisplay()">Toggle Hotlist</button>');
        document.body.prepend(div);
    }

    const displayHotlistData = (hotlistMap) => {
        const div = document.getElementsByClassName('hotlistContainer')[0];
        hotlistMap.forEach( itemInfo => {
            div.style.height = adjustHeight(div);
            div.innerHTML += itemInfo;
        });
    }

    const writeHotlistDataContainer = () => {
        const div = document.createElement('div');
        div.innerHTML = ('<div class="hotlistContainer" style="position:fixed;bottom:0;left:0;z-index:8888;width:auto;height:300px;max-height:600px;overflow-y:scroll;padding:0 5px 0 0;border:1px solid #d00;background:#fff"></div>');
        document.body.prepend(div);

    }

    window.beginDisplay = function() {
        clearHtmlElements();
        writeHotlistDataContainer();
        displayHotlistData(createReadableList(mapHotlistData(fetchHotlistData())));
        createHotlistToggleButton();
    }

    window.toggleHotlistDisplay = function() {
        const display = document.getElementsByClassName('hotlistContainer')[0].style.display;
        document.getElementsByClassName('hotlistContainer')[0].style.display = display === 'none' ? 'inline' : 'none';
    }

    const fetchHotlistData = () => Array.from(document.getElementsByClassName("slider-item-info"));

    const mapHotlistData = (slideArr) => {
        return slideArr.map( (elem) => {
            if (elem.innerText.match(/:/g)) {
                return elem.innerText.replace(/[\n\r]+/g,'--').replace(/:([^:]*):\s/g,'$1--');
            }
            return elem.innerText.replace(/[\n\r]+/g,'--').replace(": ","--")
        });
    }

    const createReadableList = (cardList) => {
        return cardList.map(elem => {
            const tempName = elem.split("--");
            return `<a href="https://www.cardkingdom.com/purchasing/mtg_singles?search=header&filter%5Bname%5D=${tempName[1]}" target="_blank">${tempName[1]} - ${tempName[0]} - ${tempName[2]}</a><br />`;
        }).sort();
    }

    const adjustHeight = (div) => (parseInt(div.style.height.replace(/[a-zA-Z]/g,'')) + 20) + "px";

    const clearHtmlElements = () => {
        const htmlElementsToClear = ['hotlistContainer', 'hotlistToggle'];
        htmlElementsToClear.forEach( selector => {
            if ( document.getElementsByClassName(selector)[0] ) { document.getElementsByClassName(selector)[0].remove(); }
        });
    }
})();