Amazon Batch Tool UI Panel PRO

UI面板版:批量ASIN/品牌工具

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Amazon Batch Tool UI Panel PRO
// @namespace    asin-link-generator
// @version      3.0
// @description  UI面板版:批量ASIN/品牌工具
// @match        *://*.amazon.com/*
// @match        *://*.amazon.ca/*
// @match        *://*.amazon.co.uk/*
// @match        *://*.amazon.de/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';

    const MAX_TABS = 20;

    function getAmazonBaseURL() {
        const host = location.hostname;
        if (host.endsWith('.com')) return 'https://www.amazon.com';
        if (host.endsWith('.ca')) return 'https://www.amazon.ca';
        if (host.endsWith('.co.uk')) return 'https://www.amazon.co.uk';
        if (host.endsWith('.de')) return 'https://www.amazon.de';
        return null;
    }

    function extractASINs(input) {
        const matches = input.match(/[A-Z0-9]{10}/gi) || [];
        return [...new Set(matches.map(a => a.toUpperCase()))];
    }

    function splitBrands(input) {
        return [...new Set(
            input.split(/[\n,;|]+/)
                .map(b => b.trim())
                .filter(Boolean)
        )];
    }

    function openLinks(urls) {
        if (urls.length > MAX_TABS) {
            alert(`最多打开 ${MAX_TABS} 个标签页`);
            urls = urls.slice(0, MAX_TABS);
        }
        urls.forEach(url => window.open(url));
    }

    // ===== UI 面板 =====
    const panel = document.createElement('div');
    panel.style = `
        position: fixed;
        right: 20px;
        top: 50px;
        width: 280px;
        background: white;
        border: 1px solid #ddd;
        border-radius: 10px;
        padding: 15px;
        z-index: 9999;
        box-shadow: 0 4px 12px rgba(0,0,0,0.2);
        font-size: 14px;
    `;

    panel.innerHTML = `
        <div style="font-weight:bold;margin-bottom:10px;">Batch Tool PRO</div>
        <textarea id="inputBox" placeholder="输入ASIN/品牌(支持任意格式)" style="width:100%;height:80px;margin-bottom:10px;"></textarea>
        <button id="btnDetail" style="width:100%;margin-bottom:5px;">打开详情页</button>
        <button id="btnSearch" style="width:100%;margin-bottom:5px;">ASIN搜索</button>
        <button id="btnBrand" style="width:100%;margin-bottom:5px;">品牌搜索</button>
        <button id="btnClear" style="width:100%;">清空</button>
    `;

    document.body.appendChild(panel);

    const inputBox = document.getElementById('inputBox');

    document.getElementById('btnDetail').onclick = () => {
        const base = getAmazonBaseURL();
        const asins = extractASINs(inputBox.value);
        if (base && asins.length) {
            openLinks(asins.map(a => `${base}/dp/${a}`));
        } else {
            alert('未识别ASIN');
        }
    };

    document.getElementById('btnSearch').onclick = () => {
        const base = getAmazonBaseURL();
        const asins = extractASINs(inputBox.value);
        if (base && asins.length) {
            const query = asins.join('%7C');
            window.open(`${base}/s?rh=p_78%3A${query}`);
        } else {
            alert('未识别ASIN');
        }
    };

    document.getElementById('btnBrand').onclick = () => {
        const base = getAmazonBaseURL();
        const brands = splitBrands(inputBox.value);
        if (base && brands.length) {
            openLinks(brands.map(b => `${base}/s?rh=p_4%3A${encodeURIComponent(b)}`));
        }
    };

    document.getElementById('btnClear').onclick = () => {
        inputBox.value = '';
    };

})();