Misplaced Pages

User:GoldenRing/wordcount.js: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:GoldenRing Browse history interactively← Previous editNext edit →Content deleted Content added
Revision as of 09:34, 24 June 2019 view sourceGoldenRing (talk | contribs)Extended confirmed users6,924 edits Ignore top-level matches with .wordcount-ignore← Previous edit Revision as of 16:03, 26 September 2020 view source MusikAnimal (talk | contribs)Edit filter managers, Autopatrolled, Interface administrators, Administrators120,542 edits rm {{pp-template}} as page is not template-protected; user CSS/JS is automatically protected so only yourself or interface admins can edit it, anywayNext edit →
Line 1: Line 1:
// {{pp-template}}
// This function shamelessly copied from https://stackoverflow.com/a/11348383/274460 // This function shamelessly copied from https://stackoverflow.com/a/11348383/274460
$.fn.ignore = function(sel){ $.fn.ignore = function(sel){

Revision as of 16:03, 26 September 2020

// This function shamelessly copied from https://stackoverflow.com/a/11348383/274460
$.fn.ignore = function(sel){
  return this.clone().find(sel||">*").remove().end();
};

function countWords(h) {
 $(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');
  var text = elements.ignore('span.localcomments').ignore('.wordcount-ignore').text();
  var search = /(\d{1,2}):(\d{2}), (\d{1,2}) (+) (\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(//); });
  var count = text.length;
  $(match).append($('<span class="mw-editsection">' + count.toLocaleString() + ' words</span>'));
 });
}

$(document).ready(function() {
    $('#p-cactions').children('ul.menu').append($('<li><a id="ca-wordcount" title="Add word counts to each section " accesskey="c">Word Count</a></li>'));
    $('#ca-wordcount').click(function() {
	countWords('h1');
	countWords('h2');
	countWords('h3');
    });
});