Copy LeetCode Title

Adds a native-looking button to copy the LeetCode problem title

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Copy LeetCode Title
// @namespace    http://tampermonkey.net/
// @version      2026.03.14
// @description  Adds a native-looking button to copy the LeetCode problem title
// @author       Holden Hu
// @match        https://leetcode.cn/problems/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=leetcode.cn
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    function copyToClipboard(text) {
        navigator.clipboard.writeText(text).then(function() {
            console.log('标题已成功复制: ' + text);
        }, function(err) {
            console.error('无法复制: ', err);
        });
    }

    function addButton() {
        if (document.getElementById('custom-copy-btn')) return;

        const titleElement = document.querySelector('.text-title-large');
        if (!titleElement) return;

        const copyButton = document.createElement('button');
        copyButton.id = 'custom-copy-btn';
        copyButton.textContent = '复制标题';

        // 这种样式写法能保证在 LeetCode 的不同主题下都有很好的融合度
        Object.assign(copyButton.style, {
            marginLeft: '12px',
            padding: '2px 10px',
            fontSize: '12px',
            borderRadius: '12px',
            border: 'none',
            cursor: 'pointer',
            backgroundColor: 'rgba(128, 128, 128, 0.15)', // 半透明灰色,适配深浅色背景
            color: 'inherit',
            transition: 'background-color 0.2s',
            verticalAlign: 'middle'
        });

        // 悬停效果
        copyButton.onmouseover = () => copyButton.style.backgroundColor = 'rgba(128, 128, 128, 0.25)';
        copyButton.onmouseout = () => copyButton.style.backgroundColor = 'rgba(128, 128, 128, 0.15)';

        copyButton.addEventListener('click', function() {
            // 点击时动态获取最新的标题
            const currentTitle = document.querySelector('.text-title-large')?.textContent.trim();
            if (currentTitle) {
                copyToClipboard(currentTitle);
                const originalText = copyButton.textContent;
                copyButton.textContent = '已复制!';
                setTimeout(() => { copyButton.textContent = originalText; }, 1500);
            }
        });

        titleElement.parentElement.appendChild(copyButton);
    }

    // 观察者,解决 SPA 页面不刷新的问题
    const observer = new MutationObserver(() => addButton());
    observer.observe(document.body, { childList: true, subtree: true });

    addButton();
})();