notion-word-count

Get word count of Notion document

  1. // ==UserScript==
  2. // @name notion-word-count
  3. // @namespace https://greasyfork.org/scripts/413663-notion-word-count
  4. // @author starrybouquet
  5. // @match https://www.notion.so/*
  6. // @version 0.0.1
  7. // @description Get word count of Notion document
  8. // ==/UserScript==
  9.  
  10. window.onload = function (){
  11. // add button
  12. var moreButton = document.getElementsByClassName('notion-topbar-more-button')[0];
  13. var countButton = document.createElement('button');
  14. countButton.textContent = 'Word Count';
  15. countButton.id = 'wordcount-userscript-button'
  16. countButton.setAttribute('style', 'element {user-select: none;transition: background 20ms ease-in 0s;cursor: pointer;display: inline-flex;align-items: center;flex-shrink: 0;white-space: nowrap;height: 28px;border-radius: 3px;font-size: 14px;line-height: 1.2;min-width: 0px;padding-left: 8px;padding-right: 8px;color: rgb(55, 53, 47);');
  17. countButton.onclick = getWordCount;
  18. moreButton.before(countButton);
  19.  
  20. // get current wordcount on click (NOT automatically updated)
  21. function getWordCount(e) {
  22. var input = document.getElementsByClassName('notion-page-content')[0];
  23. // regex gratefully based on https://stackoverflow.com/questions/38102141/how-to-make-a-word-count-that-contains-html-tags-using-javascript
  24. // console.log(input.innerText);
  25. cont = input.innerText.replace(/<[^>]*>/g," ");
  26. cont = cont.replace(/\s+/g, ' ');
  27. cont = cont.replace('—', ' ');
  28. cont = cont.trim();
  29. var words = cont.split(" ").length;
  30. countButton.textContent = 'Word Count: ' + String(words);
  31. }
  32. }