User:DannyS712/copyvio-check.js

Source: Wikipedia, the free encyclopedia.
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.
// Based on [[User:FR30799386/copyvio-check.js]]
$(document).ready(function() {
	if (
		mw.config.get('wgNamespaceNumber') !== 0
		&& mw.config.get('wgNamespaceNumber') !== 2
	) {
		return;
	}
	function runCheck() {
		var checkUrl = '//copyvios.toolforge.org/api.json?version=1&action=search&project=wikipedia&lang=en&title=' + encodeURIComponent(mw.config.get('wgTitle'));
		$.ajax(checkUrl).then(function(result) {
			if ((Math.round(result.best.confidence * 100)) < 50) {
				$('#copyvio-check-report').css('background', '#EFE');
			} else if ((Math.round(result.best.confidence * 100)) > 50) {
				$('#copyvio-check-report').css('background', '#FEE');
			}
			$('#copyvio-check-report').text('').append(
				document.createTextNode('Around ' + Math.round(result.best.confidence * 100).toString() + '% chance of being a copyvio ('),
				$('<a>')
					.attr('id', 'copyvio-report-link')
					.attr('target', '_blank')
					.attr('href', '//copyvios.toolforge.org/?lang=en&project=wikipedia&title=' +
						encodeURIComponent(mw.config.get('wgTitle')) + '&oldid=&action=search&use_engine=1&use_links=1&turnitin=0&noredirect=true')
					.text('view details'),
				document.createTextNode(')')
			);
		});
	}
	var counter = 0;
	setInterval(function() {
		if ($('.mwe-pt-toolbar-big').length && !counter) {
			counter++;
			var $copyvioDisplay = $( '<div>' )
				.attr( 'id', 'copyvio-check-report' )
				.css( 'background', '#EFE' )
				.css( 'padding', '0.5em' );
			var $copyvioCheckTrigger = $( '<a>' )
				.attr( 'id', 'copyvio-check-trigger' )
				.text( 'Check for copyvio' );
			if( $('.redirectMsg').length ) {
				$copyvioDisplay.append(
					'Redirects are not normally copyvios. '
				);
				$copyvioCheckTrigger.text( 'Check anyway' );
			}
			$copyvioDisplay.append( $copyvioCheckTrigger );
			$('#mwe-pt-info > div.mwe-pt-tool-flyout').append( $copyvioDisplay );
			$copyvioCheckTrigger.on( 'click', function ( e ) {
				e.preventDefault();
				$copyvioDisplay.css( 'background', '#e8e8e8' )
					.text( 'Calculating copyvio percentage...' );
				runCheck();
				return false;
			} );
		}
	}, 250);
});