Quick Price Copy!

Click on a sale price to copy it to the clipboard

// ==UserScript==
// @name         Quick Price Copy!
// @namespace    http://tampermonkey.net/
// @version      1
// @description  Click on a sale price 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.left = '50%';
        feedback.style.top = '50%';
        feedback.style.width = '270px';
        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 #990000';
        feedback.style.padding = '2px 3px';
        feedback.style.color = 'white'; // Optional: Set text color to white for better visibility
        feedback.style.zIndex = '1000'; // Ensure it appears above other elements

        document.body.appendChild(feedback);
        setTimeout(() => {
            document.body.removeChild(feedback);
        }, 1000);
    }

    // Function to copy text to clipboard
    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);
        //console.log(`Copying text: ${text} - Success: ${successful}`);
    }

    // Function to add click event listener to salePrice elements
    function addClickListenerToPrices() {
        const salePrices = document.querySelectorAll('.salePrice');
        salePrices.forEach(priceElement => {
            if (!priceElement.dataset.listenerAdded) { // Check if listener is already added
                priceElement.style.cursor = 'pointer'; // Change cursor to pointer
                priceElement.addEventListener('click', (event) => {
                    event.stopPropagation(); // Prevent event bubbling
                    const priceText = priceElement.textContent.replace(/[^0-9]/g, ''); // Remove non-numeric characters
                    //console.log(`Price clicked: ${priceText}`); // Log the clicked price
                    //copyToClipboard(priceText);
                    //alert(`Copied: ${priceText}`); // Optional: Show an alert
                    const priceValue = parseInt(priceText, 10) - Price_Undercut_Value; // Convert to number and subtract 1
                    //console.log(`Price clicked: ${priceValue}`); // Log the adjusted price
                    copyToClipboard(priceValue.toString()); // Copy the adjusted price to clipboard
                    if (Show_Feedback) {
                        showCopyFeedback(priceValue.toString());
                    }

                    // Change the color of the price element
                    const originalColor = priceElement.style.color; // Store the original color
                    priceElement.style.color = '#28a745'; // Change to a new color (e.g., red)

                    // Revert the color back after 2 seconds
                    setTimeout(() => {
                        priceElement.style.color = originalColor; // Revert to original color
                    }, 700); // Change back after 2000 milliseconds (2 seconds)

                });
                priceElement.dataset.listenerAdded = 'true'; // Mark listener as added
            }
        });
    }

    // Set up a MutationObserver to watch for changes in the DOM
    const observer = new MutationObserver((mutations) => {
        let pricesFound = false;
        mutations.forEach(() => {
            const salePrices = document.querySelectorAll('.salePrice');
            if (salePrices.length > 0) {
                pricesFound = true;
                addClickListenerToPrices(); // Add listeners for new elements
            }
        });
        //if (pricesFound) {
        //    console.log('Sale prices detected and listeners added.');
        //}
    });

    // Start observing the entire document for configured mutations
    observer.observe(document.body, { childList: true, subtree: true });
    //console.log('Observer is set up to watch for changes in the DOM.');

    // Add CSS to ensure cursor changes to pointer
    const style = document.createElement('style');
    style.textContent = `
        .salePrice {
            cursor: pointer !important; /* Ensure cursor is pointer */
        }
    `;
    document.head.appendChild(style);
})();