leetcode copy testcase

leetcode自动将测试用例转化为sql语句

As of 2019-09-19. See the latest version.

  1. // ==UserScript==
  2. // @name leetcode copy testcase
  3. // @namespace https://leetcode-cn.com/
  4. // @version 0.13
  5. // @description leetcode自动将测试用例转化为sql语句
  6. // @author LiMingYu
  7. // @match https://leetcode-cn.com/submissions/detail/*
  8. // @grant none
  9. // ==/UserScript==
  10. //获取表的对象集合信息
  11. var creattable="";
  12. var sql=""
  13. var deltable=""
  14. var tableinfos=JSON.parse(pageData.submissionData.input).headers
  15. //得到插入的行信息
  16. var tablerows=JSON.parse(pageData.submissionData.input).rows
  17. //获得表名,字段名
  18. for(var name in tableinfos){
  19. //得到列头
  20. var list=new Array();
  21. for(var k=0;k<tableinfos[name].length;k++)
  22. {
  23.  
  24. list.push(tableinfos[name][k]+" varchar(200)")
  25. }
  26.  
  27. creattable+='CREATE TABLE IF NOT EXISTS '+name+'('+list.join(",")+')'+';\n';
  28. deltable+='delete from '+name;
  29. }
  30. //处理数据
  31.  
  32. for(var row in tablerows)
  33. {
  34.  
  35. for(var i=0;i<tablerows[row].length;i++)
  36. {
  37. for(var j=0;j<tablerows[row][i].length;j++)
  38. {
  39. if(tablerows[row][i][j]==null)
  40. tablerows[row][i][j]="/"
  41. tablerows[row][i][j]='"'+tablerows[row][i][j]+'"'
  42. }
  43.  
  44. }
  45. console.log(tablerows[row].length)
  46. for(var m=0;m<tablerows[row].length;m++)
  47. {
  48. var str=tablerows[row][m].toString();
  49. sql+='INSERT INTO'+' '+row+ ' VALUES ('+str.replace(new RegExp("\\/","g"),"null")+')'+';\n';
  50. }
  51. }
  52.  
  53. //添加到页面上
  54. $("#details-summary").append('<input type="button" value="复制" id="copy" class="btn btn-primary" ><input type="button" value="隐藏" id="watch" style="margin-left:10px" class="btn btn-primary" > <textarea style="margin-top:10px;height:200px;display:block" id="sql" value="" " class="form-control"></textarea>')
  55. $("#sql").val(creattable+deltable+sql)
  56. //添加滑动及其处理
  57. $("#watch").click(
  58. function(){
  59. if($("#watch").val()=="查看")
  60. {
  61. $('#sql').css("display","block")
  62. $("#watch").val("隐藏")
  63. }
  64. else
  65. {
  66. $('#sql').css("display","none")
  67. $("#watch").val("查看")
  68.  
  69. }
  70. })
  71.  
  72. $("#copy").click(
  73. function(){
  74. var urlresult=document.getElementById("sql")
  75. urlresult.select(); // 选择对象
  76. document.execCommand("Copy"); // 执行浏览器复制命令
  77. alert("已复制好,可贴粘。");
  78. }
  79.  
  80. )