Greasy Fork

来自缓存

Greasy Fork is available in English.

Neopets - Mark Collectable Cards Owned

Mark collectable cards owned (NeoDeck)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Neopets - Mark Collectable Cards Owned
// @version      1.0.1
// @description  Mark collectable cards owned (NeoDeck)
// @author       ikarus
// @match        *://www.neopets.com/games/neodeck/index.phtml*
// @match        *://*.neopets.com/inventory.phtml
// @match        *://*.neopets.com/objects.phtml?*
// @match        *://*.neopets.com/browseshop.phtml?*
// @match        *://*.neopets.com/safetydeposit.phtml*
// @run-at       document-end
// @namespace http://greasyfork.icu/users/1588069
// ==/UserScript==

(function() {
'use strict';

const STORAGE_KEY = 'np_tcg_cards';

// =========================
// STORAGE
// =========================

const cardsStorage = {
    get() {
        return JSON.parse(localStorage.getItem(STORAGE_KEY)) || {
            neodeck: [],
            tcg: []
        };
    },

    set(data) {
        localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
    },

    all() {
        const data = this.get();
        return [...data.neodeck, ...data.tcg];
    }
};

// =========================
// NOME LIMPO
// =========================

function cleanName(name) {
    return name.split('(')[0].trim();
}

// =========================
// COLETA - NEODECK
// =========================

function collectNeoDeck() {
    const data = cardsStorage.get();

    const cards = [];

    document.querySelectorAll('b').forEach(el => {
        const name = cleanName(el.textContent);

        if (name.length > 2 && !cards.includes(name)) {
            cards.push(name);
        }
    });

    data.neodeck = cards;
    cardsStorage.set(data);

    const info = document.createElement('div');
    info.innerHTML = `<b>TCG:</b> ${cards.length} cartas registradas`;
    info.style.fontSize = '18px';
    info.style.margin = '10px';

    document.body.prepend(info);
}

// =========================
// MARCAR ITENS
// =========================

function markCards(elements, table = false) {
    elements.each(function() {
        const el = $(this);
        const name = cleanName(el.text());

        const isOwned = cardsStorage.all().includes(name);

        if (isOwned) {
            el.css('text-decoration', 'line-through');
            el.parent().css('opacity', '50%');

            if (table) {
                el.parent().parent().find('img').eq(0).css('opacity', '50%');
            }
        }
    });
}

// =========================
// PÁGINAS
// =========================

const pages = [
    {
        name: 'inventory',
        pageMatcher: /inventory/,
        itemNameObject: '.item-name'
    },
    {
        name: 'neopian shop',
        pageMatcher: /type=shop/,
        itemNameObject: $('.item-name')
    },
    {
        name: 'user shop',
        pageMatcher: /browseshop/,
        itemNameObject: $('a[href*=buy_item] + br + b')
    },
    {
        name: 'sdb',
        pageMatcher: /safetydeposit/,
        itemNameObject: $('.content form>table').eq(1).find('tr:not(:first-child):not(:last-child) td:nth-child(2) > b'),
        table: true
    }
];

// =========================
// INIT
// =========================

const url = window.location.href;

if (url.includes('neodeck')) {
    collectNeoDeck();
}
else if (localStorage.getItem(STORAGE_KEY)) {
    const page = pages.find(p => url.match(p.pageMatcher));
    if (!page) return;

    if (page.name === 'inventory') {
        $(document).on('ajaxSuccess', () => {
            markCards($(page.itemNameObject));
        });
    } else {
        markCards(page.itemNameObject, page.table);
    }
}

})();