复制ParentAsin

复制亚马逊ParentAsin

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

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

function copyTitle() {
    const title = document.getElementById('productTitle').textContent.trim()
    const regex = /"parentAsin":"(.*?)"/; // 非贪婪匹配,避免匹配到多余的字符
    const result = document.body.textContent.match(regex);

    let asin = '';
    if (result && result[1]) {
        asin = result[1];
    }
    const content = asin + "|x|" + title
    GM.setClipboard(content);
    alert('已复制!')
}

(function () {
    'use strict';
    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);
        titleButton.textContent = '复制标题+ParentAsin';
        // 绑定点击事件
        button.addEventListener('click', function () {
            copyAsin();
        });

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


        const targetElement = document.getElementById('desktop-breadcrumbs_feature_div');
        // 注入页面
        targetElement.parentNode.insertBefore(button, targetElement);
        targetElement.parentNode.insertBefore(titleButton, targetElement);
    }, false);

})();