Greasy Fork

Greasy Fork is available in English.

复制ParentAsin

复制亚马逊ParentAsin

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         复制ParentAsin
// @namespace    http://tampermonkey.net/
// @version      0.7
// @description  复制亚马逊ParentAsin
// @author       You
// @match        https://www.amazon.com/*/dp/*
// @match        https://www.amazon.com/gp/product/*
// @match        https://www.amazon.com/dp/*
// @match        https://yuanbao.tencent.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=amazon.com
// @grant        GM.setClipboard
// ==/UserScript==

function copyAsin() {
    const asin = getAsin();
    GM.setClipboard(asin);
    alert('已复制!')
}

function copyTitle() {
    const title = getTitle();
    const regex = /"parentAsin":"(.*?)"/; // 非贪婪匹配,避免匹配到多余的字符
    const asin = getAsin();
    const content = asin + "|x|" + title;
    GM.setClipboard(content);
    alert('已复制!')
}

function copyCookie() {
    GM.setClipboard(document.cookie);
    alert('已复制!')
}

function getAsin() {
    const regex = /"parentAsin":"(.*?)"/; // 非贪婪匹配,避免匹配到多余的字符
    const result = document.body.textContent.match(regex);
    let asin = '';
    if (result && result[1]) {
        asin = result[1];
    }
    return asin;
}

function getBrand() {
    // 辅助函数:执行XPath查询
    function xpathQuery(xpath) {
        const result = [];
        const xpathResult = document.evaluate(
            xpath,
            document,
            null,
            XPathResult.ORDERED_NODE_ITERATOR_TYPE,
            null
        );

        let node = xpathResult.iterateNext();
        while (node) {
            result.push(node.textContent);
            node = xpathResult.iterateNext();
        }
        return result;
    }

    // 首先尝试从主要位置获取品牌
    let brand = xpathQuery('//tr[@class="a-spacing-small po-brand"]/td[2]/span/text()');

    // 如果没有找到,尝试从bylineInfo获取
    if (brand.length === 0) {
        try {
            const bylineInfo = xpathQuery('//a[@id="bylineInfo"]/text()')[0];
            if (bylineInfo) {
                if (bylineInfo.includes('Visit the')) {
                    const match = bylineInfo.match(/Visit the (.+?) Store/);
                    brand = match ? match[1] : "";
                } else if (bylineInfo.includes('Brand: ')) {
                    brand = bylineInfo.replace('Brand: ', '');
                } else {
                    brand = "";
                }
            }
        } catch (error) {
            brand = "";
        }
    }
    // 如果还没找到,从商品详情中获取
    if (!brand.length) {
        const productDetails = xpathQuery('//table[@id="productDetails_detailBullets_sections1"]//tr/th/text()');
        if (productDetails.length > 0) {
            let brandNumber = 0;
            for (let i = 0; i < productDetails.length; i++) {
                brandNumber++;
                if (productDetails[i].includes('Brand')) {
                    break;
                }
            }
            const detailBrand = xpathQuery(
                `//table[@id="productDetails_detailBullets_sections1"]//tr[${brandNumber}]//td/text()`
            );
            if (detailBrand.length) {
                brand = detailBrand[0].trim();
            }
        }
    }

    // 最后尝试从店铺中获取品牌
    if (!brand.length) {
        const brandHref = xpathQuery('//a[@id="bylineInfo"]/@href');
        if (brandHref.length) {
            brand = brandHref[0].split('/')[2];
        } else {
            brand = '';
        }
    }

    return brand;
}

function getTitle() {
    return document.getElementById('productTitle').textContent.trim()
}

function getCategoryName() {
    // 创建一个XPath表达式求值器
    const evaluator = new XPathEvaluator();

    // 使用XPath查询获取所有匹配的节点
    const expression = '//li/span[@class="a-list-item"]/a[@class="a-link-normal a-color-tertiary"]/text()';
    const result = evaluator.evaluate(
        expression,
        document,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null
    );

    // 收集所有类别名称
    const categoryNames = [];
    for (let i = 0; i < result.snapshotLength; i++) {
        categoryNames.push(result.snapshotItem(i).textContent);
    }

    // 处理并连接类别名称
    let categoryName = "";
    if (categoryNames.length > 0) {
        categoryName = categoryNames
            .map(name => name.trim())
            .join(">>")
            .replace(/\n/g, "")
            .replace(/\s{2,}/g, "");
    }

    return categoryName;
}


function copyCheckData() {
    const brand = getBrand();
    const title = getTitle();
    const categoryName = getCategoryName();
    const asin = getAsin();
    GM.setClipboard(JSON.stringify({
        brand,
        title,
        categoryName,
        asin
    }));
    alert('复制成功')
}


/**
 * 复制所有asin
 */
function copyAsinAll() {
    const lis = document.querySelectorAll('li[data-asin]');
    const asins = Array.from(document.querySelectorAll('li[data-asin]'))
        .map(li => li.dataset.asin)
        .join('\n');
    if (asins) {
        GM.setClipboard(asins);
        alert('已复制所有asin!');
    } else {
        alert('复制失败');
    }
}

(function () {
    'use strict';
    window.debugger = function () {
    };
    document.oncontextmenu = null;
    document.onkeydown = null;
    window.addEventListener('load', function () {
        // 创建按钮
        const button = document.createElement('button');
        button.textContent = '复制ParentAsin';
        // 设置间隔 10px
        button.style.cssText = `
        margin: 20px;
        padding: 8px 16px;
        background: #4CAF50;
        color: white;
        border: none;
        border-radius: 4px;
        cursor: pointer;
        `;
        const titleButton = button.cloneNode(true);
        const cookieButton = button.cloneNode(true);
        const checkButton = button.cloneNode(true);
        titleButton.textContent = '复制标题+ParentAsin';
        cookieButton.textContent = '复制Cookie';
        checkButton.textContent = '复制校验参数';
        // 绑定点击事件
        button.addEventListener('click', function () {
            copyAsin();
        });

        titleButton.addEventListener('click', function () {
            copyTitle();
        });

        cookieButton.addEventListener('click', function () {
            copyCookie();
        });
        checkButton.addEventListener('click', function () {
            copyCheckData();
        });

        // 复制所有asin
        const asinButton = button.cloneNode(true);
        asinButton.addEventListener('click', function () {
            copyAsinAll();
        });
        asinButton.textContent = '复制全部Asin';

        const targetElement = document.getElementById('desktop-breadcrumbs_feature_div');
        const appElement = document.getElementById('app');
        // 注入页面
        if (targetElement) {
            targetElement.parentNode.insertBefore(button, targetElement);
            targetElement.parentNode.insertBefore(titleButton, targetElement);
            targetElement.parentNode.insertBefore(checkButton, targetElement);
            targetElement.parentNode.insertBefore(asinButton, targetElement);
        }
        if (appElement) {
            appElement.parentNode.insertBefore(cookieButton, appElement);
        }

    }, false);

})();