// ==UserScript== // @name Lecd // @namespace https://www.ruxf.com/ // @version 0.1 // @description Copy leetcode problem // @author You // @match https://leetcode-cn.com/problems/*/ // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode-cn.com // @grant none // @license MIT // @downloadURL none // ==/UserScript== (function() { 'use strict'; // Your code here... console.log('Tampermonkey loaded'); function onConsoleProblem() { const title = document.querySelector(`h4[data-cypress="QuestionTitle"]`).innerText; const hard = document.querySelector('span[data-degree]').innerText; const content = document.querySelector('div.notranslate:not(#question-detail-main-tabs)').innerHTML .replace(/(\<\/?code\>)/g, '`') // 替换列表 .replace(/\t/g, '') .replace(/
  • /g, '- ') // 替换代码块 .replace(/
    /g, '```bash\n')
            .replace(/<\/pre>/g, '\n```')
            .replace(/ /g, ' ')
            .replace(/<\/?.+?>/g, '')
            .replace(/>/g, '>')
            .replace(/</g, '<')
            const problem = `# ${title}\n\n难度:\`${hard}\`\n\n${content}`;
            if (navigator.clipboard) {
                // clipboard api 复制
                navigator.clipboard.writeText(problem);
            } else {
                var textarea = document.createElement('textarea');
                document.body.appendChild(textarea);
                // 隐藏此输入框
                textarea.style.position = 'fixed';
                textarea.style.clip = 'rect(0 0 0 0)';
                textarea.style.top = '10px';
                // 赋值
                textarea.value = problem;
                // 选中
                textarea.select();
                // 复制
                document.execCommand('copy', true);
                // 移除输入框
                document.body.removeChild(textarea);
            }
            btn.innerText = 'Copied';
        }
        const btn = document.createElement('button');
        btn.innerText = 'Copy';
        btn.style.position = 'fixed';
        btn.style.top = 0;
        btn.style.left = 0;
        btn.addEventListener('click', onConsoleProblem);
        document.body.appendChild(btn);
    })();