Greasy Fork 还支持 简体中文。

Enable Text Selection and Copy Data on Grupo Andres

Enable text selection and add a button to copy to clipboard the item data on https://online.grupoandres.com/

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Enable Text Selection and Copy Data on Grupo Andres
// @namespace    http://SrGeneroso/
// @version      1.0
// @license      MIT
// @description  Enable text selection and add a button to copy to clipboard the item data on https://online.grupoandres.com/
// @author       SrGeneroso
// @match        https://online.grupoandres.com/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    function modifyPage() {
        // Add custom styles to enable text selection
        GM_addStyle('.search-by-two-measures { user-select: text !important; -webkit-user-select: text !important; }');

        // Function to extract and copy data
        function copyData(tableResult, button) {
            var clipboardData = '';

            var brand = tableResult.querySelector('.image-brand')?.querySelector('img')?.getAttribute('title');
            var model = tableResult.querySelector('.description')?.querySelector('p')?.textContent.trim();
            var properties = tableResult.querySelector('.properties')?.textContent.trim().replace(/\s*\|$/, '');
            var price = tableResult.querySelector('.base-price')?.innerText;
            var stock = tableResult.querySelector('.stock-item')?.textContent.trim();

            console.log(`${brand} ${model} | ${properties} | ${stock} | ${price}`.replace(/\s+/g, ' '))
            if (brand && model && price && stock) {
                clipboardData = `${brand} ${model} | ${properties} | ${stock} | ${price}`.replace(/\s+/g, ' ')
            }

            // Copy data to clipboard
            navigator.clipboard.writeText(clipboardData).then(function() {
                // Change button text after successful copy
                button.textContent = buttonMsgExecuted;
                setTimeout(function() {
                    // Reset button text after a brief delay
                    button.textContent = buttonMsgReady;
                }, 2000);
            }).catch(function(err) {
                console.error('Error copying to clipboard:', err);
                // Change button text after an error
                button.textContent = buttonMsgError;
                setTimeout(function() {
                    // Reset button text after a brief delay
                    button.textContent = buttonMsgReady;
                }, 2000);
            });
        }

        // Items
        var tableResults = document.querySelectorAll('.results-body .table-result');
        var buttonMsgReady = '📎 Copy to clipboard'
        var buttonMsgExecuted = '📋 Data copied to clipboard'
        var buttonMsgError = '💥 Copy failed'

        tableResults.forEach(function(tableResult) {
            var existingButton = tableResult.querySelector('.copy-button');
            if (!existingButton) {
                // Create and append the button element
                var button = document.createElement('button');
                button.className = 'copy-button';
                button.textContent = buttonMsgReady;

                // Add click event listener to the button
                button.addEventListener('click', function() {
                    copyData(tableResult, button);
                });

                tableResult.appendChild(button);
            }
        });
    }

    // Wait until the document is fully loaded
    setTimeout(function() {
        // Run the script
        modifyPage();
        // Monitor mutations on .results-body
        var observer = new MutationObserver(function(mutations) {
            mutations.forEach(function(mutation) {
                modifyPage();

            });
        });
        var results = document.querySelector('.results-body');
        if (results) {
            // Start observing .results-body for childList changes
            observer.observe(results, { childList: true });
        } else {
            console.error('.results-body element not found.');
        }
    }, 1000);
})();