Greasy Fork is available in English.

Lecd

Copy leetcode problem

您查看的为 2022-05-10 提交的版本。查看 最新版本

// ==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
// ==/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(/<li>/g, '- ')
      // 替换代码块
      .replace(/<pre>/g, '```bash\n')
      .replace(/<\/pre>/g, '\n```')
      .replace(/&nbsp;/g, ' ')
      .replace(/<\/?.+?>/g, '')
      .replace(/&gt;/g, '>')
      .replace(/&lt;/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);

})();