// ==UserScript== // @name 中国哲学书电子化计划下载 // @namespace https://bbs.tampermonkey.net.cn/ // @version 0.2.0 // @description 中国哲学书电子化计划添加下载按钮 // @author 昊色居士 // @match *://ctext.org/wiki.pl?if=*&chapter=* // ==/UserScript== (function () { 'use strict'; // 标题的元素 let title = document.querySelector('.wikisectiontitle').firstChild.nodeValue let texts = [] texts.push(`# ${title}\n`) // 查询所有的tr let all_text_el = document.querySelectorAll('table[style="width: 100%;"]>tbody>tr') // 依次遍历所有的tr all_text_el.forEach(el => { // 子标题的元素 let title_el = el.querySelector('.wikisubsectiontitle') // 文本的元素 let text_el = el.querySelector('td:nth-child(2)') // 有标题元素就把标题文本加入进去,否则就加入文本 if (title_el) { texts.push(`## ${title_el.textContent}\n`) } else { texts.push(`${text_el.textContent}\n`) } }) // 换行符分割,拼成字符串 let text = texts.join('\n') // 添加下载按钮 let a = document.createElement('a') a.innerHTML = ''; document.querySelector('div[class="noprint opt"]').appendChild(a) // 增加下载事件 let blob = new Blob([text]) let filename = `${title}.md` a.download = filename a.href = URL.createObjectURL(blob) URL.revokeObjectURL(blob) })();