Greasy Fork 支持简体中文。

獲取Shopee標題

Get all product titles from Shopee.tw search page and copy them to clipboard

// ==UserScript==
// @name         獲取Shopee標題
// @namespace    http://tampermonkey.net/
// @version      0.1
// @license      Rayu
// @description  Get all product titles from Shopee.tw search page and copy them to clipboard
// @author       Rayu
// @match        https://shopee.tw/search?*
// @grant        GM_setClipboard
// ==/UserScript==

(function() {
    'use strict';

    // Function to extract product titles
    function getProductTitles() {
        let titles = [];
        // Get the parent container of all product titles
        const container = document.querySelector('.u37U8O section ul');
        // Check if the container exists
        if(container) {
            // Loop through each child element (product title)
            container.querySelectorAll('li div a.contents div.line-clamp-2').forEach(title => {
                titles.push(title.textContent.trim());
            });
        }
        return titles;
    }

    // Function to copy text to clipboard
    function copyToClipboard(text) {
        GM_setClipboard(text.join('\n'), 'text');
        console.log('Product titles copied to clipboard:', text);
    }

    // Function to display product titles
    function displayProductTitles() {
        const titles = getProductTitles();
        if (titles.length > 0) {
            console.log('Product Titles:');
            console.log(titles);
            copyToClipboard(titles); // Copy titles to clipboard
        } else {
            console.log('No product titles found.');
        }
    }

    // Function to create and style the button
    function createButton() {
        const button = document.createElement('button');
        button.textContent = '获取本页标题';
        button.style.backgroundColor = 'darkgreen';
        button.style.color = 'white';
        button.style.padding = '10px';
        button.style.border = 'none';
        button.style.borderRadius = '5px';
        button.style.cursor = 'pointer';
        button.style.position = 'fixed';
        button.style.top = '20px'; // Adjust top position as needed
        button.style.right = '20px'; // Adjust right position as needed
        button.addEventListener('click', () => {
            displayProductTitles();
        });
        return button;
    }

    // Wait for page to load completely
    window.onload = function() {
        // Create and append the button to the body
        const button = createButton();
        document.body.appendChild(button);
    };
})();