MediaWiki:OCR.js: Difference between revisions

no edit summary
No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 1: Line 1:
/*
    @Author [[User:Jayprakash12345]]
    @OwnBy [[meta:Indic-TechCom]]
*/
var IndicOCR = function () {
var IndicOCR = function () {


$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
    $( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
         'section': 'main',
         'section': 'main',
         'group': 'insert',
         'group': 'insert',
Line 16: Line 12:
                     type: 'callback',
                     type: 'callback',
                     execute: function () {
                     execute: function () {
                    var toolUrl = "//indic-ocr.toolforge.org/getOCR";
                        var toolUrl = "//localhost:3000/api/ocr"; // Local API endpoint
                    lang = mw.config.get( 'wgContentLanguage' );
                        var lang = mw.config.get( 'wgContentLanguage' );
                    loadingGifUrl = '//upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif';
                        var loadingGifUrl = '//upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif';
                   
                       
                    showLoadingMsg( 'OCR is in process.' );
                        showLoadingMsg( 'OCR is in process.' );
                    if ( $( '.prp-page-image img' ).length === 0 ) {
                        if ( $( '.prp-page-image img' ).length === 0 ) {
mw.notify( 'Proofread Image not found.' );
                            mw.notify( 'Proofread Image not found.' );
}
                            return;
var imageUrl = $( '.prp-page-image img' ).attr('src');
                        }
var requestUrl;
                        var fileInput = document.getElementById('fileInput'); // Assuming file input has an id 'fileInput'
                        var file = fileInput.files[0];
if( imageUrl.indexOf("wiki.ekatrafoundation.org") === -1){
                        if (!file) {
requestUrl = toolUrl + "?imageurl=https://wiki.ekatrafoundation.org" + imageUrl + "&langcode="+lang+ "&api=True";
                            mw.notify('Please upload an image file.');
} else {
                            return;
requestUrl = toolUrl + "?imageurl=" + imageUrl + "&langcode="+lang+ "&api=True";
                        }
}
                       
                        var formData = new FormData();
$.getJSON( requestUrl )
                        formData.append('image', file);
.done( processOcrResult )
                        formData.append('lang', lang);
.fail( processOcrResult ) // Same handler, for simplicity.
                       
.always( function () { showLoadingMsg( '' ); } );
                        $.ajax({
                            url: toolUrl,
                            type: 'POST',
                            data: formData,
                            processData: false,
                            contentType: false,
                            success: processOcrResult,
                            error: function(jqXHR, textStatus, errorThrown) {
                                mw.notify('Error from the OCR tool: ' + errorThrown);
                                showLoadingMsg('');
                            },
                            complete: function() {
                                showLoadingMsg('');
                            }
                        });
                     }
                     }
                 }
                 }
Line 44: Line 54:
      
      


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


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


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


// Add the new message if required.
        // Add the new message if required.
if ( msgLabel.length !== 0 ) {
        if ( msgLabel.length !== 0 ) {
msgBox = $( "<p>" )
            msgBox = $( "<p>" )
.attr( "id", loadingId )
                .attr( "id", loadingId )
.css( "background-color", "#efefef" ).css( "border", "1px solid #ccc" )
                .css( "background-color", "#efefef" ).css( "border", "1px solid #ccc" )
.text( msgLabel );
                .text( msgLabel );
loadingGif = $( "<img>" )
            loadingGif = $( "<img>" )
.attr( "src", loadingGifUrl )
                .attr( "src", loadingGifUrl )
.attr( "alt", "Animated loading indicator" )
                .attr( "alt", "Animated loading indicator" )
.css( "display", "inline-block" ).css( "margin", "0.3em" );
                .css( "display", "inline-block" ).css( "margin", "0.3em" );
msgBox.prepend( loadingGif );
            msgBox.prepend( loadingGif );
$( '#wpTextbox1' ).before( msgBox );
            $( '#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 ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
if ( mw.config.get( 'wgCanonicalNamespace' ) === 'Page' ) {
        if ( mw.config.get( 'wgCanonicalNamespace' ) === 'Page' ) {
$.when( mw.loader.using( 'ext.wikiEditor' ), $.ready)
            $.when( mw.loader.using( 'ext.wikiEditor' ), $.ready)
        .then( IndicOCR );
            .then( IndicOCR );
}
        }
}
}