// ==UserScript== // @name notion plugin // @namespace http://tampermonkey.net/ // @version 0.5 // @description add notion outline view // @author fengxxc // @match https://www.notion.so/* // @icon https://www.google.com/s2/favicons?sz=64&domain=notion.so // @grant none // @license MIT // @downloadURL https://update.greasyfork.icu/scripts/445715/notion%20plugin.user.js // @updateURL https://update.greasyfork.icu/scripts/445715/notion%20plugin.meta.js // ==/UserScript== (function() { 'use strict'; /** * 一、二、三 级标题的className分别为 * notion-header-block * notion-sub_header-block * notion-sub_sub_header-block */ function getOutlineTokens() { const headerBlock = document.querySelectorAll('.notion-header-block,.notion-sub_header-block,.notion-sub_sub_header-block') const tokens = [] for (var i = 0; i < headerBlock.length; i++) { const id = headerBlock[i].getAttribute('data-block-id').replaceAll('-', '') // const notranslate = headerBlock[i].querySelector('.notranslate') const level = headerBlock[i].className.split('sub').length const header = headerBlock[i].innerText tokens.push({id, level, header}) } return tokens } function getOutlineHTMLs(outlineTokens) { const pathname = window.location.pathname return outlineTokens.map(token => (`
${token.header}
`)) } function getOutlineBox(outlineHTMLs) { return `
${outlineHTMLs.join('')}
` } let notionFrame = null let existBox = false let lastTokenStr = '' const initInterval = setInterval(() => { // 内容居左 document.querySelector('.whenContentEditable > main').style.justifyContent = 'left'; notionFrame = notionFrame || document.querySelector('.notion-frame') if (notionFrame) { // init completed // clearInterval(initInterval) const tokens = getOutlineTokens() const tokenStr = JSON.stringify(tokens) if (!existBox) { notionFrame.insertAdjacentHTML('afterend', getOutlineBox(getOutlineHTMLs(tokens))) existBox = true lastTokenStr = tokenStr } else { if (lastTokenStr != tokenStr) { document.querySelector('#outline_view').innerHTML = getOutlineHTMLs(tokens).join('') lastTokenStr = tokenStr } } } }, 1000) })();