User:Cryptic/standard.js

From Wikibooks, open books for an open world
Jump to navigation Jump to search
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
  • Konqueror: simply click the Reload button, or press F5;
  • Opera users may need to completely clear their cache in Tools→Preferences.
//<pre>
function addlink(url, name)
{
  var na = document.createElement('a');
  na.setAttribute('href', '/wiki/' + url);

  var txt = document.createTextNode(name);
  na.appendChild(txt);
  return na;
}

function addexplicitlink(url, name)
{
  var na = document.createElement('a');
  na.setAttribute('href', url);

  var txt = document.createTextNode(name);
  na.appendChild(txt);
  return na;
}

function pipe(td)
{
  td.appendChild(document.createTextNode(' | '));
}

function replace()
{
  var s = prompt("Search regexp?");
  if (s)
    {
      var r = prompt("Replace regexp?");
      var txt = document.editform.wpTextbox1;
      txt.value = txt.value.replace(new RegExp(s, "g"), r);
    }
}

function t(txt)
{
  return '{{' + txt + '}}';
}

function sig()
{
  return ' ~~' + '~~';
}

function template(tm)
{
  document.editform.wpTextbox1.value = t(tm) + '\n' + document.editform.wpTextbox1.value;
  document.editform.wpSummary.value = t(tm);
}

function user_talk_template(tm, sum)
{
  if (document.editform.wpTextbox1.value.length > 0)
    document.editform.wpTextbox1.value += '\n';
  document.editform.wpTextbox1.value =  document.editform.wpTextbox1.value + t(tm) + sig() + '\n';
  document.editform.wpSummary.value = sum;
}

