Copy LeetCode Title

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

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

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

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

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