Hypothesis导出为markdown文件

导出Hypothesis高亮和注释为markdown文件

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Hypothesis导出为markdown文件
// @namespace    https://coycs.com/
// @version      1.0.0
// @description  导出Hypothesis高亮和注释为markdown文件
// @author       coycs
// @match        *://hypothes.is/users/*
// @grant        none
// @license MIT
// ==/UserScript==
 
(function () {
  "use strict";
 
  window.onload = function () {
    // 添加全部导出按钮
    let exportAllBtn = document.createElement("span");
    exportAllBtn.textContent = "导出全部";
    exportAllBtn.style.cssText = "margin-left: 8px;font-weight: bold;cursor: pointer;";
    document.querySelector(".search-results__total").appendChild(exportAllBtn);
    exportAllBtn.addEventListener("click", function () {
      let exportBtns = document.querySelectorAll("#exportBtn");
      exportBtns.forEach(n => {
        n.click();
      });
    });
 
    // 添加导出按钮
    const listitems = document.querySelectorAll('li[role="listitem"]');
    listitems.forEach(function (e) {
      e.style.cssText = "display: flex;";
      let bucket = e.querySelector(".search-result-bucket.js-search-bucket");
      bucket.style.cssText = "flex: 1;";
      let exportBtn = document.createElement("div");
      exportBtn.id = "exportBtn";
      exportBtn.textContent = "导出";
      exportBtn.style.cssText =
        "display: flex;align-items: center;justify-content: center;font-weight: bold;color: inherit;cursor: pointer;";
      let that = e;
      exportBtn.addEventListener("click", function () {
        ExportListItem(that);
      });
      e.appendChild(exportBtn);
    });
  };
 
  // 导出单个项目
  function ExportListItem(listitem) {
    const title = listitem.querySelector('a[data-ref="title"]').innerText;
    const url_a = listitem.querySelector(
      "div.search-bucket-stats__val.search-bucket-stats__url > a"
    );
    let outcome = new String();
    if (url_a) {
      const url = url_a.href;
      outcome = outcome.concat(`> 来自:${url}\n`);
    } else {
      outcome = outcome.concat("> 来自:本地文件\n");
    }
    const annotations = listitem.querySelectorAll("li.annotation-card");
    for (let i = annotations.length - 1; i > -1; i--) {
      const quote = annotations[i]
        .querySelector("blockquote.annotation-card__quote")
        .innerText.trim();
      const text = annotations[i].querySelector("div.annotation-card__text").innerText.trim();
      if (text) {
        outcome = outcome + "\n" + "> " + quote + "\n" + "\n" + text + "\n";
      } else {
        outcome = outcome + "\n" + "> " + quote + "\n";
      }
    }
    // 导出为markdown文件
    let blob = new Blob([outcome], { type: "application/md" });
    let aTag = document.createElement("a");
    aTag.download = title + ".md";
    aTag.href = URL.createObjectURL(blob);
    aTag.click();
    URL.revokeObjectURL(blob);
  }
})();