Greasy Fork

Greasy Fork is available in English.

ASIN链接生成器

生成ASIN对应的链接并跳转到对应的Amazon站点

当前为 2023-06-14 提交的版本,查看 最新版本

// ==UserScript==
// @name         ASIN链接生成器
// @namespace    ASIN Link Generator
// @version      1.0
// @description  生成ASIN对应的链接并跳转到对应的Amazon站点
// @match        https://www.amazon.*/*
// ==/UserScript==

(function() {
    'use strict';

    // 根据当前站点返回对应的链接前缀
    function getAmazonURLPrefix() {
        const domain = window.location.hostname;
        if (domain.endsWith('.com.mx')) {
            return 'https://www.amazon.com.mx/';
        } else if (domain.endsWith('.co.uk')) {
            return 'https://www.amazon.co.uk/';
        } else if (domain.endsWith('.de')) {
            return 'https://www.amazon.de/';
        } else if (domain.endsWith('.fr')) {
            return 'https://www.amazon.fr/';
        } else if (domain.endsWith('.it')) {
            return 'https://www.amazon.it/';
        } else if (domain.endsWith('.es')) {
            return 'https://www.amazon.es/';
        } else if (domain.endsWith('.ca')) {
            return 'https://www.amazon.ca/';
        } else {
            return 'https://www.amazon.com/';
        }
    }

    // 创建一个按钮元素并添加到页面上
    function createButton(text, clickHandler) {
        const button = document.createElement('button');
        button.style.position = 'fixed';
        button.style.left = '20px';
        button.style.top = text === '打开ASIN' ? '50%' : '55%';
        button.style.transform = 'translateY(-50%)';
        button.textContent = text;
        button.addEventListener('click', clickHandler);
        document.body.appendChild(button);
    }

    // 点击“打开ASIN”按钮的处理函数
    function openASINHandler() {
        const asin = prompt('请输入ASIN');
        if (asin) {
            const urlPrefix = getAmazonURLPrefix();
            const url = urlPrefix + 'dp/' + asin;
            window.location.href = url;
        }
    }

    // 点击“打开批量ASIN”按钮的处理函数
    function openBatchASINHandler() {
        const asins = prompt('请输入批量ASIN,以换行符分隔');
        if (asins) {
            const asinArray = asins.split('\n');
            const asinQuery = asinArray.join('%7C');
            const urlPrefix = getAmazonURLPrefix();
            const url = urlPrefix + 's?rh=p_78%3A' + asinQuery;
            window.location.href = url;
        }
    }

    // 创建并添加按钮到页面上
    createButton('打开ASIN', openASINHandler);
    createButton('打开批量ASIN', openBatchASINHandler);
})();