MediaWiki:OCR.js: Difference between revisions

no edit summary
No edit summary
Tag: Reverted
No edit summary
Tags: Undo 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 12: Line 16:
                     type: 'callback',
                     type: 'callback',
                     execute: function () {
                     execute: function () {
                        var toolUrl = "//localhost:3000/api/ocr"; // Local API endpoint
                    var toolUrl = "//indic-ocr.toolforge.org/getOCR";
                        var lang = mw.config.get( 'wgContentLanguage' );
                    lang = mw.config.get( 'wgContentLanguage' );
                        var loadingGifUrl = '//upload.wikimedia.org/wikipedia/commons/4/42/Loading.gif';
                    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 fileInput = document.getElementById('fileInput'); // Assuming file input has an id 'fileInput'
var requestUrl;
                        var file = fileInput.files[0];
                        if (!file) {
if( imageUrl.indexOf("wiki.ekatrafoundation.org") === -1){
                            mw.notify('Please upload an image file.');
requestUrl = toolUrl + "?imageurl=https://wiki.ekatrafoundation.org" + imageUrl + "&langcode="+lang+ "&api=True";
                            return;
} else {
                        }
requestUrl = toolUrl + "?imageurl=" + imageUrl + "&langcode="+lang+ "&api=True";
                       
}
                        var formData = new FormData();
                        formData.append('image', file);
$.getJSON( requestUrl )
                        formData.append('lang', lang);
.done( processOcrResult )
                       
.fail( processOcrResult ) // Same handler, for simplicity.
                        $.ajax({
.always( function () { showLoadingMsg( '' ); } );
                            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 54: Line 44:
      
      


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