MediaWiki:OCR.js

From Ekatra Wiki
Revision as of 19:05, 1 May 2024 by Gurwinder (talk | contribs)
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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*
    @Author [[User:Jayprakash12345]]
    @OwnBy [[meta:Indic-TechCom]]
*/
var IndicOCR = function () {

	$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
        'section': 'main',
        'group': 'insert',
        'tools': {
            'IndicOCR': {
                label: 'IndicOCR', // or use labelMsg for a localized label, see above
                type: 'button',
                icon: '//upload.wikimedia.org/wikipedia/commons/thumb/8/8e/Iconocr.png/22px-Iconocr.png',
                action: {
                    type: 'callback',
                    execute: function () {
                    	var toolUrl = "//indic-ocr.toolforge.org/getOCR";
                    		lang = mw.config.get( 'wgContentLanguage' );
                    		loadingGifUrl = '//upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif';
                    		
                    	showLoadingMsg( 'OCR is in process.' );
                    	if ( $( '.prp-page-image img' ).length === 0 ) {
							mw.notify( 'Proofread Image not found.' );
						}
						var imageUrl = $( '.prp-page-image img' ).attr('src');
						var requestUrl;
						
						if( imageUrl.indexOf("wiki.ekatrafoundation.org") === -1){
							requestUrl = toolUrl + "?imageurl=https://wiki.ekatrafoundation.org" + imageUrl + "&langcode="+lang+ "&api=True";
						} else {
							requestUrl = toolUrl + "?imageurl=" + imageUrl + "&langcode="+lang+ "&api=True";
						}
						
						$.getJSON( requestUrl )
							.done( processOcrResult )
							.fail( processOcrResult ) // Same handler, for simplicity.
							.always( function () { showLoadingMsg( '' ); } );
                    }
                }
            }
        }
    } );
    

	function processOcrResult( response ) {
		if ( response.responseJSON !== undefined && response.responseJSON.error ) {
			mw.notify( mw.message( 'error' ) + ' ' + response.responseJSON.error.code + ' ' + response.responseJSON.error.message );
			return;
		}
		if ( response.text === undefined || response.text.length === 0 ) {
			mw.notify( "No OCR text." );
			return;
		}
		$( '#wpTextbox1' ).val( response.text );
	}
	

	function showLoadingMsg( msgLabel ) {
		var msg, loadingGif,
			loadingId = 'GoogleOcrLoading';

		// Always remove any existing message.
		$( '#' + loadingId ).remove();

		// Add the new message if required.
		if ( msgLabel.length !== 0 ) {
			msgBox = $( "<p>" )
				.attr( "id", loadingId )
				.css( "background-color", "#efefef" ).css( "border", "1px solid #ccc" )
				.text( msgLabel );
			loadingGif = $( "<img>" )
				.attr( "src", loadingGifUrl )
				.attr( "alt", "Animated loading indicator" )
				.css( "display", "inline-block" ).css( "margin", "0.3em" );
			msgBox.prepend( loadingGif );
			$( '#wpTextbox1' ).before( msgBox );
		}
	}
};

/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
		if ( mw.config.get( 'wgCanonicalNamespace' ) === 'Page' ) {
			$.when( mw.loader.using( 'ext.wikiEditor' ), $.ready)
        	 .then( IndicOCR );
		}
}