notion auto expend

display to the current (nested) page in the sidebar

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         notion auto expend
// @namespace    http://tampermonkey.net/
// @version      2023-12-24
// @author       You
// @match        https://www.notion.so/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=notion.so
// @require     https://code.jquery.com/jquery-3.7.1.min.js
// @description  display  to the current (nested) page in the sidebar
// @license MIT
// @grant        none
// ==/UserScript==

(function () {
  'use strict';

  // Your code here...
  let currentPagePath = "";

  // 检测路径变化
  const checkPathChange = () => {
    const allpathstr = $("div.shadow-cursor-breadcrumb").text()
    if (allpathstr !== currentPagePath) {
      // 执行路径变化后的操作
      console.log('Path has changed!', currentPagePath, "=>", allpathstr);
      // 这里可以添加你想要执行的代码

      // 更新当前页面路径
      currentPagePath = allpathstr;
      const allpath = allpathstr.split("/")
      const s = ".notion-outliner-private:nth-child(1) > div "
      const level1p = ".notion-outliner-private:nth-child(1) > div > .notion-selectable > a > div"
      let levelP = ""
      for (let i = 1; i < allpath.length; i++) {
        const path = allpath[i - 1]
        console.log(i, path)
        if (i === 1) {
          const level1 = $(level1p)
          //
          for (let j = 1; j <= level1.length; j++) {
            let rootitem = level1[j - 1]
            if (rootitem.innerText === path) {
              console.log("find!", path)
              const buttonpath = s + `> .notion-selectable:nth-child(${j})` + `> a > div > div:nth-child(1)>div`
              levelP = s + `> .notion-selectable:nth-child(${j})`
              // 判断attr
              const b = $(buttonpath)
              const attr = b.attr("aria-expanded")
              console.log("attr", attr)
              if (attr === "false") {
                console.log("click")
                b.click()
              }
            }
          }
          // console.log("xxxx",level1[0].innerText)
        } else {
          const level = $(levelP + ">div > div> " + ".notion-selectable" + ">a >div")
          console.log(levelP)
          for (let j = 1; j <= level.length; j++) {
            let rootitem = level[j - 1]
            if (rootitem.innerText === path) {
              console.log("find 2!", path, j)
              const buttonpath = levelP + `>div > div> .notion-selectable:nth-child(${j})` + ">a >div" + "> div:nth-child(1)>div"
              levelP = levelP + `>div > div> .notion-selectable:nth-child(${j})`
              // 判断attr
              console.log("path is ", buttonpath)
              const b = $(buttonpath)
              const attr = b.attr("aria-expanded")
              console.log("attr", i, attr)
              if (attr === "false") {
                console.log("click")
                b.click()
              }
            }
          }
        }
      }
    }
    setTimeout(checkPathChange, 1000);
  };

  // 定时检测路径变化
  setTimeout(checkPathChange, 1000); // 每秒检测一次,可以根据需要调整间隔时间
})();