MediaWiki:OCR.js: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
Line 1: | Line 1: | ||
var IndicOCR = function () { | var IndicOCR = function () { | ||
$( '#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 = "//localhost:3000/api/ocr"; // Local API endpoint | |||
var lang = mw.config.get( 'wgContentLanguage' ); | |||
var 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.' ); | |||
return; | |||
} | |||
var fileInput = document.getElementById('fileInput'); // Assuming file input has an id 'fileInput' | |||
var file = fileInput.files[0]; | |||
if (!file) { | |||
mw.notify('Please upload an image file.'); | |||
return; | |||
} | |||
var formData = new FormData(); | |||
formData.append('image', file); | |||
formData.append('lang', lang); | |||
$.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 ) { | |||
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 ); | |||
} | |||
} | |||
}; | }; | ||
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) { | if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) { | ||
if ( mw.config.get( 'wgCanonicalNamespace' ) === 'Page' ) { | |||
$.when( mw.loader.using( 'ext.wikiEditor' ), $.ready) | |||
.then( IndicOCR ); | |||
} | |||
} | } |
Revision as of 19:10, 1 May 2024
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 = "//localhost:3000/api/ocr"; // Local API endpoint
var lang = mw.config.get( 'wgContentLanguage' );
var 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.' );
return;
}
var fileInput = document.getElementById('fileInput'); // Assuming file input has an id 'fileInput'
var file = fileInput.files[0];
if (!file) {
mw.notify('Please upload an image file.');
return;
}
var formData = new FormData();
formData.append('image', file);
formData.append('lang', lang);
$.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('');
}
});
}
}
}
}
} );
function processOcrResult( response ) {
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 );
}
}
};
if ( $.inArray( mw.config.get( 'wgAction' ), [ 'edit', 'submit' ] ) !== -1 ) {
if ( mw.config.get( 'wgCanonicalNamespace' ) === 'Page' ) {
$.when( mw.loader.using( 'ext.wikiEditor' ), $.ready)
.then( IndicOCR );
}
}