// ==UserScript== // @name Lecd // @namespace https://www.ruxf.com/ // @version 0.2.3 // @description Copy leetcode problem // @author You // @match https://leetcode-cn.com/problems/*/ // @match https://leetcode.cn/problems/*/ // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode-cn.com // @grant none // @license MIT // @downloadURL https://update.greasyfork.icu/scripts/442306/Lecd.user.js // @updateURL https://update.greasyfork.icu/scripts/442306/Lecd.meta.js // ==/UserScript== (function () { 'use strict'; // Your code here... function onCopyProblem() { 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';
      }
    
      function onCopyTitle() {
        const title = document.querySelector(
          `h4[data-cypress="QuestionTitle"]`
        ).innerText;
        if (navigator.clipboard) {
          // clipboard api 复制
          navigator.clipboard.writeText(title);
        } 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 = title;
          // 选中
          textarea.select();
          // 复制
          document.execCommand('copy', true);
          // 移除输入框
          document.body.removeChild(textarea);
        }
      }
      const div = document.createElement('div');
      div.style.position = 'fixed';
      div.style.bottom= 0;
      div.style.left = 0;
      // Copy problem button
      const problemer = document.createElement('button');
      problemer.innerText = 'Copy Problem';
      problemer.addEventListener('click', onCopyProblem);
      div.appendChild(problemer)
      // Copy title button
      const titler = document.createElement('button');
      titler.innerText = 'Copy Title';
      titler.addEventListener('click', onCopyTitle);
      div.appendChild(titler)
      document.body.appendChild(div);
    
    })();