User:NKohli (WMF)/imagery.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
$( document ).ready( function() {
	if ( mw.config.get( 'wgCanonicalNamespace' ) == 'File' ) {
		$( '.wbmi-statements-header' )
			.append( '<div id="label-detect">Suggest tags</div>' )
			.click( function(){
				$.ajax({
					url: "https://tools.wmflabs.org/imagery/api.php",
					jsonpCallback: 'displayResult',
					data: { 
						image: $('.fullImageLink').find('img').attr('src')
					},
					dataType: 'jsonp',
					success: function( response ) { 
						res = JSON.parse( response );
						$( '#label-detect' ).css({'max-width': 400, 'font-size':18, 'cursor':'none', 'text-align':'left', 'color':'black', 'background-color':'white' } );
						$( '#label-detect' ).text( '' );
						$( '#label-detect' ).append( '<b>Features found in the image:</b> <br/>' );
						$( '.wbmi-entityview-statementsGroup' ).append( '<div class="suggested-tags">Suggested tags for this image:<br/>');
						$.each( res, function( label, props ) { 
							console.log ( label + props[0] + props[1] );
							$( '.wbmi-entityview-statementsGroup' ).append( '<input type=\"checkbox\">' + label + "  " + props[0] + "  " + props[1] + '<br/>');
						} );
						$( '.wbmi-entityview-statementsGroup' ).append( '</div>' );
					},
					fail: function( response ) {
						console.log( response );
					}
				}); 
			});
		$( '#label-detect' ).css({ 'margin':6, 'font-weight':500, 'color':'blue' } );
	}
});