User:Iceblock/prefixindexonecolumn.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
 * This script adds a tab to Special:Prefixindex and Special:Allpages
 * to reformat the list into one column. It also adds a tab to create
 * a numbered list.
 *
 * Made by [[User:Iceblock]]
 */

function reorganizePrefixIndexTable() {
  var listTable = document.getElementById('mw-prefixindex-list-table');
  if (!listTable)
    var listTable = document.getElementsByClassName('mw-allpages-table-chunk')[0];
  if (!listTable)
    return;
  var tableRows = listTable.children[0].children;
  var len = tableRows.length;
  for (x = 0; x < len; x++) {
    var tableRows = listTable.children[0].children;
    var len2 = tableRows[0].children.length;
    for (y = 0; y < len2; y++) {
       var trNode = document.createElement('tr');
       var tdNode = document.createElement('td');
       tdNode.innerHTML = tableRows[0].children[y].innerHTML;
       trNode.appendChild(tdNode);
       listTable.children[0].appendChild(trNode);
    }
    listTable.children[0].removeChild(tableRows[0]);
  }  
}
function makeNumberedPrefixIndexTable() {
  var listTable = document.getElementById('mw-prefixindex-list-table');
  if (!listTable)
    var listTable = document.getElementsByClassName('mw-allpages-table-chunk')[0];
  if (!listTable)
    return;
  var tableRows = listTable.children[0].children;
  var len = tableRows.length;
  if (/\<li\>/i.test(tableRows[0].innerHTML))
    return;
  var trNode = document.createElement('tr');
  var tdNode = document.createElement('td');
  var olNode = document.createElement('ol');
  tdNode.appendChild(olNode);
  trNode.appendChild(tdNode);
  listTable.children[0].appendChild(trNode);

  for (x = 0; x < len; x++) {
    var tableRows = listTable.children[0].children;
    var len2 = tableRows[0].children.length;
    for (y = 0; y < len2; y++) {
       var liNode = document.createElement('li');
       liNode.innerHTML = tableRows[0].children[y].innerHTML;
       olNode.appendChild(liNode);
    }
    listTable.children[0].removeChild(tableRows[0]);
  }  
}

$ ( function () {
  if (wgCanonicalSpecialPageName == 'Prefixindex' || wgCanonicalSpecialPageName == 'Allpages') {
    mw.util.addPortletLink('p-cactions', 'javascript:reorganizePrefixIndexTable();', '1 column', null, 'Change the results list into one column');
    mw.util.addPortletLink('p-cactions', 'javascript:makeNumberedPrefixIndexTable();', 'Numbered list', null, 'Change the results list into a numbered list');
  }
} );