Skip to content

Instantly share code, notes, and snippets.

@brandonhellman
Last active September 16, 2019 07:21
Show Gist options
  • Save brandonhellman/375eaa4d37d86370b7ae27d9453fbcb6 to your computer and use it in GitHub Desktop.
Save brandonhellman/375eaa4d37d86370b7ae27d9453fbcb6 to your computer and use it in GitHub Desktop.
Counts words and characters in all textareas to show them on hover.
// ==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