User:Chlod/Scripts/WordCount.js

// This entire script was shamelessly copied from [[User:GoldenRing/wordcount.js]].
// This function shamelessly copied from https://stackoverflow.com/a/11348383/274460
(function () {
	$.fn.ignore = function(sel) {
		return this.clone().find(sel || "> *").remove().end();
	};
	
	function countWords(h, s) {
		$(h).each(function(i, match) {
			var elements = $(match).nextUntil(s || h);
			
			// Ignore hidden elements
			elements.find(':hidden').addClass('wordcount-ignore');
			// Ignore striked comments
			elements.find('*').filter(function() { return $(this).css('text-decoration').match(/line-through/); }).addClass('wordcount-ignore');
			// Ignore site components
			elements.find('div#siteSub').addClass('wordcount-ignore');
			elements.find('div#contentSub').addClass('wordcount-ignore');
			elements.find('div#jump-to-nav').addClass('wordcount-ignore');
			// Find elements that aren't ignored
			elements = elements.not('.wordcount-ignore');
			// Get the text
			var text = elements.ignore('.localcomments').ignore('.wordcount-ignore').text();
			// Remove all timestamps
			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 words by whitespace (will keep hyphenations as one word)
				.split(/\s+/)
				// Filter by strings worth more than one non-whitespace symbol
				.filter(function(d) { return d.length > 0; })
				// Filter by letters
	            .filter(function(d) {
	            	try {
	            		// Try unicode matching (if browser supports)
	            		const letterRegex = new RegExp("\\p{Letter}", "u");
	            		return d.match(letterRegex); 
	            	} catch (e) {
	            		// Fall back to normalization and letter filtering
						if (d.normalize != null) {
							// Normalize words that are worth one Unicode codepoint into two
							d = d.normalize("NFD");
						}
							
						// Match all letter characters (won't pick up on Kana or accented Latin letters)
						return d.match(/\w/);
	            	}
	            });
				// Filter by letters (ES5-compatible)
			var count = text.length;
			$(match).append($('<span class="wordcount mw-editsection">' + count.toLocaleString() + ' words</span>'));
		});
	}
	
	function run() {
		$(".wordcount").remove();
		countWords('.mw-parser-output > h1', 'h1');
		countWords('.mw-parser-output > h2', 'h1, h2');
		countWords('.mw-parser-output > h3', 'h1, h2, h3');
		countWords('.mw-parser-output > .mw-heading1', '.mw-heading1');
		countWords('.mw-parser-output > .mw-heading2', '.mw-heading1, .mw-heading2');
		countWords('.mw-parser-output > .mw-heading3', '.mw-heading1, .mw-heading2, .mw-heading3');
	}
	
	mw.hook("wikipage.content").add(() => {
		// Immediately run on start for WP:ARC
		if (mw.config.get("wgPageName").indexOf("Wikipedia:Arbitration/Requests/Case") === 0) {
			run();
		}
	});
	
	mw.util.addPortletLink(
	    "p-cactions",
	    "javascript:void(0)",
	    "Word count",
	    "ca-wordcount"
	).addEventListener("click", run);
})();

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.