Jump to content

User:Novem Linguae/Scripts/DontForgetG12.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Novem Linguae (talk | contribs) at 11:18, 27 July 2021 (don't focus on new tab). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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>

/*
- Check if article is unreviewed
- If so, display a giant "copyright check" button at the top, to remind you to run Earwig's copyvio detector on the article first thing.
- Many submissions are copyright violations, and catching it before you perform a bunch of other steps in the NPP/AFC flowchart saves time.
*/

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

$(function() {
	/** returns the pagename, including the namespace name, but with spaces replaced by underscores */
	function getArticleName() {
		return mw.config.get('wgPageName');
	}
	
	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;
	}
	
	function pageIsCuratedFunction(title) {
		let pageIsCurated = '';
		mw.hook( 'ext.pageTriage.toolbar.ready' ).add( function ( queue ) {
			pageIsCurated = queue.reviewed();
		});
		return pageIsCurated;
	}
	
	// don't run when not viewing articles
	let action = mw.config.get('wgAction');
	if ( action != 'view' ) return;
	
	// don't run when viewing diffs
	let isDiff = mw.config.get('wgDiffNewId');
	if ( isDiff ) return;
	
	// Only run in mainspace and draftspace
	let namespace = mw.config.get('wgNamespaceNumber');
	if ( ! [0, 118].includes(namespace) ) return;
	
	let title = getArticleName();
	let wikicode = getWikicode(title);
	
	// Only run if 1) article is uncurated or 2) draft is submitted
	let draftIsSubmitted = wikicode.match(/(?:{{AfC submission}}|{{AfC submission\|}}|{{AfC submission\|\|)/);
	// let pageIsNotCurated = $('.mwe-pt-mark-as-unreviewed-button').length;
	// let pageIsCurated = pageIsCuratedFunction();
	// console.log(pageIsCurated);
	if ( draftIsSubmitted ) {
		$('#contentSub').before(`
			<script>
				function doCopyvioCheck() {
					// open window in new tab, without focusing on it
					let handle = window.open('https://tools.wmflabs.org/copyvios/?lang=en&project=wikipedia&title=` + encodeURIComponent(title) + `');
					handle.blur();
					window.focus();
				}
			</script>
			<button style="padding: 0.25em 3em; background-color: #FFDC00; font-size: 1.5em;" onclick="doCopyvioCheck()" onauxclick="doCopyvioCheck()">Copyvio check</button>
		`);
	}
});

// </nowiki>