Notion Copy Properties

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

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

  1. // ==UserScript==
  2. // @name Notion Copy Properties
  3. // @namespace https://www.notion.so/
  4. // @version 0.8
  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(`.copyable-property: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.querySelector('.notion-page-view-discussion').parentNode.querySelector('div > div[style="margin: 0px;"]').querySelectorAll('.notion-focusable');
  36. blocks.forEach(function(block) {
  37. block.addEventListener('click', onClick);
  38. block.classList.add('copyable-property');
  39. });
  40. }
  41.  
  42. function onClick(e) {
  43. const elem = e.currentTarget;
  44. console.log("Copying text: " + elem.innerText);
  45. if (e.shiftKey) {
  46. copyTextToClipboard(elem.innerText);
  47. const color = elem.style.color;
  48. elem.style.color = "red";
  49. setTimeout(function() { elem.style.color = color; }, 300);
  50. }
  51. }
  52.  
  53. function fallbackCopyTextToClipboard(text) {
  54. var textArea = document.createElement("textarea");
  55. textArea.value = text;
  56.  
  57. // Avoid scrolling to bottom
  58. textArea.style.top = "0";
  59. textArea.style.left = "0";
  60. textArea.style.position = "fixed";
  61.  
  62. document.body.appendChild(textArea);
  63. textArea.focus();
  64. textArea.select();
  65.  
  66. try {
  67. var successful = document.execCommand('copy');
  68. var msg = successful ? 'successful' : 'unsuccessful';
  69. console.log('Fallback: Copying text command was ' + msg);
  70. } catch (err) {
  71. console.error('Fallback: Oops, unable to copy', err);
  72. }
  73.  
  74. document.body.removeChild(textArea);
  75. }
  76. function copyTextToClipboard(text) {
  77. if (!navigator.clipboard) {
  78. fallbackCopyTextToClipboard(text);
  79. return;
  80. }
  81. navigator.clipboard.writeText(text).then(function() {
  82. console.log('Async: Copying to clipboard was successful!');
  83. }, function(err) {
  84. console.error('Async: Could not copy text: ', err);
  85. });
  86. }