// ==UserScript== // @name Extract Special Characters // @namespace http://github.com/kba/ // @include https://github.com/UB-Mannheim/ocr-gt-tools/wiki/Special-Characters // @description Extract special character data from ocr-gt-tools wiki // @version 1 // @require https://code.jquery.com/jquery-2.2.3.min.js // @require https://cdnjs.cloudflare.com/ajax/libs/z-schema/3.17.0/ZSchema-browser.js // @grant GM_addStyle // @downloadURL none // ==/UserScript== /*globals GM_addStyle */ /*globals ZSchema */ var SCHEMA = { 'type': 'object', "additionalProperties": false, 'properties': { 'sample': { 'type': 'array', 'items': { 'type': 'string', 'pattern': '^
 
!! INVALID
`); function escapeHTML(str) { var entityMap = { "&": "&", "<": "<", ">": ">", '"': '"', "'": ''', "/": '/' }; return String(str).replace(/[&<>"'\/]/g, function(s) { return entityMap[s]; }); } function showError(glyphId, err) { $(`h2:contains('${glyphId}')`).append( `
${escapeHTML(JSON.stringify(err, null, 2))}
`); $("#glyph-invalid").show().append( `[${ $("#glyph-invalid a").length + 1}]`); } function clearProposals() { $('#glyph-propose').empty(); } function showProposals($input, from, to) { clearProposals(); var $propose = $('#glyph-propose'); var val = $input.val(); var chosen = val.substring(from, to); console.log(chosen, from, to); $.each(window.glyphJson, function() { var glyphDesc = this; if (glyphDesc.baseLetter.indexOf(chosen) === -1) { return; } $.each(glyphDesc.sample, function(i, sample) { $propose.append($(sample) .on('click', function(e) { e.preventDefault(); $input.val(val.substr(0, from) + glyphDesc.recognition + val.substr(to)); })); }); }); } $(function() { window.glyphJson = window.glyphJson || scrapeSpecialGlyphs(); $("#glyph-input").on('keyup', function(e) { var $input = $("#glyph-input"); var from = $input[0].selectionStart; var to = $input[0].selectionEnd; if (from == to) { from -= 1; } showProposals($input, from, to); }); $("#glyph-schema").on('click', function() { window.prompt("Ctrl-C to copy schema", JSON.stringify(SCHEMA, null, 2)); }); $("#glyph-json").on('click', function() { window.prompt("Ctrl-C to copy JSON data", JSON.stringify(SCHEMA, null, 2)); }); });