processon 导出

支持脑图文本导出

  1. // ==UserScript==
  2. // @name processon 导出
  3. // @namespace https://www.yffjglcms.com/
  4. // @version 0.1.20230301
  5. // @description 支持脑图文本导出
  6. // @author yffjglcms
  7. // @match *://www.processon.com/*
  8. // @grant none
  9. // @require https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.min.js
  10. // ==/UserScript==
  11.  
  12. // Your code here...
  13.  
  14. (function () {
  15. $($('#outline-ifram')).load(()=>{
  16. console.log('ifreame done')
  17.  
  18. const $if = $($("#outline-ifram")).contents()
  19.  
  20. // 导航。两种页面不同的样式
  21. const $nav = $(".view-header-right")[0] ? $(".view-header-right") : $(window.parent.document).find('.file_head_right')
  22. // 是否为脑图
  23. const $mind_designer = $(".mind-designer")
  24. // 大纲
  25.  
  26. // 大纲标题
  27. const $title = $("#root > div");
  28. const $content = $if.find("#outline-con")
  29.  
  30.  
  31.  
  32. // 获取标题
  33. function getTitle() {
  34. return $title.html()
  35. }
  36.  
  37. console.log(getTitle())
  38.  
  39. addBtn();
  40. bindExport();
  41.  
  42. // 是否为脑图
  43. function isMindView() {
  44. return !!($mind_designer[0])
  45. }
  46.  
  47. // 添加导出按钮
  48. function addBtn() {
  49. if (isMindView()) {
  50. if(!$('#exportIt')[0]) // 避免重重创建按钮
  51. $nav.prepend(`<button class="po-button button" id="exportIt">导出</button>`)
  52. } else {
  53. if(!$('#notSupportExportIt')[0]) // 避免重重创建按钮
  54. $nav.prepend(`<button class="po-button button" id="notSupportExportIt" disable>非脑图,不支持导出</button>`)
  55. }
  56. }
  57.  
  58. // 获取结果
  59. function getResult(){
  60. // debugger
  61. var cld = []
  62. var els = $content.children(".node-element.wider")
  63. for (let i = 0; i < els.length; i++) {
  64. cld.push(getContent(els[i]))
  65. }
  66. return {
  67. title: getTitle(),
  68. children: cld
  69. }
  70. }
  71.  
  72.  
  73. // 获取内容,param->node-element
  74. function getContent(node) {
  75. // debugger;
  76. if(!node) return null;
  77. var $node = $(node)
  78. var obj = {};
  79. var children = [];
  80. var title = $node.find(".node-self .node-title").html();
  81.  
  82. if(title.endsWith('<br>')){
  83. title = title.substring(0, title.length-4);
  84. }
  85.  
  86. obj.title = title
  87.  
  88. var $children = $node.children(".node-children").children(".node-element")
  89. if ($children.length > 0) {
  90. for (let i = 0; i < $children.length; i++) {
  91. children.push(getContent($children[i]))
  92. }
  93. obj.children = children;
  94. }
  95.  
  96. return obj;
  97.  
  98. }
  99.  
  100. // 导出
  101. function exportIt() {
  102. // 切换为大纲
  103. $(".item.abstract").click();
  104.  
  105. var result = getResult();
  106. console.log(result)
  107. exportTxt(result)
  108. }
  109.  
  110. // 绑定事件
  111. function bindExport(){
  112. $nav.on("click","#exportIt", exportIt)
  113. }
  114.  
  115. // 导出json文件
  116. function exportJson(result){
  117. var blob = new Blob([JSON.stringify(result,"", "\t")], {type: "text/plain;charset=utf-8"});
  118. saveAs(blob, "mind.json");
  119. }
  120.  
  121. /**导出txt文件 start*/
  122. var uSpan = "\t"
  123. var uLine = "\n"
  124. function getNode(json, span) {
  125. // debugger
  126. if (!json) return ""
  127. var txt = span + json.title + uLine;
  128.  
  129. if (json.children) {
  130. for (let i = 0; i < json.children.length; i++) {
  131. txt += getNode(json.children[i], span + uSpan)
  132. }
  133. }
  134. return txt;
  135. }
  136.  
  137. function exportTxt(result){
  138. var blob = new Blob([getNode(result, "")], {type: "text/plain;charset=utf-8"});
  139. saveAs(blob, getTitle()+".txt");
  140. }
  141. /**导出txt文件 end*/
  142. })
  143.  
  144. })();