LeetCode 問題描述內容擷取器

在 LeetCode 問題頁面上,按下按鈕後直接複製描述內容到剪貼簿。

目前為 2025-02-26 提交的版本,檢視 最新版本

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         LeetCode 問題描述內容擷取器
// @namespace    https://abc0922001.github.io/leetcode-userscripts
// @version      1.2
// @description  在 LeetCode 問題頁面上,按下按鈕後直接複製描述內容到剪貼簿。
// @author       abc0922001
// @match        https://leetcode.com/problems/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
    'use strict';

    // 等待指定選取器元素出現
    function waitForElement(selector, timeout = 5000) {
        return new Promise((resolve, reject) => {
            const el = document.querySelector(selector);
            if (el) {
                return resolve(el);
            }
            const observer = new MutationObserver((mutations, obs) => {
                const el = document.querySelector(selector);
                if (el) {
                    obs.disconnect();
                    resolve(el);
                }
            });
            observer.observe(document.body, { childList: true, subtree: true });
            setTimeout(() => {
                observer.disconnect();
                reject(new Error('Timeout'));
            }, timeout);
        });
    }

    // 建立按鈕
    const btn = document.createElement('button');
    btn.textContent = '複製描述';
    btn.style.position = 'fixed';
    btn.style.top = '10px';
    btn.style.right = '10px';
    btn.style.zIndex = 1000;
    document.body.appendChild(btn);

    btn.addEventListener('click', () => {
        waitForElement('div.elfjS[data-track-load="description_content"]', 5000)
            .then(targetDiv => {
                const htmlContent = targetDiv.innerHTML;
                navigator.clipboard.writeText(htmlContent)
                    .then(() => {
                        alert('描述內容已複製到剪貼簿!');
                    })
                    .catch(err => {
                        alert('複製失敗:' + err);
                    });
            })
            .catch(err => {
                alert('找不到目標內容');
                console.error(err);
            });
    });
})();