Copy LeetCode Title

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

Bu betiği kurabilmeniz için Tampermonkey, Greasemonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği yüklemek için Tampermonkey gibi bir uzantı yüklemeniz gerekir.

Bu betiği kurabilmeniz için Tampermonkey ya da Violentmonkey gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği kurabilmeniz için Tampermonkey ya da Userscripts gibi bir kullanıcı betiği eklentisini kurmanız gerekmektedir.

Bu betiği indirebilmeniz için ayrıca Tampermonkey gibi bir eklenti kurmanız gerekmektedir.

Bu komut dosyasını yüklemek için bir kullanıcı komut dosyası yöneticisi uzantısı yüklemeniz gerekecek.

(Zaten bir kullanıcı komut dosyası yöneticim var, kurmama izin verin!)

Advertisement:

Bu stili yüklemek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için Stylus gibi bir uzantı kurmanız gerekir.

Bu stili yükleyebilmek için Stylus gibi bir uzantı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

Bu stili yüklemek için bir kullanıcı stili yöneticisi uzantısı kurmanız gerekir.

Bu stili yükleyebilmek için bir kullanıcı stili yöneticisi uzantısı yüklemeniz gerekir.

(Zateb bir user-style yöneticim var, yükleyeyim!)

Advertisement:

// ==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();
})();