Notion Copy Properties

Copy Notion Database item property values by clicking Shift + LeftMouseButton on it

当前为 2021-10-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Notion Copy Properties
  3. // @namespace https://www.notion.so/
  4. // @version 0.6
  5. // @description Copy Notion Database item property values by clicking Shift + LeftMouseButton on it
  6. // @author Maxim Kurbatov
  7. // @match https://www.notion.so/*
  8. // @grant GM_addStyle
  9. // @run-at document-end
  10. // ==/UserScript==
  11.  
  12. var debug = false;
  13. GM_addStyle(`#notion-app .notion-frame > div.notion-scroller.vertical.horizontal > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div > div:nth-child(1) > div > div:nth-child(1) > div > div > div:hover { font-style: italic; }`);
  14.  
  15. // create an observer instance
  16. var observer = new MutationObserver(function(mutations) {
  17. mutations.forEach(function(mutation) {
  18. var target = false;
  19. if ( mutation.target.nodeName == "DIV" ) target = mutation.target;
  20. else target = mutation.target.parentElement;
  21. if ( target.getAttribute("contentEditable") == "true" ) target = target.parentElement;
  22. if ( ! target ) return;
  23. addCopyHandler(target);
  24. });
  25. });
  26. var config = { characterData: true, attributes: false, childList: true, subtree: true };
  27. var mutgt = document.body;
  28. observer.observe(mutgt, config);
  29. var lastBlock = false;
  30.  
  31. function addCopyHandler(elm) {
  32. elm = elm || document;
  33. //if ( typeof elm.querySelectorAll !== 'function' ) return;
  34. //var blocks = elm.querySelectorAll("#notion-app .notion-focusable");
  35. const blocks = document.querySelectorAll(`#notion-app .notion-frame > div.notion-scroller.vertical.horizontal > div:nth-child(2) > div:nth-child(1) > div:nth-child(2) > div > div:nth-child(1) > div > div:nth-child(1) > div > div > div`);
  36. blocks.forEach(function(block) {
  37. block.addEventListener('click', onClick);
  38. });
  39. }
  40.  
  41. function onClick(e) {
  42. const elem = e.currentTarget;
  43. console.log("Copying text: " + elem.innerText);
  44. if (e.shiftKey) {
  45. copyTextToClipboard(elem.innerText);
  46. const color = elem.style.color;
  47. elem.style.color = "red";
  48. setTimeout(function() { elem.style.color = color; }, 300);
  49. }
  50. }
  51.  
  52. function fallbackCopyTextToClipboard(text) {
  53. var textArea = document.createElement("textarea");
  54. textArea.value = text;
  55.  
  56. // Avoid scrolling to bottom
  57. textArea.style.top = "0";
  58. textArea.style.left = "0";
  59. textArea.style.position = "fixed";
  60.  
  61. document.body.appendChild(textArea);
  62. textArea.focus();
  63. textArea.select();
  64.  
  65. try {
  66. var successful = document.execCommand('copy');
  67. var msg = successful ? 'successful' : 'unsuccessful';
  68. console.log('Fallback: Copying text command was ' + msg);
  69. } catch (err) {
  70. console.error('Fallback: Oops, unable to copy', err);
  71. }
  72.  
  73. document.body.removeChild(textArea);
  74. }
  75. function copyTextToClipboard(text) {
  76. if (!navigator.clipboard) {
  77. fallbackCopyTextToClipboard(text);
  78. return;
  79. }
  80. navigator.clipboard.writeText(text).then(function() {
  81. console.log('Async: Copying to clipboard was successful!');
  82. }, function(err) {
  83. console.error('Async: Could not copy text: ', err);
  84. });
  85. }
  86.  
  87.