User:Cryptic/transwikifrom.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

//<pre>
var transwiki_log_location = "Wikibooks:Transwiki log";

function add_link(url, name)
{
  //replace_editing_help(url, name);    // remove the "//" at the start of this line to replace the "editing help" under the edit window with the links
  //before_editbox(url, name);          // remove the "//" at the start of this line to place the links before the editing box

  //the following options are skin-specific.
  //monobook_tab(url, name);            // remove the "//" at the start of this line to place the links in tabs in the monobook skin
  //monobook_toolbox(url, name);        // remove the "//" at the start of this line to place the links in the toolbox in the monobook skin
  //classic_header(url, name);          // remove the "//" at the start of this line to place the links in the header in the classic skin
  //classic_quickbar(url, name);        // remove the "//" at the start of this line to place the links in the quickbar in the classic skin
  //nostalgia_header(url, name);        // remove the "//" at the start of this line to place the links in the header in the nostalgia skin
  //cologneblue_quickbar(url, name);    // remove the "//" at the start of this line to place the links in the quickbar in the cologne blue skin
}

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

  var form = document.editform;
  var x;
  for (x = form.firstChild; x; x = x.nextSibling)
    if (x.tagName == 'BR')
      {
        form.insertBefore(document.createTextNode(' | '), x);
        form.insertBefore(na, x);
        break;
      }
    else if (x.tagName == 'TEXTAREA')
      {
        form.insertBefore(na, x);
        form.insertBefore(document.createElement('br'), x);
        break;
      }
}

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

  var form = document.editform;
  var em = form.getElementsByTagName('em');
  if (em.length > 1)
    form.replaceChild(na, em[1]);       // we haven't yet removed the "editing help" link, so replace it
  else
    {
      // either there's a br and a list of templates, or the editpage-copywarn div
      var x;
      for (x = em[0]; x && x.tagName != "BR" && x.tagName != "DIV"; x = x.nextSibling);
      if (!x)
        { // didn't find anything! so just append
          form.appendChild(document.createTextNode(' | '));
          form.appendChild(na);
        }
      else
        {
          form.insertBefore(document.createTextNode(' | '), x);
          form.insertBefore(na, x);
        }
    }
}

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

  var li = document.createElement('li');
  li.appendChild(na);
  document.getElementById('p-cactions').getElementsByTagName('ul')[0].appendChild(li);
}

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

  var li = document.createElement('li');
  li.appendChild(na);
  document.getElementById('p-tb').getElementsByTagName('ul')[0].appendChild(li);
}

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

  var td = document.getElementById('topbar').getElementsByTagName('table')[0].getElementsByTagName('td')[1].getElementsByTagName('p')[0];

  if (td.hasChildNodes())
    td.appendChild(document.createTextNode(' | '));
  td.appendChild(na);
}

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

  document.getElementById('quickbar').appendChild(na);
}

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

  var bar = document.getElementById('topbar');
  var br = bar.getElementsByTagName('br')[1];
  bar.insertBefore(document.createTextNode(' | '), br);
  bar.insertBefore(na, br);
}

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

  var qb = document.getElementById("quickbar");
  qb.appendChild(na);
  qb.appendChild(document.createElement('br'));
}

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

// End of generic code

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)/))
    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)/))
    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 onload_transwiki()
{
  if (document.title.indexOf("Editing ") == 0)
    {
      if (document.title.indexOf("Editing Talk:Transwiki:") == 0)
        add_link('javascript:transwiki_history()', 'Wikify History');
      else if (document.title.indexOf("Editing Transwiki:") == 0)
        add_link('javascript:transwiki_summary()', 'Transwiki summary');
      else if (document.title.indexOf("Editing " + transwiki_log_location) == 0)
        add_link('javascript:transwiki_log(prompt("Title?"))', 'Log entry');
    }
  //add_link("/wiki/" + transwiki_log_location, 'Transwiki log');       // remove the "//" from the start of this line to add a link to the transwiki log.  You may want to use a specific add_link function instead, e.g. monobook_toolbox, so it doesn't appear in the same place as the editting links.
}

addOnloadHook(onload_transwiki);
//</pre>