User:Ruakh/keypad.js

/* todo: -find better "icon" -refactor/cleanup

var html;

// Returns a new XMLHttpRequest object, except in IE, where it returns the // equivalent. function newXMLHttpRequest { var reqObj = false; /*@cc_on @*/ /*@if (@_jscript_version >= 5) try { reqObj = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { reqObj = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { reqObj = false; }  } @else reqObj = false; @end @*/ if (!reqObj) { try { reqObj = new XMLHttpRequest; } catch (e) { reqObj = false; } } return reqObj; }

function getAppendCharFunc(c) { return function { document.getElementById('searchInput').value += c;   return false; }; }

// Given, in string form, one of the "p"-s that makes up the edittools, returns // an table object containing all the character-adding links. (N.B.: currently // expects its input in a rather idiosyncratic form. TODO change that) function p2Table ( txt ) { var q='"'; var sq="'";  tbl = document.createElement('table');  tbl.id = 'searchKeyboard';  var row = document.createElement('tr');  txt = txt.replace(/]*class=["'][^"']*\bcharinsert\b[^"']*["'][^>]*>/g, ' ');  kybs = txt.split(' ');  var onthisline=0;  for (i=1; i -1 ) {        tbl.appendChild(row);        row = document.createElement('tr');        onthisline=0;      }      if ( chars[j].length > 1 || chars[j] == ' ' || chars[j] == 'p' || chars[j] == '>' || chars[j] == "'" || chars[j] == '"' ) continue; onthisline++; var cell = document.createElement('td'); cell.bgcolor = 'gray'; cell.align = 'center'; var link = document.createElement('a'); link.href = '#' + chars[j]; link.onclick = getAppendCharFunc(chars[j]); link.appendChild(document.createTextNode(chars[j])); cell.appendChild(link); row.appendChild(cell); needBr=true; if (onthisline > 7) { tbl.appendChild(row); row = document.createElement('tr'); onthisline=0; }   }  }  tbl.appendChild(row); return tbl; }

function keyPadSelection( keyBoardNumber ) { var tbl = document.getElementById('searchKeyboard'); var kW = tbl.parentNode; kW.removeChild(tbl); var txt = html.split('<p ')[keyBoardNumber+2]; var tbl = p2Table(txt); kW.insertBefore(tbl, kW.firstChild); return true; }

function keyPad { var q='"';   var sq="'";    var selected = parseInt( getCookie('edittoolscharsubset') );    if ( isNaN(selected) ) selected = 1;    if (selected < 1) selected = 1; //skip zero, as that is the "templates" thing.    var sF = document.getElementById("searchform");        var searchBox = document.getElementById('searchBody');        var kW = document.createElement('div');        var tbl = document.createElement('table');        tbl.bgcolor = 'black';        tbl.id = 'searchKeyboard';        var row = document.createElement('tr');        var cell = document.createElement('td');        var link = document.createElement('a');        var func;        link.appendChild(document.createTextNode('testing'));        cell.appendChild(link);        cell.bgcolor = 'gray';        cell.align = 'center';

var req = newXMLHttpRequest; req.open('GET', 'http://en.wiktionary.org/w/index.php?title=MediaWiki:Edittools&action=raw', true) ; req.send(null); req.onreadystatechange = function { if (req.readyState==4) { html = req.responseText; //TODO: get all of them, extract name, add combobox, all loop thing that makes them visible=flase/visible=true. var menus = html.split('editpage-specialchars')[1].split(' -1) lin = lin.split('"')[1]; if (lin.indexOf('edittools-') == 0) lin = lin.split('edittools-')[1]; if (lin == '') { lin = 'Unnamed ' + j; }; menu.options[menu.options.length] = new Option(lin); }    menu.options[selected-1].selected = true; menu.onchange = function { keyPadSelection(this.selectedIndex); }; tbl.appendChild(row); kW.appendChild(tbl); searchBox.appendChild(kW); kW.appendChild(menu); }  }

return; }

function addKeyPadTimer { setTimeout( 'keyPad', 200 ); //wait 1/5th of one second. }

$( addKeyPadTimer );