BagList copy

Copy relevant URLs to the clipboard with buttons

2024-02-02 या दिनांकाला. सर्वात नवीन आवृत्ती पाहा.

// ==UserScript==
// @name         BagList copy
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Copy relevant URLs to the clipboard with buttons
// @author       You
// @match        https://tngadmin.triplenext.net/Admin/CompareBag/BagList*
// @grant        GM_setClipboard
// ==/UserScript==

(function () {
    'use strict';

    const buttonStyles = {
        position: 'fixed',
        zIndex: '9999',
        backgroundColor: '#3daae9',
        width: '100px', // Adjust the width as needed
        height: '26px', // Adjust the height as needed
        border: '1px solid #ffffff',
        color: 'white',
        fontFamily: 'Telex, sans-serif',
    };

    const positionStyles = {
        'top-left': { top: '0px', left: '0px' },
        'top-right': { top: '25px', left: '100px' },
        'bottom-left': { top: '0px', left: '100px' },
        'bottom-right': { bottom: '10px', right: '10px' },
    };

    const alertStyles = {
        position: 'fixed',
        zIndex: '10000',
        top: '50px',
        left: '50%',
        transform: 'translateX(-50%)',
        backgroundColor: '#ffffff',
        padding: '10px',
        border: '1px solid #000000',
    };

    function copyToClipboard(text) {
        GM_setClipboard(text, 'text');
    }

    function trimURLParameters(url) {
        return url.split('?')[0];
    }

    function createNotification(message) {
        const alertDiv = document.createElement('div');
        alertDiv.innerHTML = message;
        Object.assign(alertDiv.style, alertStyles);
        document.body.appendChild(alertDiv);
        setTimeout(() => {
            alertDiv.remove();
        }, 1000);
    }

    function createButton(buttonText, urlSelector, position, trimParameters, alertMessage) {
        const button = document.createElement('button');
        button.innerHTML = buttonText;
        Object.assign(button.style, buttonStyles, positionStyles[position]);

        button.addEventListener('click', () => {
            const urls = document.querySelectorAll(urlSelector);
            const urlArray = Array.from(urls).map(url => trimParameters && urlSelector.includes('EditBag/') ? trimURLParameters(url.href) : url.href);

            copyToClipboard(urlArray.join('\n'));
            createNotification(alertMessage);
        });

        document.body.appendChild(button);
    }

    function createAllButton() {
        const allButton = document.createElement('button');
        allButton.innerHTML = 'ALL';
        Object.assign(allButton.style, buttonStyles, { top: '0px', left: '200px', width: '100px', height: '51px', } );

        allButton.addEventListener('click', () => {
            const categories = [
                { selector: 'body > div.container.notification > table:nth-child(5) > tbody td:nth-child(7)', trim: false },
                { selector: 'body > div.container.notification > table:nth-child(5) > tbody td:nth-child(3)', trim: false },
                { selector: 'body > div.container.notification > table:nth-child(5) > tbody td:nth-child(5)', trim: false },
                { selector: 'body > div.container.notification > table:nth-child(5) > tbody td:nth-child(6)', trim: false },
                { selector: 'a[href*="EditBag/"]', trim: true },
                { selector: 'a[href^="https://www."]', trim: false },
                { selector: 'a[href*="cdn.tangiblee.com"]', trim: false },
            ];

            const allUrlArray = categories.map(category => {
                const elements = document.querySelectorAll(category.selector);
                return Array.from(elements).map(element => category.trim ? trimURLParameters(element.href || element.textContent.trim()) : element.href || element.textContent.trim());
            });

            const transposedUrlArray = allUrlArray[0].map((_, colIndex) => allUrlArray.map(row => row[colIndex]));

            copyToClipboard(convertToCSV(transposedUrlArray));
            createNotification(messages.all);
        });

        window.addEventListener('keydown', (event) => {
            if (event.ctrlKey && event.code === 'KeyC') {
                allButton.click();
            }
        });

        document.body.appendChild(allButton);
    }

    function convertToCSV(data) {
        return data.map(row => row.join('\t')).join('\n');
    }

    const messages = {
        admin: 'Ссылки на Admin скопированы',
        url: 'Ссылки на URL скопированы',
        widget: 'Ссылки на WIDGET скопированы.',
        all: 'Скопировано',
        sku: 'Скопированы SKU',
        sku1: 'Скопированы SKU1',
    };

    createButton('Admin', 'a[href*="EditBag/"]', 'top-left',  true, messages.admin);
    createButton('Url', 'a[href^="https://www."]', 'top-right', false, messages.url);
    createButton('Widget', 'a[href*="cdn.tangiblee.com"]', 'bottom-left', false, messages.widget);

    createAllButton();

    createAllButton();

function createSkuButton() {
    const skuButton = document.createElement('button');
    skuButton.innerHTML = 'SKU';
    Object.assign(skuButton.style, buttonStyles, { top: '25px', left: '0px' });

    skuButton.addEventListener('click', (event) => {
        const skuElements = document.querySelectorAll('body > div.container.notification > table:nth-child(5) > tbody td:nth-child(3)');
        const skuArray = Array.from(skuElements).map(skuElement => skuElement.textContent.trim());

        const separator = event.ctrlKey ? ',' : ', '; // Use ',' without space if Ctrl is pressed

        copyToClipboard(skuArray.join(separator));
        createNotification(messages.sku);
    });

    document.body.appendChild(skuButton);
}

createSkuButton(); // Добавил эту строку для вызова функции создания кнопки

})();