LeetCode 問題描述內容擷取器

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

当前为 2025-02-26 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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);
            });
    });
})();