Greasy Fork is available in English.

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. function addlight(){
  17. var node=document.createElement("div");
  18. node.id="light"
  19. node.style="display: none;"+
  20. "position: absolute;"+
  21. "top: 25%;"+
  22. "left: 25%;"+
  23. "width: 55%;"+
  24. "height: 55%;"+
  25. "padding: 20px;"+
  26. "border: 10px solid orange;"+
  27. "background-color: white;"+
  28. "z-index: 1002;"+
  29. "overflow: auto;"
  30. node.innerHTML="code Example."+
  31. "<a href=\"javascript:void(0)\" "+
  32. " onclick=\"document.getElementById('light').style.display='none';document.getElementById('fade').style.display='none'\">Close Window</a>"+
  33. "<textarea name=\"lines\" rows=\"30\" cols=\"100\" id=\"contentid\"></textarea>";
  34. return node;
  35. }
  36. function addfade(){
  37. var node=document.createElement("div");
  38. node.id="fade";
  39. node.style="display: none;"+
  40. "position: absolute;"+
  41. "top: 0%;"+
  42. "left: 0%;"+
  43. "width: 100%;"+
  44. "height: 100%;"+
  45. "background-color: black;"+
  46. "z-index: 1001;"+
  47. "-moz-opacity: 0.8;"+
  48. "opacity: .20;"+
  49. "filter: alpha(opacity=88);"
  50. return node;
  51. }
  52.  
  53.  
  54. function getfunctionname(content){
  55. //param: content include function name
  56. // return function name in code
  57. // eg. content= def containsPattern(self, arr: List[int], m: int, k: int) -> bool:
  58. // return: containsPattern
  59. content=content.split("(")[0].replace("def","").replace(" ","").replace(/(^\s*)|(\s*$)/g, "");
  60. return content;
  61.  
  62. }
  63. window.onload = function(){
  64.  
  65. console.log("find language");
  66. var stra=$("#lang-select").find('span')[0].innerText;
  67.  
  68. if (stra=='Python3'){
  69. console.log(stra);
  70. var childs=$(".view-lines").children()
  71. if (childs.length==2){
  72. console.log('child of code len is 2')
  73. var diivlines=$("div.view-lines")[0];
  74.  
  75. var linesnum=$("div.margin-view-overlays")[0];
  76.  
  77. var contentlist=[]
  78. contentlist.push("from typing import List")
  79. contentlist.push(childs[0].innerText.replace(/\xA0/g," "));
  80. contentlist.push(childs[1].innerText.replace(/\xA0/g," "));
  81. contentlist.push(" pass")
  82. contentlist.push("if __name__ == '__main__':");
  83. contentlist.push(" obj=Solution()")
  84.  
  85. var funcname=getfunctionname($('div.view-lines > div:nth-child(2) > span')[0].innerText);
  86.  
  87. var prelist=$(".notranslate pre")
  88.  
  89. console.log(funcname);
  90.  
  91. for(var i=0;i<prelist.length;i++){
  92. var usercase=prelist[i].innerText;
  93. usercase=usercase.replaceAll(":",":").split("\n")
  94.  
  95.  
  96. var content2=" print(obj."+funcname;
  97. for(var j=0;j<usercase.length;j++){
  98. console.log(usercase[j])
  99.  
  100. var intputlist=usercase[j].split(":")
  101. if (intputlist.length==2){
  102.  
  103. if (j==0){
  104.  
  105. content2+="("+intputlist[1]+')==';
  106. }else if(j==1){
  107.  
  108. //change true-->True and false -->False
  109. var result=intputlist[1].replaceAll("false","False").replaceAll("true","True");
  110. content2+=result+")";
  111. contentlist.push(content2);
  112. //create next node content
  113. content2=" print("+funcname;
  114.  
  115. }
  116.  
  117. }
  118. }
  119.  
  120. var appex=document.getElementById("app")
  121. appex.appendChild(addfade());
  122. appex.appendChild(addlight());
  123.  
  124. document.getElementById('light').style.display='block';
  125. document.getElementById('fade').style.display='block';
  126. document.getElementById("contentid").value = contentlist.join('\n');
  127.  
  128. }
  129. }
  130.  
  131.  
  132. }
  133. }
  134. })();