Jump to content

User:Bob/monobook.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Bob (talk | contribs) at 00:18, 10 April 2008 (testing this script on IE). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
addOnloadHook(function(){
if(wgNamespaceNumber > 0 && wgNamespaceNumber % 2 != 0) { // if we're on a talk page
 
  /* do you want the templates to be collapsed on page load? */
  var autocollapse = true;
 
  var pageEl = next(document.getElementById('contentSub'));
 
  if (isYellowBox(pageEl)) { // if there's a yellow box at the beginning
 
    var tableIndex = 'YellowBoxes';
    var collapseCaption = "hide";
    var expandCaption = "show";
 
    var table = document.createElement('table');
      table.className = 'messagebox standard-talk collapsible collapsed';
      table.style.background = '#F8FCFF';
      table.style.border = '1px solid #AAAAAA';
      table.setAttribute( "id", "collapsibleTable" + tableIndex );
    var headtr = table.insertRow(0);
    var header = document.createElement("th");
      header.appendChild( document.createTextNode( "Talk page templates" ) );
      header.style.textAlign = "center";
    var bodytr = table.insertRow(1);
    var td = bodytr.insertCell(0);
    headtr.appendChild(header);
 
    var Button     = document.createElement( "span" );
    var ButtonLink = document.createElement( "a" );
    var ButtonText = document.createTextNode( collapseCaption );
 
    Button.style.styleFloat = "right";
    Button.style.cssFloat = "right";
    Button.style.fontWeight = "normal";
    Button.style.textAlign = "right";
    Button.style.width = "6em";
 
    ButtonLink.style.color = header.style.color;
    ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
    ButtonLink.setAttribute( "href", "javascript:collapseTable('" + tableIndex + "');" );
    ButtonLink.appendChild( ButtonText );
 
    Button.appendChild( document.createTextNode( "[" ) );
    Button.appendChild( ButtonLink );
    Button.appendChild( document.createTextNode( "]" ) );
 
    header.insertBefore( Button, header.childNodes[0] );
 
    document.getElementById('bodyContent').insertBefore(table,pageEl); // insert our table
    var templates = new Array();
    while(isYellowBox(pageEl)) { // keep moving through the page until we run out of yellow boxes
      templates.push(pageEl);
      pageEl = next(pageEl);
    }
 
    for (i = 0; i < templates.length; i++) {
      templates[i] = wrapInMessageBox(templates[i],td); // put the templates in our new table
    }
 
    if(autocollapse)
      collapseTable('YellowBoxes'); // collapse the table
  }
}
 
});
 
function isYellowBox(el) {
  if(el.className.search(/messagebox|standard-talk/i) != -1)
    return true;
  else
    return false;
}
 
function wrapInMessageBox(el,wrapper) {
  var newEl = el.cloneNode(true);
  wrapper.appendChild(newEl);
  el.parentNode.removeChild(el);
  return newEl;
}
 
function next(el) {
    el = el.nextSibling;
    while (el.nodeType != 1) { // fix for Firefox
      el = el.nextSibling;
    }
    return el;
}