Parcourir la source

Merge remote-tracking branch 'origin/master'

tianyunperfect il y a 3 ans
Parent
commit
ee73b1c56a
2 fichiers modifiés avec 115 ajouts et 7 suppressions
  1. 11 7
      tmp/monkey/notion-双击打开.js
  2. 104 0
      tmp/monkey/notion-标题编号.js

+ 11 - 7
tmp/monkey/notion-双击打开.js

@@ -9,11 +9,15 @@
 
 // ==/UserScript==
 (async () => {
-    await getDom("div[data-block-id]");
-    $("div[data-block-id]").dblclick((ev) => {
-        ev.stopPropagation();
-        let block_id = $(ev.target).parents("div[data-block-id]").eq(0).attr('data-block-id');
-        block_id = block_id.replaceAll("-","")
-        window.open("https://www.notion.so/tianyunperfect/" + block_id, "_blank");
-    })
+    setInterval(() => {
+        $("div[data-block-id]").off("dblclick").on("dblclick", (ev) => {
+            ev.stopPropagation();
+            if (ev.metaKey) {
+                let block_id = $(ev.target).parents("div[data-block-id]").eq(0).attr('data-block-id');
+                block_id = block_id.replaceAll("-", "")
+                window.open("https://www.notion.so/tianyunperfect/" + block_id, "_blank");
+            }
+        })
+    }, 2000)
+
 })();

+ 104 - 0
tmp/monkey/notion-标题编号.js

@@ -0,0 +1,104 @@
+// ==UserScript==
+// @name        T-Notion-标题编号
+// @namespace   Violentmonkey Scripts
+// @match       *://www.notion.so/*
+// @grant       none
+// @version     1.0
+// @author      -
+// @description 2021/11/5 下午2:18:51
+// @require           https://git.tianyunperfect.cn/tianyunperfect/web-base/raw/master/tmp/monkey/util.js?a=1
+// ==/UserScript==
+(async () => {
+    function myHeader() {
+        // 删除最后gap个数字后seq+1
+        function leftMoveAndAddSeq(str, gap) {
+            let arr = str.split(".")
+            arr[arr.length - 2 - gap] = parseInt(arr[arr.length - 2 - gap]) + 1
+            while (gap >= 0) {
+                arr.pop();
+                gap -= 1;
+            }
+            return arr.join(".") + ".";
+        }
+
+        // 删除过去生成的序号
+        function removeOldPrefix(content) {
+            return content.replace(/^(\d+\.\d+\.\d+\.\s)/, '').replace(/^(\d+\.\d+\.\s)/, '').replace(/^(\d+\.\s)/, '')
+        }
+
+        // 获取所有的header
+        const $headers = [...document.querySelectorAll(`.notion-header-block, .notion-sub_header-block, .notion-sub_sub_header-block`)];
+        // 遍历生成序号,固定格式:
+        // level1 x.
+        // level2 x.x.
+        // level3 x.x.x.
+        let outermostLevel = 0 // 最外层的level
+        let lastPrefix = '' // 上一项的前缀
+        let lastLevel = 0 // 上一项的level
+        for (let i = 0; i < $headers.length; i++) {
+            const $header = $headers[i]
+            const level = $header.classList.contains('notion-header-block')
+                ? 1 : $header.classList.contains('notion-sub_header-block')
+                    ? 2 : 3
+            let curPrefix = ''
+            if (i === 0) {
+                outermostLevel = level
+                curPrefix = '1.'
+            } else {
+                if (level < outermostLevel) {
+                    // 不规范格式,直接停止转换
+                    break
+                }
+                if (level === lastLevel) {
+                    // 同一层级
+                    curPrefix = leftMoveAndAddSeq(lastPrefix, 0)
+                }
+                if (level > lastLevel) {
+                    // 下一层级
+                    curPrefix = lastPrefix + 1 + '.'
+                }
+                if (level < lastLevel) {
+                    // 外层
+                    if (lastLevel - level === 1) {
+                        if (lastPrefix.length < 4) break
+                        curPrefix = leftMoveAndAddSeq(lastPrefix, 1)
+                    } else if (lastLevel - level === 2) {
+                        if (lastPrefix.length < 6) break
+                        curPrefix = leftMoveAndAddSeq(lastPrefix, 2)
+                    }
+                }
+            }
+
+            // 删除之前生成的prefix
+            const originContent = removeOldPrefix($header.textContent)
+            // 更改dom
+            const $edit = $header.querySelector('.notranslate[contenteditable=true]')
+            $edit.textContent = curPrefix + ' ' + originContent
+            // 手动触发input event 以触发notion进行远程更新
+            setTimeout(() => {
+                $edit.dispatchEvent(new Event('input', {
+                    bubbles: true,
+                    cancelable: true,
+                }))
+            }, 0)
+            // 更新变量
+            lastPrefix = curPrefix
+            lastLevel = level
+        }
+    }
+
+    document.onkeydown = function (ev) {
+        if (ev.key === 'h' && ev.shiftKey) {
+            ev.preventDefault() // 关闭浏览器快捷键
+            myHeader()
+        }
+    }
+    setTimeout(() => {
+        myHeader();
+    }, 2000);
+
+    setInterval(() => {
+        myHeader();
+    }, 20000);
+
+})();