AcWing content to markdown

将AcWing上的内容转换为markdown

目前为 2022-03-20 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name AcWing content to markdown
  3. // @namespace acwing
  4. // @match https://www.acwing.com/*
  5. // @grant GM_setClipboard
  6. // @version 1.1
  7. // @author -
  8. // @description 将AcWing上的内容转换为markdown
  9. // @require https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js
  10. // @require https://cdn.bootcdn.net/ajax/libs/turndown/7.1.1/turndown.min.js
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. let turndownService = new TurndownService();
  15. turndownService.addRule('pre', {
  16. filter: 'pre',
  17. replacement: function (content, node) {
  18. let t = $(node).attr("class").split(/\s+/).slice(-1);
  19. return "```" + t + "\n" + content.trim() + "\n```";
  20. }
  21. });
  22.  
  23. $("div[data-tab='preview-tab-content']").before(
  24. "<div> <button class='html2md-view'>显示markdown</button> <button class='html2md-cb'>复制markdown到剪贴板</button> </div>"
  25. );
  26.  
  27.  
  28. $(".html2md-cb").click(function() {
  29. let target = $(this).parent().next().get(0);
  30. if (!target.markdown)
  31. target.markdown = turndownService.turndown($(target).html());
  32. GM_setClipboard(target.markdown);
  33. // console.log(markdown);
  34. $(this).text("已复制到剪贴板");
  35. });
  36.  
  37. $(".html2md-view").click(function() {
  38. let target = $(this).parent().next().get(0);
  39. console.log(target);
  40. if (target.viewmd) {
  41. target.viewmd = false;
  42. $(this).text("显示markdown");
  43. $(target).html(target.original_html);
  44. } else {
  45. target.viewmd = true;
  46. if (!target.original_html)
  47. target.original_html = $(target).html();
  48. if (!target.markdown)
  49. target.markdown = turndownService.turndown($(target).html());
  50. $(this).text("显示原始内容");
  51. $(target).html(`<textarea oninput="$(this).parent().get(0).markdown=this.value;" style="width:100%; height:400px;"> ${target.markdown} </textarea>`);
  52. }
  53. });