phpmyadmin index highlighter

Highlights index fields, adds a link to the related table to foreign keys

  1. // ==UserScript==
  2. // @name phpmyadmin index highlighter
  3. // @author ameboide
  4. // @namespace http://userscripts.org/scripts/show/117633
  5. // @description Highlights index fields, adds a link to the related table to foreign keys
  6. // @include */phpmyadmin/tbl_properties_structure.php*
  7. // @include */phpmyadmin/tbl_structure.php*
  8. // @include */phpmyadmin/db_structure.php*
  9. // @version 0.0.1.20140705013815
  10. // ==/UserScript==
  11.  
  12. //campos['nombre_del_campo'] = td
  13. var listaCampos = document.querySelectorAll('#tablestructure tbody th');
  14. var campos = {};
  15. for(var i=0; i<listaCampos.length; i++){
  16. var campo = listaCampos[i];
  17. var txt = campo.textContent.trim();
  18. campos[txt] = campo;
  19.  
  20. //id sin indice -> rojo claro / codigo sin indice -> rojo oscuro
  21. if(txt.match(/^id_|_id$/i)) campo.style.color = '#f00';
  22. else if(txt.match(/^codigo_|_codigo$/i)) campo.style.color = '#800';
  23. }
  24.  
  25. //idx -> azul oscuro
  26. var idxs = document.querySelectorAll('#table_indexes tbody tr:not(.tblFooters) td:last-of-type');
  27. for(i=0; i<idxs.length; i++){
  28. var campo = campos[idxs[i].textContent.trim()];
  29. if(campo) campo.style.color = '#008';
  30. }
  31.  
  32. var urlBase = document.querySelector('[href^="tbl_relation.php"]').href;
  33.  
  34. var matches = document.location.href.match(/\/(\w+\.php).*token=(\w+)/);
  35. var url = matches[1];
  36. var token = matches[2];
  37.  
  38. //FK -> link azul claro
  39. function rellenar(claves){
  40. for(var campo in claves){
  41. if(!campos[campo]) continue;
  42.  
  43. var s = claves[campo].split('.');
  44. var db = s[0];
  45. var tabla = s[1];
  46. var campo_fk = s[2];
  47.  
  48. var a = document.createElement('a');
  49. a.href = url+'?db='+db+'&token='+token+'&table='+tabla;
  50. a.title = tabla+' . '+campo_fk;
  51. a.id = 'link_fk_' + campo;
  52. a.innerHTML = ' -&gt;';
  53.  
  54. var link_viejo = document.getElementById(a.id);
  55. if(link_viejo) campos[campo].removeChild(link_viejo);
  56.  
  57. campos[campo].appendChild(a);
  58. campos[campo].style.color = '#00f';
  59. }
  60. }
  61.  
  62. //si ya habia visitado esta tabla, tengo las claves foraneas guardadas
  63. var codigo_cache = document.location.host + ' : ' +
  64. urlBase.match(/(\?|&)db=(\w+)/)[2] + ' . ' +
  65. urlBase.match(/(\?|&)table=(\w+)/)[2];
  66.  
  67. var cache = GM_getValue(codigo_cache, null);
  68. if(cache) rellenar(JSON.parse(cache));
  69.  
  70. //leer la pag de las claves foraneas (si estaba cacheado, leo igual para actualizar)
  71. var xhr = new XMLHttpRequest();
  72. xhr.onreadystatechange = function(){
  73. if (xhr.readyState == 4 && xhr.status == 200){
  74. try{
  75. var div = document.createElement('div');
  76. div.innerHTML = xhr.responseText;
  77.  
  78. var claves = {};
  79.  
  80. var opts = div.querySelectorAll('select[name^="destination"] option[selected]');
  81. for(var i=0; i<opts.length; i++){
  82. var opt = opts[i];
  83. var val = opt.value;
  84.  
  85. while(opt.nodeName != 'TR') opt = opt.parentNode;
  86. var campo = opt.firstElementChild.textContent.trim();
  87.  
  88. claves[campo] = val;
  89. }
  90.  
  91. var claves_str = JSON.stringify(claves);
  92. //si no es lo mismo q tenia cacheado, vuelvo a rellenar con la info nueva
  93. if(claves_str != cache){
  94. //workaround para error raro
  95. setTimeout(function() {
  96. GM_setValue(codigo_cache, claves_str);
  97. }, 0);
  98. rellenar(claves);
  99. }
  100. }
  101. catch(e){}
  102. }
  103. }
  104. xhr.open('GET', urlBase, true);
  105. xhr.send(null);