Jump to content

User:Novem Linguae/Scripts/VoteCounter.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Novem Linguae (talk | contribs) at 04:18, 21 July 2021 (create). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(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.
// <nowiki>

/*
- 
*/

mw.loader.using('mediawiki.storage').then(function () {
	mw.storage.session.set( 'client-error-opt-out', '1' );
});

$(function() {
	function getWikicode(title) {
		// https://www.mediawiki.org/wiki/API:Get_the_contents_of_a_page#Method_1:_Use_the_Revisions_API
		// https://en.wikipedia.org/w/api.php?action=parse&page=Pet_door&prop=wikitext&formatversion=2
		var wikicode = '';
		title = encodeURIComponent(title);
		$.ajax({
			url: 'https://en.wikipedia.org/w/api.php?action=parse&page='+title+'&prop=wikitext&formatversion=2&format=json',
			success: function (result) {
				wikicode = result['parse']['wikitext'];
			},
			dataType: "json",
			async: false
		});
		return wikicode;
	}

	/** returns the pagename, including the namespace name, but with spaces replaced by underscores */
	function getArticleName() {
		return mw.config.get('wgPageName');
	}
	
	// don't run when not viewing articles
	let action = mw.config.get('wgAction');
	if ( action != 'view' ) return;
	
	// only run in talk namespaces (all of them) or Wikipedia namespace
	let namespace = mw.config.get('wgNamespaceNumber');
	if ( ! [4, 1, 3, 5, 7, 9, 11, 13, 15, 101, 119, 711, 829, 2301, 2303].includes(namespace) ) {
		return;
	}
	
	// get wikitext
	let title = getArticleName();
	let wikicode = getWikicode(title);
	console.log(wikicode);	

	// count the following
		// - keep
		// - delete
		// - support
		// - oppose
	// caveats:
		// - don't count strikethroughs
		// - don't get thrown off by weak/strong
	// display totals on page using JQuery
	
	// will need to eventually do it by section instead of by page. these pages have it by section:
		// - RFC
		// - TFD
		// - CFD
		// - RFD
		// - RFC
	// these pages have it by page:
		// - RFA
		// - AFD
		// - MFD
		
	// need to count the initial vote as a keep/delete. check type of page for this
});

// </nowiki>