← All examples

JavaScript Productivity bbc.com

BBC: article word count

Appends the article word count next to the title so you immediately know how long the text is.

Code to copy

// Show a word count of the article next to the headline
(function () {
  if (document.getElementById('jz-bbc-wordcount')) { return; }
  var blocks = document.querySelectorAll('[data-component="text-block"]');
  if (!blocks.length) { return; }
  var words = 0;
  blocks.forEach(function (b) {
    words += (b.textContent || '').trim().split(/\s+/).length;
  });
  var h1 = document.querySelector('article h1') || document.querySelector('h1');
  if (!h1) { return; }
  var tag = document.createElement('span');
  tag.id = 'jz-bbc-wordcount';
  tag.textContent = ' (' + words + ' words)';
  tag.style.cssText = 'font-size:0.8em;color:#888;font-weight:400';
  h1.appendChild(tag);
})();

How to use this example

  1. Copy the code with the button above.
  2. Install JustZix (2 minutes) and open the extension on the target page.
  3. Add a new rule matching that page.
  4. Paste the code into the rule's JavaScript panel and save — it runs on every page visit.

Rate this example

No ratings yet — be the first.

Does this example work?

Snippets are useless without somewhere to paste them.

JustZix takes 2 minutes to install and runs your code on every matching page. No account, no payment.

Download free See use cases