function transwiki_history()
{
  var txt = document.editform.wpTextbox1;

  // Replace double braces, add a newline at end
  txt.value = txt.value.replace(/{/g, "&#123;") + "\n";
  txt.value = txt.value.replace(/}/g, "&#125;");

  // Find minor edits, and make them regexp-friendly
  txt.value = txt.value.replace(/ m \(/g, " (MINOR");
  txt.value = txt.value.replace(/ m$/gm, " (MINOR)");
  // Find edits without summaries, and make them regexp-friendly
  txt.value = txt.value.replace(new RegExp("([^)])\\r", "gm"), "$1 ()\r");
  // Replace the "(cur) (last)  " at the start of each line with "*" and link user names
  txt.value = txt.value.replace(new RegExp("^\\(cur\\) \\(last\\)  ([0-9][0-9]:[0-9][0-9], [0-9][0-9]? [A-Z][a-z]* 200[0-9]) ([^(\\n\\r]*) (\\(.*\\))$", "gm"), "*$1 [[w:User:$2|$2]] $3");
  // Fix minor edits
  txt.value = txt.value.replace(/ \(MINOR/g, " '''m''' (");
  // Fix edits without summaries
  txt.value = txt.value.replace(/ \(\)$/gm, "");
  txt.value = txt.value.replace(new RegExp("^([^(\\n\\r]*) \\((.*)\\)$", "gm"), "$1 ''($2)''");

  // Add header
  var title;
  if (!document.title.match(/Talk:Transwiki:(.*) - (Preview|Edit this page)/))
    title = prompt("Couldn't find article name.");
  else
    title = RegExp.$1;

  txt.value = "This article was [[m:transwiki|transwiki]]ed from [[w:" + title + "]]." + sig() + "\n\nThe history of the article there was:\n" + txt.value;

  // And summary
  document.editform.wpSummary.value = "[[m:transwiki|transwiki]]ed from [[w:" + title + "]]";
}

function transwiki_summary()
{
  var txt = document.editform.wpTextbox1;

  var title;
  if (!document.title.match(/Editing Transwiki:(.*) - (Preview|Edit this page)/))
    title = prompt("Couldn't find article name.");
  else
    title = RegExp.$1;

  document.editform.wpSummary.value = "[[m:transwiki|transwiki]]ed from [[w:" + title + "]]";
}

function transwiki_log(title)
{
  var txt = document.editform.wpTextbox1;
  txt.value += "*[[w:" + title + "]] &rarr; [[Transwiki:" + title + "]]" + sig();
  document.editform.wpSummary.value = "[[Transwiki:" + title + "]] transwikied from [[w:" + title + "]]";
}

function interwiki(lk, td, a, host, abbrev)
{
  host = lk.replace(/source/, host);
  td.insertBefore(addexplicitlink(host, abbrev), a);
  if (abbrev == "D" || abbrev == "d")
    {
      td.insertBefore(document.createTextNode('/'), a);
      td.insertBefore(addexplicitlink(host.toLowerCase(), "lc"), a);
    }
  td.insertBefore(document.createTextNode(' | '), a);
}

function morelinks()
{
  var table = document.getElementById('topbar').getElementsByTagName('table')[0];
  var tds = table.getElementsByTagName('td');
  var td = tds[1];
  var lks = td.getElementsByTagName('a');
  var a;
  var txt;
  var lk;

  // Add comment link to top, and replace Main Page with Transwiki log
  for (a = 0; a < lks.length; ++a)
    {
      txt = lks[a].childNodes[0].data;
      if (txt == "Main Page")
        {
          lks[a].childNodes[0].data = "Transwiki Log";
          lks[a].setAttribute('href', "/w/index.php?title=Wikibooks:Transwiki_log");
        }
      else if (txt == "Edit this page")
        {
          txt = lks[a].getAttribute('href');
          a = lks[a + 1];
          td.insertBefore(addexplicitlink(txt + "&section=new", "Comment"), a);
          td.insertBefore(document.createTextNode(' | '), a);
          td.insertBefore(addexplicitlink(txt + "&section=0", "0"), a);
          td.insertBefore(document.createTextNode(' | '), a);
          break;
        }
    }

  if (document.title.indexOf("User:Cryptic/standard") >= 0)
    {
      a = td.getElementsByTagName('p')[0];
      td.insertBefore(document.createTextNode(' | '), a);
      td.insertBefore(addexplicitlink("/w/index.php?title=User:Cryptic/standard.js&action=raw&ctype=text/javascript", "js"), a);
      td.insertBefore(document.createTextNode(' | '), a);
      td.insertBefore(addexplicitlink("/w/index.php?title=User:Cryptic/standard.css&action=raw&ctype=text/css", "css"), a);
    }

  // Remove second line, preserving "Current revision" (on the first line) and
  // "You have new messages" (replacing (Talk) in upper right) if present
  td = td.getElementsByTagName('p')[0];
  while (td.hasChildNodes())
    {
      lk = td.firstChild;
      if (lk.nodeName == 'A' && lk.firstChild.data == 'Current revision')
        {
          a = tds[1].getElementsByTagName('p')[0];
          tds[1].insertBefore(document.createTextNode(' | '), a);
          tds[1].insertBefore(lk, a);
        }
      else if (lk.nodeName == 'STRONG' && lk.firstChild.data == 'You have ')
        {
          lks = tds[2].getElementsByTagName('a');
          for (a = 0; a < lks.length; ++a)
            if (lks[a].firstChild.data == 'Talk')
              {
                lks[a].firstChild.data = 'New Messages';
                lks[a].setAttribute('style', 'font-style: italic;');
                lks[a].setAttribute('href', '/w/index.php?title=User_talk:Cryptic&action=history');
              }
        }
      td.removeChild(td.firstChild);
    }

  // Replace them
  td.appendChild(addlink('Special:Watchlist', 'Watchlist'));
  pipe(td);
  td.appendChild(addlink('Special:Randompage', 'Random'));
  pipe(td);
  td.appendChild(addexplicitlink('/w/index.php?title=Special:Contributions&hideminor=0&namespace=&target=Cryptic&limit=500&offset=0', 'Contribs'));
  pipe(td);
  td.appendChild(addlink('Special:Specialpages', 'Special'));

  if (document.title.indexOf("Editing ") == 0)
    {
      pipe(td);
      td.appendChild(addexplicitlink('javascript:replace()', 'Replace'));

      if (document.title.indexOf("Editing Talk:Transwiki:") == 0)
        {
          pipe(td);
          td.appendChild(addexplicitlink('javascript:transwiki_history()', 'Fixup Transwiki history'));
        }
      else if (document.title.indexOf("Editing Transwiki:") == 0)
        {
          pipe(td);
          td.appendChild(addexplicitlink('javascript:transwiki_summary()', 'Transwiki summary'));
        }
      else if (document.title.indexOf("Editing Wikibooks:Transwiki log") == 0)
        {
          pipe(td);
          td.appendChild(addexplicitlink('javascript:transwiki_log(prompt("Title?"))', 'Log entry'));
        }
    }

  // Interwiki links
  td = tds[2];
  a = td.firstChild;
  for (lk = lks.length - 1; lk >= 0 && lks[lk].firstChild.data != 'What links here'; --lk);
  if (lk >= 0)
    {
      lk = lks[lk].getAttribute('href');
      lk = lk.replace(/en.wikibooks/, "source");
      lk = lk.replace(/Special:Whatlinkshere&target=/, "");
      interwiki(td, a, "en.wikipedia", "w");
      interwiki(td, a, "en.wiktionary", "d");
      interwiki(td, a, "en.wikinews", "n");
      interwiki(td, a, "en.wikibooks", "B");
      interwiki(td, a, "wikisource", "s");
      interwiki(td, a, "commons.wikimedia", "c");
      interwiki(td, a, "meta.wikimedia", "m");
      interwiki(td, a, "en.wikiquote", "q");
    }
}

function do_onload()
{
  morelinks();
}

if (window.addEventListener) 
  window.addEventListener("load", do_onload, false);
else if (window.attachEvent) 
  window.attachEvent("onload", do_onload);



// Comparison tables extension
// (C) GreyCat, 2006
// License: GPLv2
//
// Note: changes to this file may affect many users;
//       please discuss on the talk page before editing.

function switchColumn(el, col) {
var tbl = el.parentNode.parentNode.nextSibling;
var state = el.checked ? '' : 'none';
for (var i = 0; i < tbl.rows.length; i++) {
var row = tbl.rows[i];
row.cells[col].style.display = state;
}
}

function processTable(tbl) {
var headrow = tbl.rows[0];
var selector = '';
for (var i = 0; i < headrow.cells.length; i++) {
var el = headrow.cells[i];
selector += '<div><input type="checkbox" checked="1" onclick="javascript:switchColumn(this, ' + i + ');" /> ' + el.textContent + '</div>';
}
var selectorDiv = document.createElement('DIV');
selectorDiv.innerHTML = selector;
tbl.parentNode.insertBefore(selectorDiv, tbl);
}

function setupComparisonTables() {
var notice = document.getElementById('comparison_notice');
if (notice) {
notice.style.display = 'none';
var tables = document.getElementsByTagName('TABLE');
for (var i = 0; i < tables.length; i++) {
if (tables[i].className == 'wikitable') {
processTable(tables[i]);
}
}
}
}

if (window.addEventListener) 
  window.addEventListener("load", setupComparisonTables, false);
else if (window.attachEvent) 
  window.attachEvent("onload", setupComparisonTables);
//</pre>