leetcode user case

create user case unit test in leetcode.com

  1. // ==UserScript==
  2.  
  3. // @require http://libs.baidu.com/jquery/1.8.3/jquery.min.js
  4. // @name leetcode user case
  5. // @namespace http://tangmocd.cn/
  6. // @version 0.1
  7. // @description create user case unit test in leetcode.com
  8. // @author kenybens@gmail.com
  9. // @match *://leetcode-cn.com/problems/*
  10. // @grant none
  11. // ==/UserScript==
  12.  
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. function addlight() {
  18. var node = document.createElement("div");
  19. node.id = "light"
  20. node.style = "display: none;" +
  21. "position: absolute;" +
  22. "top: 25%;" +
  23. "left: 25%;" +
  24. "width: 55%;" +
  25. "height: 55%;" +
  26. "padding: 20px;" +
  27. "border: 10px solid orange;" +
  28. "background-color: white;" +
  29. "z-index: 1002;" +
  30. "overflow: auto;"
  31. node.innerHTML = "code Example." +
  32. "<a href=\"javascript:void(0)\" " +
  33. " onclick=\"document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'\">Close Window</a>" +
  34. "<textarea name=\"lines\" rows=\"30\" cols=\"100\" id=\"contentid\"></textarea>";
  35. return node;
  36. }
  37.  
  38. function addfade() {
  39. var node = document.createElement("div");
  40. node.id = "fade";
  41. node.style = "display: none;" +
  42. "position: absolute;" +
  43. "top: 0%;" +
  44. "left: 0%;" +
  45. "width: 100%;" +
  46. "height: 100%;" +
  47. "background-color: black;" +
  48. "z-index: 1001;" +
  49. "-moz-opacity: 0.8;" +
  50. "opacity: .20;" +
  51. "filter: alpha(opacity=88);"
  52. return node;
  53. }
  54.  
  55.  
  56.  
  57.  
  58. function getfunctionname(content) {
  59. //param: content include function name
  60. // return function name in code
  61. // eg. content= def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
  62. // return: containsPattern
  63. content = content.split("(")[0].replace("def", "").replace(" ", "").replace(/(^\s*)|(\s*$)/g, "");
  64. return content;
  65.  
  66. }
  67.  
  68. function addbutton() {
  69. // add a link
  70. var node = document.createElement("div");
  71. node.style = " display: flex;\n" +
  72. " flex-wrap: nowrap;\n" +
  73. " -webkit-box-align: center;\n" +
  74. " align-items: center;"
  75. node.innerHTML =
  76. "<a href=\"javascript:void(0)\" " +
  77. " onclick=\"document.getElementById('light').style.display='block';document.getElementById('fade').style.display='block'\">Use Case Generate</a>";
  78.  
  79. return node
  80.  
  81. }
  82. function createcontent(){
  83.  
  84. //create content
  85. console.log("find language");
  86. var stra = $("#lang-select").find('span')[0].innerText;
  87.  
  88. if (stra == 'Python3') {
  89. console.log(stra);
  90. var childs = $(".view-lines").children()
  91. if (childs.length == 2) {
  92. console.log('child of code len is 2')
  93.  
  94. var contentlist = []
  95. contentlist.push("from typing import List")
  96. contentlist.push(childs[0].innerText.replace(/\xA0/g, " "));
  97. contentlist.push(childs[1].innerText.replace(/\xA0/g, " "));
  98. contentlist.push(" pass")
  99. contentlist.push("if __name__ == '__main__':");
  100. contentlist.push(" obj=Solution()")
  101.  
  102. var funcname = getfunctionname($('div.view-lines > div:nth-child(2) > span')[0].innerText);
  103.  
  104. var prelist = $(".notranslate pre")
  105.  
  106. console.log(funcname);
  107.  
  108. for (var i = 0; i < prelist.length; i++) {
  109. var usercase = prelist[i].innerText;
  110. usercase = usercase.replaceAll(":", ":").split("\n")
  111.  
  112.  
  113. var content2 = " print(obj." + funcname;
  114. for (var j = 0; j < usercase.length; j++) {
  115. console.log(usercase[j])
  116.  
  117. var intputlist = usercase[j].split(":")
  118. if (intputlist.length == 2) {
  119.  
  120. if (j == 0) {
  121.  
  122. content2 += "(" + intputlist[1] + ')==';
  123. } else if (j == 1) {
  124.  
  125. //change true-->True and false -->False
  126. var result = intputlist[1].replaceAll("false", "False").replaceAll("true", "True");
  127. content2 += result + ")";
  128. contentlist.push(content2);
  129. //create next node content
  130. content2 = " print(" + funcname;
  131.  
  132. }
  133.  
  134. }
  135. }
  136.  
  137.  
  138. document.getElementById("contentid").value = contentlist.join('\n');
  139.  
  140. }
  141. }
  142.  
  143.  
  144. }
  145.  
  146. }
  147.  
  148.  
  149.  
  150.  
  151. window.onload = function () {
  152.  
  153.  
  154. var appex = document.getElementById("app")
  155. appex.appendChild(addfade());
  156. appex.appendChild(addlight());
  157.  
  158. var divmenu=document.getElementsByClassName("second-section-container__2cAh")[0]
  159. var downloadapp=document.getElementsByClassName("css-1y830sm-MockInterviewContainer e3jm4na1")[0]
  160. divmenu.insertBefore(addbutton(),downloadapp)
  161.  
  162. createcontent()
  163.  
  164.  
  165.  
  166.  
  167. }
  168. })();