Copy LeetCode Title

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

Advertisement:

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

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