Utilisateur:Phe/OCR.js
Apparence
Note : après avoir enregistré vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
- Firefox / Safari : Maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ou Ctrl-R (⌘-R sur un Mac) ;
- Google Chrome : Appuyez sur Ctrl-Maj-R (⌘-Shift-R sur un Mac) ;
- Internet Explorer : Maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5 ;
- Opera : Allez dans Menu → Settings (Opera → Préférences sur un Mac) et ensuite à Confidentialité & sécurité → Effacer les données d'exploration → Images et fichiers en cache.
/*jshint boss:true*/
/*global $, mw*/
/*
* Query an ocr for a given Page:, first try to get the hocr text layer as it's available
* for most book, fast and of a better quality. If it fails, try the older and slower
* ocr method. hocr fail around 1/5000 books. ocr should never fails as it use the image
* visible on the Page:.
*/
var lang = mw.config.get( 'wgContentLanguage' );
function disable_input(set)
{
if (set) {
$(document).keyup(function(e) {
if (e.which == 27) { disable_input(false); }
});
}
set ? $('#wsOcr1').off('click') : $('#wsOcr1').on('click', do_hocr);
set ? $('#wsOcr2').off('click') : $('#wsOcr1').on('click', fraktur_ocr);
$('#wpTextbox1').prop('disabled', set);
}
function ocr_callback(data) {
if (data.error) {
alert(data.text);
} else {
// Checking if tb is disabled is required with chrome as ESC doesn't kill
// the query.
var tb = document.getElementById("wpTextbox1");
if (tb.disabled)
tb.value = data.text;
}
disable_input(false);
}
function hocr_callback(data) {
if (data.error) {
// Fallback to the slow way.
disable_input(false);
do_ocr();
return;
} else {
// Checking if tb is disabled is required with chrome as ESC doesn't kill
// the query.
var tb = document.getElementById("wpTextbox1");
if (tb.disabled) {
var text = $(data.text).text();
text = text.replace(/[ ]*\n[ ]*/g, '\n');
text = text.replace(/\n\n\n\n/g, '@_@_@_@_@_@');
text = text.replace(/\n\n/g, '\n');
text = text.replace(/@_@_@_@_@_@/g, '\n\n');
tb.value = $.trim(text);
}
}
disable_input(false);
}
function do_hocr() {
disable_input(true);
var request_url = '//tools.wmflabs.org/phetools/hocr_cgi.py?cmd=hocr&book='
+ encodeURIComponent(mw.config.get('wgTitle')) + '&lang=' + lang + '&user=' + mw.config.get('wgUserName');
$.getJSON(request_url).done(hocr_callback);
}
function do_ocr() {
if ($( '.prp-page-image img' ).length) {
disable_input(true);
// server side can't use protocol relative url, request it as https:
var url_image = 'https:' + $( '.prp-page-image img' ).attr('src');
var request_url = "//tools.wmflabs.org/phetools/ocr.php?cmd=ocr&url="+url_image+"&lang="+lang+"&user="+mw.config.get('wgUserName');
$.getJSON( request_url ).done( ocr_callback );
}
}
function addOCRButton2(id,comment,source,onclick){
var image = document.createElement("img");
image.width = 46;
image.id = id;
image.height = 22;
image.border = 0;
image.className = "mw-toolbar-editbutton";
image.style.cursor = "pointer";
image.alt = "OCR";
image.title = comment;
image.src = source;
image.onclick = onclick;
var tb = document.getElementById("toolbar");
if(tb) {
tb.appendChild(image);
} else {
if (id === 'wsOcr1')
image.src = '//upload.wikimedia.org/wikipedia/commons/c/c9/Toolbaricon_OCR.png';
//append the button only after the toolbar is ready
mw.loader.using('ext.wikiEditor', function () {
var new_tb = document.getElementById("wikiEditor-ui-toolbar");
if(new_tb) {
new_tb.childNodes[0].childNodes[1].appendChild(image);
}
});
}
}
function fraktur_ocr()
{
lang = 'de-f';
// For fraktur we need to use the slow way, all hocr for 'de'
// are done with non-fraktur.
do_ocr();
lang = mw.config.get( 'wgContentLanguage' );
}
function addOCRButton()
{
if(mw.config.get( 'wgCanonicalNamespace' ) !== 'Page' || $.inArray(mw.config.get('wgAction'), ['edit', 'submit']) === -1)
return;
if(mw.config.get('wgContentLanguage') == "de"){
addOCRButton2("wsOcr1","Normale OCR","//upload.wikimedia.org/wikipedia/commons/e/e0/Button_ocr.png", do_hocr);
addOCRButton2("wsOcr2","Fraktur OCR","//upload.wikimedia.org/wikipedia/commons/a/af/Button_Fractur_OCR.png", fraktur_ocr);
} else {
addOCRButton2("wsOcr1","Get the text by OCR","//upload.wikimedia.org/wikipedia/commons/e/e0/Button_ocr.png", do_hocr);
}
}
if (!self.proofreadpage_disable_ocr) {
$(addOCRButton);
}