Notion Tags

Makes Notion better - #tags decoration

当前为 2019-06-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Notion Tags
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.4
  5. // @description Makes Notion better - #tags decoration
  6. // @author Andreas Huttenrauch
  7. // @match https://www.notion.so/*
  8. // @grant GM_log
  9. // @grant GM_addStyle
  10. // @run-at document-end
  11. // ==/UserScript==
  12.  
  13. var debug = false;
  14.  
  15. LOG ("starting up");
  16. GM_addStyle('div.gwstagblock { }');
  17. GM_addStyle('div.gwstagblock tag { padding: 1px 5px; font-weight: bold; background: red; border-radius: 5px; color: white; }');
  18.  
  19. // create an observer instance
  20. var observer = new MutationObserver(function(mutations) {
  21. mutations.forEach(function(mutation) {
  22. var target = false;
  23. if ( mutation.target.nodeName == "DIV" ) target = mutation.target;
  24. else target = mutation.target.parentElement;
  25. if ( target.getAttribute("contentEditable") == "true" ) target = target.parentElement;
  26. if ( ! target ) return;
  27. findtags(target);
  28. });
  29. });
  30. var config = { characterData: true, attributes: false, childList: true, subtree: true };
  31. var mutgt = document.body;
  32. observer.observe(mutgt, config);
  33. var lastBlock = false;
  34.  
  35. function findtags(elm) {
  36. elm = elm || document;
  37. //if ( typeof elm.querySelectorAll !== 'function' ) return;
  38. var blocks = elm.querySelectorAll("div[contenteditable='true']");
  39. blocks.forEach(function(el) {
  40. var s = el.textContent
  41. if ( s.indexOf("#") != -1 || el.classList.contains("gwstagblock")) {
  42. markuptags(el);
  43. }
  44. })
  45. }
  46.  
  47. function replaceTags(text, html) {
  48. var matches = text.match(/(#[^\n\s\t]+)/g);
  49. if ( matches ) {
  50. for (var i=0; i<matches.length; i++) {
  51. if ( html.indexOf('<tag>'+matches[i]+'</tag>') == -1 ) {
  52. html = html.replace(matches[i], '<tag>'+matches[i]+'</tag>');
  53. }
  54. }
  55. }
  56. matches = html.match(/<tag>([^#].*?)<\/tag>/g);
  57. if ( matches ) {
  58. for (i=0; i<matches.length; i++) {
  59. html = html.replace('<tag>'+matches[i]+'</tag>', matches[i]);
  60. }
  61. }
  62. return html;
  63. }
  64.  
  65. function removeTags(html) {
  66. var matches = html.match(/<tag>(.*?)<\/tag>/g);
  67. if ( matches ) {
  68. for (var i=0; i<matches.length; i++) {
  69. html = html.replace('<tag>'+matches[i]+'</tag>', matches[i]);
  70. }
  71. }
  72. return html;
  73. }
  74.  
  75. function lostFocusEvent() {
  76. if (document.activeElement === lastBlock) return;
  77. document.removeEventListener('keyup', lostFocusEvent);
  78. document.removeEventListener('click', lostFocusEvent);
  79. markuptags(lastBlock);
  80. }
  81.  
  82. function markuptags(element) {
  83. var isFocused = (document.activeElement === element);
  84. var text = element.textContent;
  85. var html = element.innerHTML;
  86. if ( isFocused ) {
  87. var untagged = removeTags(html);
  88. if ( untagged != html ) {
  89. element.innerHTML = untagged;
  90. }
  91. lastBlock = element;
  92. document.addEventListener('keyup', lostFocusEvent);
  93. document.addEventListener('click', lostFocusEvent);
  94. return;
  95. }
  96. var tagged = replaceTags(text, element.innerHTML);
  97. if ( element.innerHTML == tagged ) return;
  98. element.innerHTML = tagged;
  99. if ( text.indexOf("#") != -1 ) {
  100. if ( ! element.classList.contains("gwstagblock") ) {
  101. LOG("markuptags found: "+text);
  102. element.classList.add("gwstagblock");
  103. }
  104. } else if ( element.classList.contains("gwstagblock") ) {
  105. LOG("removing tag from: "+text);
  106. element.classList.remove("gwstagblock");
  107. }
  108. }
  109.  
  110. function LOG(...args) {
  111. if (!debug) return
  112. console.log('[Notion UserScript]', ...args)
  113. }
  114.  
  115.  

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址