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

})();