Josh

Building in the open

Word Count Bookmarklet

Inspired by See Your Word Count While You Write from dreeves, I whipped up my own word count bookmarklet.

My version:

  • displays the word count on the textarea when you focus it
  • updates the count as you type

Tip

Drag this bookmarklet to your bookmarks bar:
Word Counter

How to use:

  1. Drag the link above to your bookmarks bar
  2. Click it on any webpage
  3. Focus on any textarea to see a live word count

Bookmarklet for word count, in action
Bookmarklet for word count, in action

Full code:

document.addEventListener('focusin', (e) => {
  if (e.target.tagName === 'TEXTAREA') {
    const textarea = e.target;
    
    if (textarea.dataset.hasCounter) return;
    textarea.dataset.hasCounter = 'true';
    
    const originalPosition = textarea.style.position;
    
    if (!originalPosition || originalPosition === 'static') {
      textarea.style.position = 'relative';
    }
    
    const counter = document.createElement('div');
    counter.style.cssText = `
      position: absolute;
      bottom: 4px;
      right: 8px;
      font-size: 11px;
      color: #666;
      pointer-events: none;
      background: rgba(255, 255, 255, 0.9);
      padding: 2px 6px;
      border-radius: 3px;
      font-family: monospace;
      z-index: 1;
    `;
    
    textarea.parentElement.insertBefore(counter, textarea.nextSibling);
    
    let rafId = null;
    const updateCount = () => {
      const text = textarea.value.trim();
      const words = text ? text.split(/\s+/).length : 0;
      counter.textContent = `${words} word${words !== 1 ? 's' : ''}`;
    };
    
    textarea.addEventListener('input', () => {
      if (rafId) cancelAnimationFrame(rafId);
      rafId = requestAnimationFrame(updateCount);
    });
    
    updateCount();
    
    textarea.addEventListener('blur', () => {
      setTimeout(() => {
        if (document.activeElement !== textarea) {
          counter.remove();
          if (!originalPosition || originalPosition === 'static') {
            textarea.style.position = '';
          }
          delete textarea.dataset.hasCounter;
        }
      }, 100);
    });
  }
});

Keyboard Shortcuts

Key Action
o Source
e Edit
i Insight
r Random
h Home
s or / Search
Josh Beckman: https://www.joshbeckman.org/blog/practicing/word-count-bookmarklet