Quick Price Copy!

Click on a sale price or item name to copy it to the clipboard

// ==UserScript==
// @name         Quick Price Copy!
// @namespace    http://tampermonkey.net/
// @version      1.1
// @description  Click on a sale price or item name to copy it to the clipboard
// @author       Lucky11
// @match        https://fairview.deadfrontier.com/onlinezombiemmo/index.php?page=35
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const Price_Undercut_Value = 1;
    let Show_Feedback = true;

    function showCopyFeedback(price) {
        const feedback = document.createElement('div');
        feedback.textContent = `Copied: ${price}`;

        // Apply the new styles
        feedback.style.position = 'absolute';
        feedback.style.bottom = '250px'; // Adjusted to match your request
        feedback.style.left = '50%'; // Centering
        feedback.style.width = '90%'; // Responsive width
        feedback.style.maxWidth = '300px'; // Optional max width
        feedback.style.height = '19px';
        feedback.style.backgroundColor = 'rgba(0, 0, 0, 0.9)';
        feedback.style.textAlign = 'center';
        feedback.style.fontSize = '15px';
        feedback.style.border = '2px solid rgb(153, 0, 0)';
        feedback.style.padding = '2px 3px';
        feedback.style.color = 'white';
        feedback.style.zIndex = '1000';
        feedback.style.transform = 'translateX(-50%)'; // Center adjustment

        const invController = document.getElementById('invController'); // Replace with the actual ID or method to get your InvController element
        invController.appendChild(feedback);

        // Remove feedback after 1000 milliseconds
        setTimeout(() => {
            invController.removeChild(feedback);
        }, 1000);
    }

    function copyToClipboard(text) {
        const textarea = document.createElement('textarea');
        textarea.value = text;
        document.body.appendChild(textarea);
        textarea.select();
        const successful = document.execCommand('copy');
        document.body.removeChild(textarea);
    }

    function addClickListenerToPricesAndNames() {
        const salePrices = document.querySelectorAll('.salePrice');
        const itemNames = document.querySelectorAll('.itemName');

        salePrices.forEach(priceElement => {
            if (!priceElement.dataset.listenerAdded) {
                priceElement.style.cursor = 'pointer';
                priceElement.addEventListener('click', (event) => {
                    event.stopPropagation();
                    const priceText = priceElement.textContent.replace(/[^0-9]/g, '');
                    const priceValue = parseInt(priceText, 10) - Price_Undercut_Value;
                    copyToClipboard(priceValue.toString());
                    if (Show_Feedback) {
                        showCopyFeedback(priceValue.toString());
                    }
                    changeColor(priceElement);
                });
                priceElement.dataset.listenerAdded = 'true';
            }
        });

        itemNames.forEach(nameElement => {
            if (!nameElement.dataset.listenerAdded) {
                nameElement.style.cursor = 'pointer';
                nameElement.addEventListener('click', (event) => {
                    event.stopPropagation();
                    const itemName = nameElement.textContent;
                    copyToClipboard(itemName);
                    if (Show_Feedback) {
                        showCopyFeedback(itemName);
                    }
                    // Create a new style element
                    const style3 = document.createElement('style');
                    changeColor(nameElement);

                });
                nameElement.dataset.listenerAdded = 'true';
            }
        });
    }


    function changeColor(element) {
        const originalColor = element.style.color;
        element.style.color = '#28a745';

        setTimeout(() => {
            element.style.color = originalColor;
        }, 700);
    }

    const observer = new MutationObserver((mutations) => {
        let pricesFound = false;
        mutations.forEach(() => {
            const salePrices = document.querySelectorAll('.salePrice');
            const itemNames = document.querySelectorAll('.itemName');
            if (salePrices.length > 0 || itemNames.length > 0) {
                pricesFound = true;
                addClickListenerToPricesAndNames();
            }
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });
    // Create a new style element
    const style = document.createElement('style');

    // Add CSS rules for the classes and pseudo-elements
    style.textContent = `
    .itemName::before,
    .itemName::after {
        z-index: -1; /* Put pseudo-elements below item names */
    }
    .itemName {
        z-index: 10; /* Bring itemName in front */
    }
    .salePrice, .itemName {
        cursor: pointer !important; /* Ensure both classes have pointer cursor */
    }
`;

    // Append the style element to the head of the document
    document.head.appendChild(style);
})();