BrickLink Suchscript AFOL-Version

Buttons oben rechts + farbige SVG-Icons + Pick-a-Brick mit allen Alternativnummern

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         BrickLink Suchscript AFOL-Version
// @namespace    https://bricklink.com/
// @version      1.0.1
// @description  Buttons oben rechts + farbige SVG-Icons + Pick-a-Brick mit allen Alternativnummern
// @match        https://www.bricklink.com/v2/catalog/catalogitem.page*
// @match        http://www.bricklink.com/v2/catalog/catalogitem.page*
// @run-at       document-end
// @grant        none
// @license      MIT
// @author       Glattnoppe


// ==/UserScript==

(function() {
    'use strict';

    const host = location.hostname;

    // ------------------------------------------------------------
    // SVG ICONS (farbig, hoher Kontrast)
    // ------------------------------------------------------------
    const ICON_PAB =
        "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48Y2lyY2xlIGN4PSIxMiIgY3k9IjEyIiByPSI4IiBmaWxsPSIjRkZEMzAwIiBzdHJva2U9IiNBODcwMDAiIHN0cm9rZS13aWR0aD0iMiIvPjwvc3ZnPg==";

    const ICON_REBRICKABLE =
        "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48Y2lyY2xlIGN4PSIxMCIgY3k9IjEwIiByPSI2IiBzdHJva2U9IiMwMDAiIHN0cm9rZS13aWR0aD0iMiIgZmlsbD0iI2ZmZiIvPjxsaW5lIHgxPSIxNCIgeTE9IjE0IiB4Mj0iMjAiIHkyPSIyMCIgc3Ryb2tlPSIjMDAwIiBzdHJva2Utd2lkdGg9IjIiLz48L3N2Zz4=";

    const ICON_BRICKOWL =
        "data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjQiIGhlaWdodD0iMjQiIHZpZXdCb3g9IjAgMCAyNCAyNCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48Y2lyY2xlIGN4PSIxMiIgY3k9IjEyIiByPSI4IiBmaWxsPSIjRjY2N0ZGIiBzdHJva2U9IiM0NDQiIHN0cm9rZS13aWR0aD0iMiIvPjxjaXJjbGUgY3g9IjkiIGN5PSIxMSIgcj0iMSIgZmlsbD0iIzAwMCIvPjxjaXJjbGUgY3g9IjE1IiBjeT0iMTEiIHI9IjEiIGZpbGw9IiMwMDAiLz48cGF0aCBkPSJNOC41IDE1YzEuNSAxLjUgNS41IDEuNSA3IDBsLTEtMS41Yy0xIDEuNS0zIDEuNS00IDBsLTEgMS41eiIgZmlsbD0iIzQ0NCIvPjwvc3ZnPg==";

    function iconImg(src) {
        return `<img src="${src}" style="width:14px;height:14px;vertical-align:middle;margin-right:4px;">`;
    }

    // ------------------------------------------------------------
    // TEIL A: BRICKLINK
    // ------------------------------------------------------------
    if (host.includes("bricklink.com")) {

        const params = new URLSearchParams(window.location.search);
        const partNumber = params.get("P");
        if (!partNumber) return;

        // ------------------------------------------------------------
        // ALTERNATIVNUMMERN AUSLESEN
        // ------------------------------------------------------------
        let altNumbers = [];

        const altElem = [...document.querySelectorAll("td, div, span, p")]
            .find(el => el.textContent.includes("Alternate Item No:"));

        if (altElem) {
            let text = altElem.textContent;
            const idx = text.indexOf("Alternate Item No:");
            if (idx !== -1) text = text.substring(idx + "Alternate Item No:".length).trim();

            text = text.split("View Price Guide")[0];
            text = text.split("Color")[0];
            text = text.split("\n")[0];

            const ids = text.match(/[0-9A-Za-z-]+/g);
            if (ids) altNumbers = [...new Set(ids.map(n => n.toLowerCase()))];
        }

        const allIDs = [partNumber, ...altNumbers].join("+");

        // ------------------------------------------------------------
        // URL-Generatoren
        // ------------------------------------------------------------
        const pickabrickUrl = `https://www.lego.com/de-de/pick-and-build/pick-a-brick?query=${allIDs}`;
        const rebrickableUrl = `https://rebrickable.com/parts/?get_drill_downs=&tag=&q=${partNumber}&part_cat=`;
        const brickowlUrl = `https://www.brickowl.com/search/catalog?query=${partNumber}&cat=1&variant_type=0`;

        // ------------------------------------------------------------
        // BUTTONS
        // ------------------------------------------------------------
        const topRightCell = document.querySelector("table[width='100%'] td[align='right']");
        if (!topRightCell) return;

        function makeButton(icon, text, url, tooltip) {
            const btn = document.createElement("a");
            btn.href = url;
            btn.target = "_blank";
            btn.innerHTML = `${iconImg(icon)}${text}`;
            btn.className = "blButton blButtonInput blButtonBlue bold";
            btn.style.marginLeft = "6px";
            btn.style.padding = "5px 8px";
            btn.style.textDecoration = "none";
            btn.style.color = "#eee";
            btn.style.fontSize = "12px";
            btn.style.whiteSpace = "nowrap";
            if (tooltip) btn.title = tooltip;
            return btn;
        }

        topRightCell.appendChild(makeButton(ICON_PAB, "LEGO® Pick a Brick", pickabrickUrl, "LEGO® Pick a Brick"));
        topRightCell.appendChild(makeButton(ICON_BRICKOWL, "BrickOwl", brickowlUrl, "BrickOwl"));
        topRightCell.appendChild(makeButton(ICON_REBRICKABLE, "Rebrickable", rebrickableUrl, "Rebrickable"));

        return;
    }

})();