User:Guergana Tzatchkova (WMDE)/MismatchFinderWidget.js

From Wikidata
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
$(document).ready( function () {
	if ( $('body').hasClass('wb-itempage') ){
		
	// import css 
	importStylesheet( 'User:Guergana_Tzatchkova_(WMDE)/MismatchFinderWidget.css' );

	const currentItem = mw.config.get('wbEntityId');
	
	const MMFiconURL = 'https://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Mismatch_Finder_Widget_Icon.svg/20px-Mismatch_Finder_Widget_Icon.svg.png';
	const linkIconURL = 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Mismatch_finder_link_icon_SVG.svg/20px-Mismatch_finder_link_icon_SVG.svg.png';
	
	$.ajax({
		url: `https://mismatch-finder.toolforge.org/api/mismatches?ids=${currentItem}`
	})
	.then(function( data ) {
		if(data.length > 0){
			// Create Mismatch Finder Gadget Box
			const box = $('<div></div>').addClass('mmf-message-box');
			
			// Create left side content of Mismatch Finder box
			const leftContentContainer = $('<div></div>').append([
				$('<img />').attr('src', MMFiconURL),
				$(`<span>There are ${data.length} mismatches on this item</span>`)
				]);
				
			const linkToMMF = $('<a></a>').attr(
				{
					'href' : `https://mismatch-finder.toolforge.org/results?ids=${currentItem}`, 
					'target': '_blank'				
				});
			const linkImage = $('<img />').attr('src', linkIconURL );
			linkToMMF.append( linkImage );
			linkToMMF.append( '<span>Inspect</span>' );
			
			// Add elements to Mismatch Finder Gadget Box
			box.append(leftContentContainer);
			box.append(linkToMMF);
			
			// Add box to Wikidata page
			$('#bodyContent').prepend(box);
		}
		})
	.catch(function() {
		console.log( 'Mismatch Finder API request fail' );
	});
}
});