Greasy Fork is available in English.

leetcode copy testcase

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

  1. // ==UserScript==
  2. // @name leetcode copy testcase
  3. // @namespace https://leetcode-cn.com/
  4. // @version 0.144
  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+='DROP TABLE if EXISTS '+name+';\n';
  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]="a"
  41. tablerows[row][i][j]='"'+tablerows[row][i][j]+'"'
  42. }
  43.  
  44. }
  45. for(var m=0;m<tablerows[row].length;m++)
  46. {
  47. var str=tablerows[row][m].toString();
  48. sql+='INSERT INTO'+' '+row+ ' VALUES ('+str.replace(new RegExp("a","g"),"null")+')'+';\n';
  49. }
  50. }
  51.  
  52. //添加到页面上
  53. $("#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>')
  54. $("#sql").val(deltable+creattable+sql)
  55. //添加滑动及其处理
  56. $("#watch").click(
  57. function(){
  58. if($("#watch").val()=="查看")
  59. {
  60. $('#sql').css("display","block")
  61. $("#watch").val("隐藏")
  62. }
  63. else
  64. {
  65. $('#sql').css("display","none")
  66. $("#watch").val("查看")
  67.  
  68. }
  69. })
  70.  
  71. $("#copy").click(
  72. function(){
  73. var urlresult=document.getElementById("sql")
  74. urlresult.select(); // 选择对象
  75. document.execCommand("Copy"); // 执行浏览器复制命令
  76. alert("已复制好,可贴粘。");
  77. }
  78.  
  79. )