Copy LeetCode Title

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

Advertisement:

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

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