// ==UserScript==
// @name 知乎下载器
// @namespace http://howardzhangdqs.eu.org/
// @version 0.1.8-0590f6d7
// @description 一键复制知乎文章、回答为Markdown
// @author HowardZhangdqs
// @match *://www.zhihu.com/*
// @match *://zhuanlan.zhihu.com/*
// @license MIT
// @icon https://static.zhihu.com/heifetz/favicon.ico
// @grant none
// ==/UserScript==
/** 更新日志
* 23.10.30: 脚本开写
* 23.10.31:
* feat: 解析渲染表格
* feat: 解析渲染链接
* fix: 加了一个被忘掉的break,但是我忘了是哪忘了加了
* fix: 修复编辑框会被加上`复制为Markdown`的按钮
* doc: 给types加了完整的注释
* doc: 给Lexer和Parser函数添加完整的注释
*/
(()=>{"use strict";function e(){for(var e="复制为Markdown",n=function(n){var o,c,i;return n.parentElement.classList.contains("Editable")||n.children[0].classList.contains("zhihucopier-button")?"continue":(i=function(e){for(var n=[],r=0;r<e.length;r++){var o=e[r];switch(o.tagName.toLowerCase()){case"h2":n.push({type:t.H1,text:o.textContent,dom:o});break;case"h3":n.push({type:t.H2,text:o.textContent,dom:o});break;case"div":o.classList.contains("highlight")?n.push({type:t.Code,content:o.textContent,language:o.querySelector("pre > code").classList.value.slice(9),dom:o}):o.classList.contains("RichText-LinkCardContainer")&&(c=o.firstChild,n.push({type:t.Link,text:c.getAttribute("data-text"),href:c.href,dom:o}));break;case"blockquote":n.push({type:t.Blockquote,content:a(o),dom:o});break;case"figure":var c=o.querySelector("img").getAttribute("data-actualsrc");c&&n.push({type:t.Figure,src:c,dom:o});break;case"ul":var i=Array.from(o.querySelectorAll("li"));n.push({type:t.UList,content:i.map((function(e){return a(e)})),dom:o});break;case"ol":i=Array.from(o.querySelectorAll("li")),n.push({type:t.Olist,content:i.map((function(e){return a(e)})),dom:o});break;case"p":n.push({type:t.Text,content:a(o),dom:o});break;case"hr":n.push({type:t.HR,dom:o});break;case"table":var s=function(e){for(var t=[],n=0,a=Array.from(e.rows);n<a.length;n++){var r=a[n];r=Array.from(r.cells),t.push(r.map((function(e){return e.textContent.trim()})))}return t}(o);n.push({type:t.Table,content:s,dom:o})}}return n}(n.childNodes),o=function(e){for(var n=[],a=0;a<e.length;a++)!function(a){var o=e[a];switch(o.type){case t.Figure:n.push(""));break;case t.Code:n.push("```".concat(o.language||"","\n").concat(o.content,"```"));break;case t.UList:n.push(o.content.map((function(e){return"- ".concat(r(e))})).join("\n"));break;case t.Olist:n.push(o.content.map((function(e,t){return"".concat(t+1,". ").concat(r(e))})).join("\n"));break;case t.H1:n.push("# ".concat(o.text));break;case t.H2:n.push("## ".concat(o.text));break;case t.Blockquote:n.push(r(o.content,"> "));break;case t.Text:n.push(r(o.content));break;case t.HR:n.push("\n---\n");break;case t.Link:n.push("[".concat(o.text,"](").concat(o.href,")"));break;case t.Table:var c,i=o.content,s=i[0].length,l=new Array(s).fill(0),u=[];for(c in i)for(var h in i[c])l[h]=Math.max(l[h],i[c][h].length);var p=function(e){for(var t="",n=0;n<s;n++)t+="| ".concat(e[n].padEnd(l[n])," ");return t+"|"};u.push(p(i[0])),u.push(function(){for(var e="",t=0;t<s;t++)e+="| ".concat("-".repeat(l[t])," ");return e+"|"}());for(var d=1;d<i.length;d++)u.push(p(i[d]));n.push(u.join("\n"))}}(a);return n}(i),(i=document.createElement("button")).classList.add("zhihucopier-button"),i.innerText=e,i.style.right="0",i.style.top="-2em",i.style.zIndex="999",i.style.width="150px",i.style.height="2em",i.style.backgroundColor="rgba(0,0,0,0.3)",i.style.color="white",i.style.border="white 1px solid",i.style.outline="none",i.style.cursor="pointer",i.style.borderRadius="1em 0 0 1em",i.style.margin="0 0 1em 0",(c=i).style.borderRadius="1em",n.prepend(c),void c.addEventListener("click",(function(){try{navigator.clipboard.writeText(o.join("\n\n")),c.innerHTML="复制成功✅",setTimeout((function(){c.innerHTML=e}),1e3)}catch(t){c.innerHTML="发生未知错误<br>请联系开发者",c.style.height="4em",setTimeout((function(){c.style.height="2em",c.innerHTML=e}),1e3)}})))},o=0,c=Array.from(document.querySelectorAll(".RichText"));o<c.length;o++)n(c[o])}(n=t=t||{})[n.H1=0]="H1",n[n.H2=1]="H2",n[n.Text=2]="Text",n[n.Figure=3]="Figure",n[n.InlineLink=4]="InlineLink",n[n.InlineCode=5]="InlineCode",n[n.InlineMath=6]="InlineMath",n[n.Italic=7]="Italic",n[n.Bold=8]="Bold",n[n.PlainText=9]="PlainText",n[n.UList=10]="UList",n[n.Olist=11]="Olist",n[n.BR=12]="BR",n[n.HR=13]="HR",n[n.Blockquote=14]="Blockquote",n[n.Code=15]="Code",n[n.Link=16]="Link",n[n.Table=17]="Table";var t,n,a=function(e){if("string"==typeof e)return[{type:t.PlainText,text:e}];for(var n=[],r=0,o=Array.from(e.childNodes);r<o.length;r++){var c=o[r];if(c.nodeType==c.TEXT_NODE)n.push({type:t.PlainText,text:c.textContent,dom:c});else{var i=c;switch(i.tagName.toLowerCase()){case"b":n.push({type:t.Bold,content:a(i),dom:i});break;case"i":n.push({type:t.Italic,content:a(i),dom:i});break;case"br":n.push({type:t.BR,dom:i});break;case"code":n.push({type:t.InlineCode,dom:i});break;case"span":i.classList.contains("ztext-math")&&n.push({type:t.InlineMath,content:i.getAttribute("data-tex"),dom:i});break;case"a":n.push({type:t.InlineLink,text:i.textContent,href:i.href,dom:i})}}}return n},r=function(e,n){void 0===n&&(n="");for(var a="",o=0,c=e;o<c.length;o++){var i=c[o];switch(i.type){case t.Bold:a+="**".concat(r(i.content),"**");break;case t.Italic:a+="*".concat(r(i.content),"*");break;case t.InlineLink:a+="[".concat(i.text,"](").concat(i.href,")");break;case t.PlainText:a+=i.text;break;case t.BR:a+="\n"+n;break;case t.InlineCode:a+="`".concat(i.content,"`");break;case t.InlineMath:a+="$".concat(i.content,"$")}}return n+a};document.addEventListener("click",(function(){setTimeout(e,300)})),setTimeout(e,300)})();