知乎问答重排for印象笔记

增加一个“重排”链接于导航栏,点击后重排页面,只保留问题和答案,然后可用“印象笔记·剪藏”收藏。

目前為 2018-09-09 提交的版本,檢視 最新版本

// ==UserScript==
// @name         知乎问答重排for印象笔记
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  增加一个“重排”链接于导航栏,点击后重排页面,只保留问题和答案,然后可用“印象笔记·剪藏”收藏。
// @author       twchen
// @match        https://www.zhihu.com/question/*/answer/*
// @grant        none
// ==/UserScript==

(function() {
  "use strict";

  function format() {
    const showMoreBtn = document.querySelector(
      "button.Button.QuestionRichText-more"
    );
    if(showMoreBtn !== null) showMoreBtn.click();
    const title = document.querySelector("h1.QuestionHeader-title");
    const detail = document.querySelector("div.QuestionHeader-detail");
    const answer = document.querySelector("div.Card.AnswerCard");

    const div = document.createElement("div");
    div.appendChild(title);
    div.appendChild(detail);

    Object.assign(div.style, {
      "background-color": "white",
      margin: "0.8rem 0",
      padding: "0.2rem 1rem 1rem"
    });

    const body = document.getElementsByTagName("body")[0];
    while (body.firstChild) {
      body.removeChild(body.firstChild);
    }

    body.appendChild(div);
    body.appendChild(answer);
  }

  function addLinkToNav() {
    const nav = document.querySelector("nav.AppHeader-nav");
    const a = document.createElement("a");
    a.href = "#";
	a.text = "重排";
	a.classList.add('AppHeader-navItem');
    a.onclick = e => {
      e.preventDefault();
      format();
    };
    nav.appendChild(a);
  }

  addLinkToNav();
})();