Greasy Fork

juejin掘金小册转markdown

掘金:掘金 保存小册 下载 保存 小册

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

// ==UserScript==
// @name         juejin掘金小册转markdown
// @name:zh-CN   一键存为markdown文件
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @description  掘金:掘金 保存小册 下载 保存  小册
// @author       BiubiuUp

// @match        *://juejin.cn/*
// @match        *://juejin.im/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=meitun-test.com
// @license      MIT
// @run-at       document-idle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_addElement
// ==/UserScript==

function addScript(src, callback) {
  var s = document.createElement('script');
  s.setAttribute('src', src);
  s.onload = callback;
  document.body.appendChild(s);
}
// 删除干扰的html标签
function removeDivInPre(node) {
  // 删除 <style> 标签
  const styleNodes = node.querySelectorAll ? node.querySelectorAll('style') : [];
  styleNodes.forEach(styleNode => {
    styleNode.parentNode.removeChild(styleNode);
  });
  if (node.nodeType === Node.ELEMENT_NODE) {
    if (node.tagName === 'PRE') {
      // 在 <pre> 中查找并删除 <div>
      const divNodes = node.querySelectorAll ? node.querySelectorAll('div') : [];
      divNodes.forEach(divNode => {
        divNode.parentNode.removeChild(divNode);
      });
    } else {
      // 对于其他类型的元素节点,递归调用函数
      const childNodes = node.childNodes;
      childNodes.forEach(childNode => {
        removeDivInPre(childNode);
      });
    }
  }

}
function saveAsMd(mdStr) {
  const blob = new Blob([mdStr], {
    type: 'text/markdown'
  })
  // 根据 blob生成 url链接
  const objectURL = URL.createObjectURL(blob)

  // 创建一个 a 标签Tag
  const aTag = document.createElement('a')
  // 设置文件的下载地址
  aTag.href = objectURL
  // 设置保存后的文件名称
  aTag.download = `${title}.md`
  // 给 a 标签添加点击事件
  aTag.click()
  // 释放一个之前已经存在的、通过调用 URL.createObjectURL() 创建的 URL 对象。
  // 当你结束使用某个 URL 对象之后,应该通过调用这个方法来让浏览器知道不用在内存中继续保留对这个文件的引用了。
  URL.revokeObjectURL(objectURL)
}
let index = document.querySelector('.route-active .index').textContent;
let titleDom = document.querySelector('.route-active .title-text')
// 获取span元素的文字内容
const textContent = titleDom.textContent;
// 去除标点符号
const title = index + '.' + textContent.replace(/[`:_.~!@#$%^&*() \+ =<>?"{}|, \/ ;' \\ [ \] ·~!@#¥%……&*()—— \+ ={}|《》?:“”【】、;‘’,。、]/g,
  '');
let dom = document.querySelector('.markdown-body')
// 调用函数开始遍历并删除 div
removeDivInPre(dom);
// 引入 turndown
addScript('https://unpkg.com/turndown/dist/turndown.js', () => {
  let turndownService = new TurndownService({ headingStyle: 'atx', codeBlockStyle: 'fenced' })
  let markdown = turndownService.turndown(dom)
  markdown = `# ${textContent}\n\n${markdown}`;
  // console.log(markdown)
  saveAsMd(markdown)
  // automa插件使用的next函数
  // automaNextBlock()
})

// let TurndownService = require('turndown')