LeetCode Problem Copyer

复制Leetcode的题目的文本或HTML

  1. // ==UserScript==
  2. // @name LeetCode Problem Copyer
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description 复制Leetcode的题目的文本或HTML
  6. // @author You
  7. // @match https://leetcode.cn/problems/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.cn
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function () {
  14. ("use strict");
  15. var x = 100;
  16. var y = 20;
  17. var id = "dsafaafdsfss2112";
  18.  
  19. function info(message) {
  20. let p = document.createElement("p");
  21. p.style.padding = "10px 20px";
  22. p.style.fontSize = "12px";
  23. p.style.display = "block";
  24. p.style.position = "absolute";
  25. p.style.left = `${x}px`;
  26. p.style.top = `${y}px`;
  27. p.style.border = "1px solid black";
  28. p.style.borderRadius = "3px";
  29. p.style.backgroundColor = "#FFF";
  30. p.innerText = message;
  31. document.body.appendChild(p);
  32. setTimeout(function () {
  33. document.body.removeChild(p);
  34. }, 1000);
  35. }
  36.  
  37. function fnCopy(copyText) {
  38. navigator.clipboard
  39. .writeText(copyText)
  40. .then(() => {
  41. info("Copy Ok!");
  42. })
  43. .catch(() => {
  44. const input = document.createElement("input");
  45. document.body.appendChild(input);
  46. input.setAttribute("value", copyText);
  47. input.select();
  48. if (document.execCommand("copy")) {
  49. document.execCommand("copy");
  50. }
  51. document.body.removeChild(input);
  52. info("Copy Ok!");
  53. });
  54. }
  55.  
  56. var createBtn = function (title, cb) {
  57. let mbtn = document.createElement("button");
  58. //mbtn.style.width = "40px";
  59. mbtn.classList.add("css-nabodd-Button");
  60. mbtn.style.height = "20px";
  61. mbtn.innerText = title;
  62. mbtn.style.padding='3px 5px';
  63. mbtn.style.border = "1px solid rgb(89, 89, 89)";
  64. mbtn.style.borderRadius = "3px";
  65. mbtn.style.zIndex = 999;
  66. mbtn.style.marginRight = "5px";
  67. mbtn.addEventListener("click", cb);
  68. return mbtn;
  69. };
  70. var createDiv = function () {
  71. let div = document.createElement("div");
  72. const htmlBtn = createBtn("CopyHtml", function (e) {
  73. x = e.clientX + 10;
  74. y = e.clientY + 10;
  75. fnCopy(
  76. document.querySelector('div[data-key="description-content"]').innerHTML
  77. );
  78. });
  79. const textBtn = createBtn("CopyText", function (e) {
  80. x = e.clientX + 10;
  81. y = e.clientY + 10;
  82. fnCopy(
  83. document.querySelector('div[data-key="description-content"]').innerText
  84. );
  85. });
  86. div.style.display='flex';
  87. div.appendChild(htmlBtn);
  88. div.appendChild(textBtn);
  89. div.id = id;
  90. return div;
  91. };
  92.  
  93. function task() {
  94. if (document.getElementById(id)) {
  95. return true;
  96. }
  97. var _parent = document.querySelector('div[data-key="description-content"]').firstChild.firstChild.lastChild;
  98. if (_parent) {
  99. let div = createDiv();
  100. _parent.appendChild(div);
  101. return true;
  102. }
  103. return false;
  104. }
  105.  
  106. var onceTask = setInterval(function () {
  107. try {
  108. task();
  109. } catch (e) {
  110. console.log("try..");
  111. }
  112. }, 1000);
  113.  
  114. })();