LeetCode Copy Testcase

自动将 LeetCode 测试用例转化为 SQL 和 Pandas 语句

  1. // ==UserScript==
  2. // @name LeetCode Copy Testcase
  3. // @namespace https://leetcode.cn/
  4. // @version 0.2
  5. // @description 自动将 LeetCode 测试用例转化为 SQL 和 Pandas 语句
  6. // @author wangxm
  7. // @match https://leetcode.cn/problems/*
  8. // @match https://leetcode-cn.com/submissions/detail/*
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 使用 MutationObserver 确保在页面加载完成后执行
  17. const observer = new MutationObserver((mutations, obs) => {
  18. const targetElement = document.querySelector("#details-summary");
  19. if (targetElement) {
  20. obs.disconnect();
  21. addButtons(targetElement);
  22. }
  23. });
  24.  
  25. observer.observe(document, { childList: true, subtree: true });
  26.  
  27. function addButtons(targetElement) {
  28. // 获取表的对象集合信息
  29. let createTableSQL = "";
  30. let insertSQL = "";
  31. let dropTableSQL = "";
  32. let pandasCode = "";
  33.  
  34. const tableInfos = JSON.parse(pageData.submissionData.input).headers;
  35. const tableRows = JSON.parse(pageData.submissionData.input).rows;
  36.  
  37. // 生成表创建和删除语句
  38. for (const name in tableInfos) {
  39. const columns = tableInfos[name].map(col => `${col} VARCHAR(200)`);
  40. createTableSQL += `CREATE TABLE IF NOT EXISTS ${name} (${columns.join(", ")});\n`;
  41. dropTableSQL += `DROP TABLE IF EXISTS ${name};\n`;
  42.  
  43. // 生成 Pandas 代码
  44. const pandasColumns = tableInfos[name].map(col => `"${col}"`);
  45. const pandasData = tableRows[name].map(row => `[${row.map(value => value === null ? "None" : `"${value}"`).join(", ")}]`);
  46. pandasCode += `import pandas as pd\n${name}_df = pd.DataFrame(columns=[${pandasColumns.join(", ")}], data=[${pandasData.join(", ")}])\n\n`;
  47. }
  48.  
  49. // 处理数据并生成插入语句
  50. for (const tableName in tableRows) {
  51. tableRows[tableName].forEach(row => {
  52. const values = row.map(value => value === null ? "NULL" : `"${value}"`);
  53. insertSQL += `INSERT INTO ${tableName} VALUES (${values.join(", ")});\n`;
  54. });
  55. }
  56.  
  57. // 添加到页面上
  58. targetElement.insertAdjacentHTML('beforeend', `
  59. <input type="button" value="复制 SQL" id="copy-sql" class="btn btn-primary">
  60. <input type="button" value="复制 Pandas" id="copy-pandas" class="btn btn-primary" style="margin-left:10px">
  61. <input type="button" value="隐藏" id="toggle" style="margin-left:10px" class="btn btn-primary">
  62. <textarea style="margin-top:10px;height:200px;display:block" id="sql" class="form-control"></textarea>
  63. <textarea style="margin-top:10px;height:200px;display:none" id="pandas" class="form-control"></textarea>
  64. `);
  65.  
  66. document.getElementById("sql").value = dropTableSQL + createTableSQL + insertSQL;
  67. document.getElementById("pandas").value = pandasCode;
  68.  
  69. // 添加滑动及其处理
  70. document.getElementById("toggle").addEventListener('click', function() {
  71. const sqlTextarea = document.getElementById("sql");
  72. const pandasTextarea = document.getElementById("pandas");
  73. const isHidden = sqlTextarea.style.display === "none";
  74. sqlTextarea.style.display = isHidden ? "block" : "none";
  75. pandasTextarea.style.display = isHidden ? "none" : "block";
  76. this.value = isHidden ? "隐藏" : "查看";
  77. });
  78.  
  79. document.getElementById("copy-sql").addEventListener('click', function() {
  80. const sqlTextarea = document.getElementById("sql");
  81. sqlTextarea.select();
  82. document.execCommand("Copy");
  83. alert("SQL 已复制好,可贴粘。");
  84. });
  85.  
  86. document.getElementById("copy-pandas").addEventListener('click', function() {
  87. const pandasTextarea = document.getElementById("pandas");
  88. pandasTextarea.select();
  89. document.execCommand("Copy");
  90. alert("Pandas 代码已复制好,可贴粘。");
  91. });
  92. }
  93. })();