Last active
September 16, 2019 07:21
-
-
Save brandonhellman/375eaa4d37d86370b7ae27d9453fbcb6 to your computer and use it in GitHub Desktop.
Counts words and characters in all textareas to show them on hover.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Word Counter | |
// @namespace https://github.com/Kadauchi | |
// @version 1.0.2 | |
// @description Counts words and characters in all textareas to show them on hover. | |
// @author Kadauchi | |
// @icon http://i.imgur.com/oGRQwPN.png | |
// @include * | |
// ==/UserScript== | |
setInterval(main, 250); | |
function getAllTextareas() { | |
return document.querySelectorAll(`textarea`); | |
} | |
function getWordCount(text) { | |
const possibleWords = text.split(/\s+/); | |
const hasCharacters = possibleWords.filter((word) => word.length); | |
return hasCharacters.length; | |
} | |
function setCountAsTitle(textarea) { | |
const text = textarea.value; | |
const wordCount = getWordCount(text); | |
textarea.title = `${wordCount} words ${text.length} characters` | |
} | |
function main() { | |
const textareas = getAllTextareas(); | |
textareas.forEach(setCountAsTitle); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment