// ==UserScript== // @name leetcode题目转markdown // @namespace https://github.com/faithererer/leetcode2md // @version 2024-02-07 // @description leetcode转md // @author faithererer // @match https://leetcode.cn/problems/* // @icon https://www.google.com/s2/favicons?sz=64&domain=leetcode.cn // @grant none // @resource css https://unpkg.com/layui@2.9.6/dist/css/layui.css // @license MIT // @downloadURL https://update.greasyfork.icu/scripts/486822/leetcode%E9%A2%98%E7%9B%AE%E8%BD%ACmarkdown.user.js // @updateURL https://update.greasyfork.icu/scripts/486822/leetcode%E9%A2%98%E7%9B%AE%E8%BD%ACmarkdown.meta.js // ==/UserScript== (function () { 'use strict'; // get title let script = document.createElement("script"); script.type = "text/javascript"; script.src = "https://unpkg.com/layui@2.9.6/dist/layui.js"; document.body.appendChild(script); // let link = document.createElement("link"); // link.rel = "stylesheet"; // link.href = "https://unpkg.com/layui@2.9.6/dist/css/layui.css"; // document.head.appendChild(link); var markdownText = '' window.onload = function () { // 创建一个按钮元素 const btn = document.createElement("button"); btn.innerText = "复制为Markdown"; btn.style.fontSize = "16px"; // 找到类名为"text-title-large"的元素 const targetElement = document.querySelector(".text-title-large"); targetElement.parentNode.insertBefore(btn, targetElement.nextSibling); btn.onclick = function () { // 获取题目的标题 var title = document.querySelector(".text-title-large > a").innerText; title = '# ' + title + '\n' markdownText = markdownText + title // 获取题目的内容 var content = document.querySelector("div[data-track-load='description_content']").innerHTML; markdownText = markdownText + htmlToMarkdown(content) // 复制到剪贴板 navigator.clipboard.writeText(markdownText); // 提示用户复制成功 layui.use('layer', function () { var layer = layui.layer; layer.msg('复制成功'); }); } } function htmlToMarkdown(html) { // 处理
标签
        html = html.replace(/
([\s\S]*?)<\/pre>/g, (match, codeBlock) => {
            return '```\n' + codeBlock.trim() + '\n```';
        });
        // 处理列表
        html = html.replace(/