Amazon Batch Tool UI Panel PRO

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==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 = '';
    };

})();