Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save cuylerstuwe/27662c3c4ab0020972e396a007bf2755 to your computer and use it in GitHub Desktop.

Select an option

Save cuylerstuwe/27662c3c4ab0020972e396a007bf2755 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Qualtrics - Add Character Counters with Escape
// @namespace salembeats
// @version 1.5
// @description Press escape and all text fields and textareas get character counters beneath them.
// @author Cuyler Stuwe (salembeats)
// @include /.*qualtrics.*/
// @grant none
// ==/UserScript==
const KEYCODE_ESCAPE = 27;
function createCharacterCountersForAllTextFields() {
document.querySelectorAll("input[type='TEXT'],textarea").forEach( (el) => {
let newElement = document.createElement("DIV");
newElement.innerText = "Characters Entered: [], Word Estimate: []";
el.insertAdjacentElement('afterend', newElement);
el.addEventListener('keyup', () => {
let numberOfSpaces = (el.value.match(/ /g) || "").length;
newElement.innerText = `Characters Entered: [${el.value.length}], Word Estimate: [${numberOfSpaces}]`;
});
} );
}
function handleKeypress(e) {
if(e.keyCode === KEYCODE_ESCAPE) {
createCharacterCountersForAllTextFields();
}
}
(function() {
'use strict';
document.addEventListener('keyup', handleKeypress);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment