Greasy Fork

Greasy Fork is available in English.

ASIN Link Generator

Generates ASIN and brand search links for Amazon websites

目前为 2025-05-19 提交的版本。查看 最新版本

// ==UserScript==
// @name         ASIN Link Generator
// @namespace    asin-link-generator
// @version      1.1
// @description  Generates ASIN and brand search links for Amazon websites
// @match        *://*.amazon.com/*
// @match        *://*.amazon.ca/*
// @match        *://*.amazon.co.uk/*
// @match        *://*.amazon.de/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';

    // Helper: Get base URL by domain
    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;
    }

    // Common style for buttons
    const commonStyle = `
        position: fixed;
        left: 20px;
        padding: 10px 12px;
        background-color: #FF9900;
        color: white;
        border: none;
        border-radius: 5px;
        cursor: pointer;
        z-index: 9999;
    `;

    // Create buttons
    const openASINButton = document.createElement('button');
    const openBatchASINButton = document.createElement('button');
    const openBrandBatchButton = document.createElement('button');

    openASINButton.textContent = '打开ASIN详情页';
    openBatchASINButton.textContent = '打开批量ASIN';
    openBrandBatchButton.textContent = '批量打开品牌';

    openASINButton.style.cssText = commonStyle + 'top: 45%; transform: translateY(-50%);';
    openBatchASINButton.style.cssText = commonStyle + 'top: calc(45% + 50px); transform: translateY(-50%);';
    openBrandBatchButton.style.cssText = commonStyle + 'top: calc(45% + 100px); transform: translateY(-50%);';

    document.body.appendChild(openASINButton);
    document.body.appendChild(openBatchASINButton);
    document.body.appendChild(openBrandBatchButton);

    // 打开单个ASIN
    openASINButton.addEventListener('click', function () {
        const asin = prompt('请输入ASIN:');
        if (asin && /^[A-Z0-9]{10}$/i.test(asin.trim())) {
            const base = getAmazonBaseURL();
            if (base) window.open(`${base}/dp/${asin.trim()}`);
        } else {
            alert('请输入有效的ASIN(10位字母或数字)');
        }
    });

    // 打开多个ASIN(使用search)
    openBatchASINButton.addEventListener('click', function () {
        const asins = prompt('请输入多个ASIN,以换行符分隔:');
        if (asins) {
            const base = getAmazonBaseURL();
            if (base) {
                const query = asins.split('\n').map(s => s.trim()).filter(Boolean).join('%7C');
                const url = `${base}/s?rh=p_78%3A${query}`;
                window.open(url);
            }
        }
    });

    // 批量打开品牌搜索
    openBrandBatchButton.addEventListener('click', function () {
        const brands = prompt('请输入多个品牌名称,以换行符分隔:');
        if (brands) {
            const base = getAmazonBaseURL();
            if (base) {
                const brandList = brands.split('\n').map(b => b.trim()).filter(Boolean);
                brandList.forEach(brand => {
                    const encodedBrand = encodeURIComponent(brand);
                    const url = `${base}/s?rh=p_4%3A${encodedBrand}`;
                    window.open(url);
                });
            }
        }
    });
})();