Greasy Fork镜像 还支持 简体中文。

leetcode题目转markdown

leetcode转md

  1. // ==UserScript==
  2. // @name leetcode题目转markdown
  3. // @namespace https://github.com/faithererer/leetcode2md
  4. // @version 2024-02-07
  5. // @description leetcode转md
  6. // @author faithererer
  7. // @match https://leetcode.cn/problems/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.cn
  9. // @grant none
  10. // @resource css https://unpkg.com/layui@2.9.6/dist/css/layui.css
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14.  
  15. (function () {
  16. 'use strict';
  17.  
  18. // get title
  19.  
  20. let script = document.createElement("script");
  21. script.type = "text/javascript";
  22. script.src = "https://unpkg.com/layui@2.9.6/dist/layui.js";
  23. document.body.appendChild(script);
  24.  
  25. // let link = document.createElement("link");
  26. // link.rel = "stylesheet";
  27. // link.href = "https://unpkg.com/layui@2.9.6/dist/css/layui.css";
  28. // document.head.appendChild(link);
  29. var markdownText = ''
  30.  
  31.  
  32. window.onload = function () {
  33. // 创建一个按钮元素
  34. const btn = document.createElement("button");
  35. btn.innerText = "复制为Markdown";
  36. btn.style.fontSize = "16px";
  37. // 找到类名为"text-title-large"的元素
  38. const targetElement = document.querySelector(".text-title-large");
  39.  
  40. targetElement.parentNode.insertBefore(btn, targetElement.nextSibling);
  41.  
  42. btn.onclick = function () {
  43. // 获取题目的标题
  44. var title = document.querySelector(".text-title-large > a").innerText;
  45. title = '# ' + title + '\n'
  46. markdownText = markdownText + title
  47. // 获取题目的内容
  48. var content = document.querySelector("div[data-track-load='description_content']").innerHTML;
  49. markdownText = markdownText + htmlToMarkdown(content)
  50. // 复制到剪贴板
  51. navigator.clipboard.writeText(markdownText);
  52. // 提示用户复制成功
  53. layui.use('layer', function () {
  54. var layer = layui.layer;
  55. layer.msg('复制成功');
  56. });
  57. }
  58.  
  59.  
  60. }
  61.  
  62.  
  63.  
  64. function htmlToMarkdown(html) {
  65. // 处理<pre>标签
  66. html = html.replace(/<pre>([\s\S]*?)<\/pre>/g, (match, codeBlock) => {
  67. return '```\n' + codeBlock.trim() + '\n```';
  68. });
  69. // 处理列表
  70. html = html.replace(/<ul>/g, '');
  71. html = html.replace(/<\/ul>/g, '');
  72. html = html.replace(/\s*<li>/g, '- ');
  73. html = html.replace(/<\/li>/g, '\n'); // 保持列表项结束后的换行
  74. // 转换<p>、<strong>和<code>
  75. html = html.replace(/<p>([\s\S]*?)<\/p>/g, '$1\n');
  76. html = html.replace(/<strong>([\s\S]*?)<\/strong>/g, '**$1**');
  77. html = html.replace(/<code>([\s\S]*?)<\/code>/g, '`$1`');
  78. // 移除HTML实体&nbsp;
  79. html = html.replace(/&nbsp;/g, ' ');
  80. // 匹配<img>标签,包括那些具有style属性的标签
  81. html = html.replace(/<img\s*(?:alt="([^"]*)"\s*)?src="([^"]+)"\s*(?:style="[^"]*"\s*)?\/?>/g, (match, alt = '', src) => {
  82. // 如果alt属性不存在,使用空字符串作为替代文本
  83. alt = alt || '';
  84. return `![${alt}](${src})`;
  85. });
  86. // 移除剩余的HTML标签(保守处理,可能会移除一些未处理的标签)
  87. html = html.replace(/<[^>]*>/g, '');
  88. // 移除连续的空行
  89. html = html.replace(/\n{2,}/g, '\n\n');
  90. return html.trim();
  91. }
  92.  
  93.  
  94.  
  95. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址