Usor:Hsarrazin/tl.js

E Wikisource

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.
( function ( mw, $ ) {
'use strict';

function hasGadget(name, modules) {
	return $.inArray('ext.gadget.' + name, modules) != -1;
}

function getTextLayer(djvuname, page_nr) {
	$.ajax({
		url: mw.util.wikiScript( 'api' ),
		dataType: 'json',
		data: {
			'format': 'json',
			'action': 'query',
			'titles': 'File:' + djvuname,
			'prop': 'imageinfo',
			'iiprop': 'metadata',
			'indexpageids': '1'
		},
		success : function (res) {
			var text = null;
			try {
				var page_id = res.query.pageids[0];
				var data = res.query.pages[page_id].imageinfo[0].metadata[0].value;
				var $xml = $.parseXML(data);
				text = $($xml).find('DjVuTxt BODY PAGE').eq(page_nr - 1).attr('value');
			}
			catch(err) {
				console.log("unable to load text layer, possible cause are 1) the djvu file doesn't contain a text layer, 2) djvu file contains a text layer but server side is buggy, try to purge the djvu file");
			}
			if (text) {
				$('#wpTextbox1').val(text);
			}
		}
	});
}

if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1 && mw.config.get('wgCanonicalNamespace') == 'Page') {
  mw.loader.using(['ext.wikiEditor', 'ext.proofreadpage.page.edit'], function() {
  	$(function() {
      $('#wpTextbox1').wikiEditor('addToToolbar', {
        section: 'main',
		group: 'insert',
        tools: {
          'textLayer': {
            label: 'Remplace le texte de la Page: actuel par la couche texte',
            type: 'button',
            icon: '//upload.wikimedia.org/wikisource/fr/1/1d/TL.png',
            action: {
              type: 'callback',
              execute: function() {
                var datas = mw.config.get('wgTitle').split('/');
                getTextLayer(datas[0], datas[1]);
              }
            }
          }
        }
      });
  	});
  });
}
 
}( mediaWiki, jQuery ) );