Greasy Fork

Extract ASIN

Add a button to the page; clicking it extracts all ASIN values, combines them with Amazon product URL, displays for easy copying, and auto-closes the display on copy.

当前为 2024-03-04 提交的版本,查看 最新版本

// ==UserScript==
// @name         Extract ASIN
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Add a button to the page; clicking it extracts all ASIN values, combines them with Amazon product URL, displays for easy copying, and auto-closes the display on copy.
// @author       You
// @match        https://www.amazon.com/*
// @grant        none
// @license MIT

// ==/UserScript==

(function() {
    'use strict';

    // Function to create the display element for Amazon product links and ASIN count
    function createDisplayElement(asinLinks) {
        const container = document.createElement('div');
        container.style.position = 'fixed';
        container.style.top = '10px';
        container.style.right = '10px';
        container.style.padding = '10px';
        container.style.backgroundColor = 'white';
        container.style.border = '1px solid black';
        container.style.zIndex = '10000';
        container.style.overflow = 'auto';
        container.style.maxHeight = '90vh';

        // Display the count of ASINs found
        const countDisplay = document.createElement('p');
        countDisplay.textContent = `ASINs found: ${asinLinks.length}`;
        container.appendChild(countDisplay);

        const textArea = document.createElement('textarea');
        textArea.value = asinLinks.join('\n');
        textArea.rows = '20';
        textArea.cols = '50';
        textArea.style.marginTop = '10px'; // Add some space between the count and the text area

        const copyButton = document.createElement('button');
        copyButton.textContent = 'Copy to Clipboard';
        copyButton.onclick = function() {
            textArea.select();
            document.execCommand('copy');
            container.remove(); // This line closes the container after copying
        };

        container.appendChild(textArea);
        container.appendChild(copyButton);
        document.body.appendChild(container);
    }

    // Function to extract ASIN values and generate Amazon product links
    function generateAmazonLinks() {
        const targetElements = document.querySelectorAll('[data-asin]');
        const asinLinks = [];

        targetElements.forEach(element => {
            const asin = element.getAttribute('data-asin');
            if (asin) {
                const amazonLink = `https://www.amazon.com/gp/product/${asin}`;
                asinLinks.push(amazonLink);
            }
        });

        if (asinLinks.length > 0) {
            createDisplayElement(asinLinks);
        }
    }

    // Function to create the button that will trigger the generation of Amazon links
    function createExtractionButton() {
        const button = document.createElement('button');
        button.textContent = 'Generate Amazon Links';
        button.style.position = 'fixed';
        button.style.bottom = '10px';
        button.style.right = '10px';
        button.style.padding = '10px';
        button.style.backgroundColor = '#4CAF50';
        button.style.color = 'white';
        button.style.border = 'none';
        button.style.cursor = 'pointer';

        button.onclick = generateAmazonLinks;

        document.body.appendChild(button);
    }

    // Add the button to the page
    createExtractionButton();
})();