Taobao Item Display Hover Text

Display text of elements corresponding to hover effects directly on the page, and copy it to the clipboard on click.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Taobao Item Display Hover Text
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Display text of elements corresponding to hover effects directly on the page, and copy it to the clipboard on click.
// @author       max5555
// @license      MIT
// @match        https://item.taobao.com/*
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // Adjusted styling for our display box to appear on the left
    GM_addStyle(`
        #hoverTextDisplay {
            position: fixed;
            bottom: 10px;
            left: 10px;
            max-width: 300px;
            max-height: 300px;
            overflow-y: auto;
            background-color: rgba(0,0,0,0.7);
            color: #FFF;
            padding: 10px;
            border-radius: 5px;
            z-index: 999999;
            font-size: 12px;
        }
    `);

    // Create the display box and append to body
    const displayBox = document.createElement('div');
    displayBox.id = 'hoverTextDisplay';
    document.body.appendChild(displayBox);

    let previousElements = new Set();

    const captureHoverElements = () => {
        const allElements = new Set(document.querySelectorAll('*'));
        const newOrVisibleElements = Array.from(allElements).filter(el => {
            const isPreviouslyLogged = previousElements.has(el);
            const isVisible = window.getComputedStyle(el).display !== 'none';

            //return !isPreviouslyLogged && isVisible;
            return isVisible;
        });

        // Display only the text content of the hovered element
        newOrVisibleElements.forEach(el => {
            displayBox.textContent = el.textContent.trim();
            //displayBox.innerHTML = el.outerHTML;
            //displayBox.innerHTML = el.outerHTML.textContent.trim();
            //copyToClipboard(el.outerHTML.textContent.trim());
        });

        // Update the set of known elements
        previousElements = allElements;
    };

    // Function to copy text to the clipboard
    const copyToClipboard = (text) => {
        navigator.clipboard.writeText(text).then(() => {
            console.log('Text copied to clipboard');
        }).catch(err => {
            console.error('Could not copy text: ', err);
        });
    };

    document.body.addEventListener('mouseover', () => {
        setTimeout(captureHoverElements, 500);
    });

    // Added a click event listener to copy the text on click
    displayBox.addEventListener('click', () => {
        copyToClipboard(displayBox.textContent);
    });

})();