User:L235/wordAndLinkCounter.js

This is the current revision of this page, as edited by L235 (talk | contribs) at 16:56, 10 December 2024 (fix). The present address (URL) is a permanent link to this version.

(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/**
 * When executed, this script presents the number of words and links in each 
 * section of the currently-open page.
 * Based on wordcount.js by GoldenRing: https://en.wikipedia.org/w/index.php?title=User:GoldenRing/wordcount.js&oldid=982615623
 * @author [[User:L235]]
 */
(function () {
  // This function shamelessly copied from https://stackoverflow.com/a/11348383/274460
  $.fn.ignore = function (sel) {
    return this.clone()
      .find(sel || ">*")
      .remove()
      .end();
  };

  function countWordsAndLinks(h) {
    $("#mw-content-text")
      .find(h)
      .each(function (i, match) {
        var elements = $(match).nextUntil(h);
        elements.find(":hidden").addClass("wordcount-ignore");
        elements
          .find("*")
          .filter(function () {
            return $(this)
              .css("text-decoration")
              .match(/line-through/);
          })
          .addClass("wordcount-ignore");
        elements.find("div#siteSub").addClass("wordcount-ignore");
        elements.find("div#contentSub").addClass("wordcount-ignore");
        elements.find("div#jump-to-nav").addClass("wordcount-ignore");
        elements = elements.not(".wordcount-ignore");
        elements = elements
          .ignore("span.localcomments")
          .ignore(".wordcount-ignore");
        var linkcount = elements.find("a").length;
        var text = elements.text();
        var search = /(\d{1,2}):(\d{2}), (\d{1,2}) ([A-Z][a-z]+) (\d{4}) \(UTC\)/g;
        text = text.replace(search, "");
        text = text
          .split(/\s/)
          .filter(function (d) {
            return d.length > 0;
          })
          .filter(function (d) {
            return d.match(/[a-zA-Z0-9]/);
          });
        var count = text.length;
        $(match).append(
          $(
            '<span class="mw-editsection">' +
              count.toLocaleString() +
              " words, " + linkcount.toLocaleString() + " links</span>"
          )
        );
      });
  }

  $(document).ready(function () {
    var link = mw.util.addPortletLink(
      "p-cactions",
      "#",
      "Word counts",
      "ca-wordcounts",
      "Add word counts to each section"
    );
    $(link).click(function () {
      countWordsAndLinks("h1");
      countWordsAndLinks(".mw-heading2");
      countWordsAndLinks(".mw-heading3");
    });
  });
})();