notion-word-count

Get word count of Notion document

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name            notion-word-count
// @namespace       https://greasyfork.org/scripts/413663-notion-word-count
// @author          starrybouquet
// @match           https://www.notion.so/*
// @version         0.0.1
// @description     Get word count of Notion document
// ==/UserScript==

window.onload = function (){
    // add button
    var moreButton = document.getElementsByClassName('notion-topbar-more-button')[0];
    var countButton = document.createElement('button');
    countButton.textContent = 'Word Count';
    countButton.id = 'wordcount-userscript-button'
    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);');
    countButton.onclick = getWordCount;
    moreButton.before(countButton);

    // get current wordcount on click (NOT automatically updated)
    function getWordCount(e) {
        var input = document.getElementsByClassName('notion-page-content')[0];
        // regex gratefully based on https://stackoverflow.com/questions/38102141/how-to-make-a-word-count-that-contains-html-tags-using-javascript
        // console.log(input.innerText);
        cont = input.innerText.replace(/<[^>]*>/g," ");
        cont = cont.replace(/\s+/g, ' ');
        cont = cont.replace('—', ' ');
        cont = cont.trim();
        var words = cont.split(" ").length;
        countButton.textContent = 'Word Count: ' + String(words);
    }
}