Misplaced Pages

User:Cryptic/cologneblue.js: Difference between revisions

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:Cryptic Browse history interactively← Previous editNext edit →Content deleted Content added
Revision as of 13:18, 8 January 2020 view sourceCryptic (talk | contribs)Administrators41,691 edits follow change to MediaWiki:Deletereason-dropdown‎← Previous edit Revision as of 13:19, 8 January 2020 view source Cryptic (talk | contribs)Administrators41,691 edits the MediaWiki:Deletereason-dropdown‎ version omits "please"Next edit →
Line 403: Line 403:
txt = txt + " – If you wish to retrieve it, please see ]"; txt = txt + " – If you wish to retrieve it, please see ]";
else if (txt.match(/^\\].+/)) else if (txt.match(/^\\].+/))
txt = txt.replace(/to retrieve it,/, "If you wish to retrieve it,"); txt = txt.replace(/to retrieve it, see/, "If you wish to retrieve it, please see");
else if (txt.match(/^\\].+/)) else if (txt.match(/^\\].+/))
txt = txt.replace(/ or \\] submission/, ''); txt = txt.replace(/ or \\] submission/, '');

Revision as of 13:19, 8 January 2020

//<nowiki>
function add_global_style(css)
{
  var head, style;
  head = document.getElementsByTagName('head');
  if (!head)
    return;
  style = document.createElement('style');
  style.type = 'text/css';
  style.innerHTML = css;
  head.appendChild(style);
}

function add_link_general(url, action, name, id, node, prev)
{
  var na = document.createElement('a');
  na.setAttribute('href', url);

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

  if (id)
    na.id = id;

  if (action)
    {
      if (typeof action == "string")
        na.setAttribute("onClick", action);
      else if (typeof action == "function")
        na.onclick = action;
    }

  if (node)
    if (prev)
      node.insertBefore(na, prev);
    else
      node.appendChild(na);
  else
    return na;
}


function addexplicitlink(url, name, node, prev)
{
  return add_link_general(url, null, name, null, node, prev);
}


function addlink_onclick(action, name, node, prev)
{
  return add_link_general('#', action, name, null, node, prev);
}


function pipe(node, txt, prev)
{
  if (node)
    if (prev)
      node.insertBefore(document.createTextNode(txt ? txt : ' | '), prev);
    else
      node.appendChild(document.createTextNode(txt ? txt : ' | '));
  else
    return document.createTextNode(' | ');
}


function makebutton(lbl, action)
{
  var button = document.createElement('input');
  button.type = 'button';
  button.value = lbl;
  button.setAttribute('onClick', action);
  return button;
}


// Add links for Logs, Current version, Enable rollback.
// Copy the more-informative label for Undelete on the (hidden) left navbar to the Undelete link on the bottom.
// If there's a template providing a default delete reason, display that in a delete link at the bottom, retaining the no-reason delete link.
// If it's a redirect-to-existent-page reason, provide the name of the target page.
// If this page is a redirect to a non-existent page, do the same.
function morelinks()
{
  var enable_rollback_txt = "var head=document.getElementsByTagName('head');"
                            + "var style=document.createElement('style');"
                            + "style.type='text/css';"
                            + "style.innerHTML='.mw-rollback-link { display:inline !important; }';"
                            + "head.appendChild(style);";

  // If there's a protect link, add link for Logs and Current version.
  // Always add a link for Enable rollback.
  var protect = document.getElementById("cb-ca-unprotect");
  if (!protect)
    protect = document.getElementById("cb-ca-protect");
  if (protect)
    {
      var parent = protect.parentNode;

      addexplicitlink("/search/?title=Special:Log&page=" + encodeURIComponent(mw.config.get('wgPageName')),
                      'Logs', parent, protect);
      pipe(parent, null, protect);

      addexplicitlink("/search/?title=" + encodeURIComponent(mw.config.get('wgPageName')),
                      'Curr', parent, protect);
      pipe(parent, null, protect);

      addlink_onclick(enable_rollback_txt, 'Enable rollback', parent, protect);
      pipe(parent, null, protect);
    }
  else
    {
      var parent = document.getElementById("searchform-footer").parentNode;

      pipe(parent);
      addlink_onclick(enable_rollback_txt, 'Enable rollback', parent);
    }


  var del = document.getElementById("cb-ca-undelete");
  if (!del)
    {
      del = document.getElementById("cb-ca-delete");
      if (del)
        {
          var delete_reason = document.getElementById("delete-reason");
          if (delete_reason)
            {
              delete_reason = delete_reason.firstChild.nodeValue;
              if (delete_reason)
                {
                  if (delete_reason.match(/Redirect.*to\+a\+deleted\+or\+non-existent\+page/))
                    {
                      delete_reason = encodeURIComponent("]: Redirect to a deleted or nonexistent page");

                      for (var node = document.getElementById('mw-content-text').firstChild.firstChild; node; node = node.nextSibling)
                        if (node.tagName == 'OL' && node.firstChild)
                          {
                            var n = node.firstChild;
                            if (n.tagName != 'LI' && n.nextSibling)
                              n = n.nextSibling;
                            if (n.tagName == 'LI')
                              {
                                delete_reason += ": ]";
                                break;
                              }
                          }
                    }
                  else if (delete_reason.match(/Cross.*namespace.+redirect.+from.+mainspace/))
                    {
                      delete_reason = encodeURIComponent("]: ] from mainspace");

                      for (var node = document.getElementById('mw-content-text').firstChild.firstChild; node; node = node.nextSibling)
                        if (node.tagName == 'OL' && node.firstChild)
                          {
                            var n = node.firstChild;
                            if (n.tagName != 'LI' && n.nextSibling)
                              n = n.nextSibling;
                            if (n.tagName == 'LI')
                              {
                                delete_reason += " to ]";
                                break;
                              }
                          }
                    }
                  else if (delete_reason.match(/^CSD.F5:/))
                    delete_reason = encodeURIComponent(']: Unused non-free media file for more than 7 days');
                  else
                    delete_reason = delete_reason.replace("+", "%20", "g");

                  addexplicitlink(del.firstChild.href + "&wpReason=" + delete_reason,
                                  decodeURIComponent(delete_reason),
                                  del.parentNode, del.nextSibling);
                  pipe(del.parentNode, ': ', del.nextSibling);
                }
            }
          else
            {
              for (var node = document.getElementById('mw-content-text').firstChild.firstChild; node; node = node.nextSibling)
                {
                  if (node.tagName == 'OL'
                      && node.firstChild
                      && node.firstChild.nextSibling
                      && node.firstChild.nextSibling.tagName == 'LI'
                      && node.firstChild.nextSibling.firstChild
                      && node.firstChild.nextSibling.firstChild.nodeName == '#text'
                      && node.firstChild.nextSibling.firstChild.nodeValue.match(/^redirect *$/i)
                      && node.firstChild.nextSibling.firstChild.nextSibling.tagName == 'A'
                      && node.firstChild.nextSibling.firstChild.nextSibling.className == 'new')
                    node = node.firstChild.nextSibling.firstChild.nextSibling.firstChild;
                  else if (node.tagName == 'DIV'
                           && node.className == 'redirectMsg'
                           && node.firstElementChild
                           && node.firstElementChild.nextElementSibling
                           && node.firstElementChild.nextElementSibling.tagName == 'UL'
                           && node.firstElementChild.nextElementSibling.firstChild
                           && node.firstElementChild.nextElementSibling.firstChild.tagName == 'LI'
                           && node.firstElementChild.nextElementSibling.firstChild.firstChild
                           && node.firstElementChild.nextElementSibling.firstChild.firstChild.tagName == 'A'
                           && node.firstElementChild.nextElementSibling.firstChild.firstChild.className == 'new')
                    node = node.firstElementChild.nextElementSibling.firstChild.firstChild.firstChild;
                  else
                    continue;

                  delete_reason = "]: Redirect to a deleted or nonexistent page: ]";

                  addexplicitlink(del.firstChild.href + "&wpReason=" + encodeURIComponent(delete_reason),
                                  delete_reason,
                                  del.parentNode, del.nextSibling);
                  pipe(del.parentNode, ': ', del.nextSibling);

                  break;
                }
            }
        }
    }
  else  // Show number of deleted revisions in undelete link at bottom
    {
      var sidenode = document.getElementById("ca-undelete");
      if (sidenode)
        del.firstChild.firstChild.nodeValue = sidenode.firstChild.firstChild.nodeValue;
    }
}


// If a template, wikilink, or external link appears in a diff, make it clickable
function link_links_in_diffs()
{
  var tds = document.getElementsByTagName('td');

  for (var i = 0; i < tds.length; ++i)
    if (tds.className == 'diff-deletedline' || tds.className == 'diff-addedline' || tds.className == 'diff-context')
      {
        var txt = tds.innerHTML.replace(new RegExp('(\\ <]+)( ])', 'gi'), '$1<a href="$2">$2</a>$3');
        txt = txt.replace(new RegExp('\\{\\{((?:talk|user|wikipedia|image|mediawiki|template|help|category|portal)(?:talk)?:+)()', 'gi'), '{{<a href="https://en.wikipedia.org/$1">$1</a>$2');
        txt = txt.replace(new RegExp('\\{\\{(+)()', 'g'), '{{<a href="https://en.wikipedia.org/Template:$1">$1</a>$2');
        txt = txt.replace(new RegExp('(\\|<]+)(|])', 'g'), '$1<a href="https://en.wikipedia.org/$2">$2</a>$3');
        tds.innerHTML = txt.replace(new RegExp('(\\|]*<SPAN class="*">:?)(|<]+)(</SPAN>|])', 'gi'), '$1<a href="https://en.wikipedia.org/$2">$2</a>$3');
      }
}


// If a ref appears in a diff page but isn't changed, dim it out and make it smaller so it doesn't distract from text
function dim_refs_in_diffs()
{
  var tds = document.getElementsByTagName('td');

  for (var i = 0; i < tds.length; ++i)
    if (tds.className == 'diff-deletedline' || tds.className == 'diff-addedline' || tds.className == 'diff-context')
      {
        var txt = tds.innerHTML;
        if (txt.indexOf("&@my") != -1)
          continue;
        txt = txt.replace(new RegExp('<(/?(del|ins)( class="*")?)>', 'g'), '&@mybra;$1&@myket;');
        txt = txt.replace(new RegExp('(&lt;ref*?(/&gt;|\\bref\\b( *&gt;)?))', 'gi'), '<span style="color:#cccccc !important; font-size:x-small !important;">$1</span>');
        txt = txt.replace(new RegExp('(url *= *)(http:\\/\\/\\|\\} <]+?)( *(\\||\\}\\}|</div>$))', 'gi'), '$1<a href="$2">$2</a>$3');
        txt = txt.replace(new RegExp('&@mybra;', 'g'), '<');
        txt = txt.replace(new RegExp('&@myket;', 'g'), '>');
        tds.innerHTML = txt;
      }
}


function cesarb_fixDiffOverflowTableCell(cell)
{
  var div = document.createElement('div');
  div.style.overflow = 'auto';
  cell.insertBefore(div, cell.firstChild);

  while (div.nextSibling)
    div.appendChild(div.nextSibling);
}


function cesarb_fixDiffOverflowTable(table)
{
  var cells = table.getElementsByTagName('td');

  for (var i = 0; i < cells.length; i++)
    {
      var cell = cells;
      var classes = cell.className.split(' ');

      for (var j = 0; j < classes.length; j++)
        if (classes == 'diff-context' || classes == 'diff-addedline' || classes == 'diff-deletedline' || classes == 'diff-otitle' || classes == 'diff-ntitle')
          {
            cesarb_fixDiffOverflowTableCell(cell);
            break;
          }
    }
}


function cesarb_fixDiffOverflowLoadListener(evt)
{
  var tables = document.getElementsByTagName('table');

loop:
  for (var i = 0; i < tables.length; i++)
    {
      var table = tables;
      var classes = table.className.split(' ');

      for (var j = 0; j < classes.length; j++)
        if (classes == 'diff')
          {
            cesarb_fixDiffOverflowTable(table);
            dim_refs_in_diffs();
            link_links_in_diffs();
            break loop;
          }
    }
}

function check_reason_length(node)
{
  if (node.value.length > node.maxLength
      && (node.maxLength > 0 || node.value.length > 255))
    node.style = "background-color:pink";
  else
    node.style = "background-color:white";
}

function should_watch(txt)
{
  return (!txt.match(/^\[\[WP:CSD#(C1|G7|G8|G13|U1)\|/)
          || txt.match(/^\edirect to a/));
}

/* In no particular order:
 * 1. Expands the delete dropdownlist to show all its contents instead of just
 *    the currently-selected line.
 * 2. Move the "Additional reasons" input, the watchlist checkbox, and the
 *    delete button above the newly-expanded dropdownlist, so they're always in
 *    the same place.
 * 3. Whenever an item is selected from the dropdownlist, immediately insert it
 *    into the "Additional reasons" input for further editing, then reselect
 *    "Other reason".
 * 4. Add a button to clear the reason line.
 * 5. If a preset deletion reason, or adding reasons from the dropdownlist,
 *    force the deletion reason above its maximum length, color the input pink
 *    so I know it'll be truncated. */
function expand_delete_dropdown_etc()
{
  var reasons = document.getElementById('wpDeleteReasonList');
  if (!reasons)
    return;

  /* Remove the warnings.  Seeing them 50000 times was enough.  The right way
   * to do it is to put them in a named div, but some busybody is sure to raise
   * a fuss if I edit ].  Life's too short. */
  var delreason = document.getElementById('Deletereason');
  if (delreason)
    {
      var p = delreason.parentNode;
      var ul = p.previousElementSibling;
      if (ul.nodeName == 'UL')
        {
          p = ul.previousElementSibling;
          if (p.nodeName == 'P')
            {
              p.parentNode.removeChild(p);
              ul.parentNode.removeChild(ul);
            }
        }
    }

  // OOjs considered harmful
  //$(".oo-ui-dropdownInputWidget").removeClass("oo-ui-dropdownInputWidget");
  //$(".oo-ui-dropdownInputWidget-php").removeClass("oo-ui-dropdownInputWidget-php");
  /* Expand the whole list.  We actually create a new one, copy the elements
   * from the old one into there, and hide the old one, to minimize interference
   * from hostile developers. */
  var nsel = document.createElement('select');

  var len = 0;
  var optgroups = reasons.getElementsByTagName('optgroup');
  for (var i = 0; i < optgroups.length; ++i)
    {
      var og = optgroups;
      /* Skip the oversighter-only section (how come it's displayed to
       * everyone?) - obviously this is very fragile */
      if (og.label != "FOR USE BY OVERSIGHTERS ONLY, when suppressing privacy and defamation:")
	{
	  var nog = document.createElement('optgroup');
	  nog.label = og.label;
	  nsel.appendChild(nog);
	  ++len;

	  var options = og.getElementsByTagName('option');
	  for (var j = 0; j < options.length; ++j)
	    {
	      var txt = options.value;
	      /* Entirely the wrong place to try to teach admins which articles
	       * can be A7d; the text for U5 is vague and antagonistic; and add
	       * same advice to G7 and U1 that db-g7 and db-u1 do */
	      if (txt.match(/^\\].+/))
		txt = txt.replace(/ \(indiv.+/, '');
	      else if (txt.match(/^\\].+/))
		txt = txt.replace(/Misuse of Misplaced Pages as a web host/, "Writings unrelated to Wikimedia's goals, by a user with few or no edits outside of userspace");
	      else if (txt.match(/^\\].+/))
		txt = txt.replace(/Cross-\\] \\]/, "]");
	      else if (txt.match(/^\\].+/))
		txt = txt + " – If you wish to retrieve it, please see ]";
	      else if (txt.match(/^\\].+/))
		txt = txt.replace(/to retrieve it, see/, "If you wish to retrieve it, please see");
	      else if (txt.match(/^\\].+/))
		txt = txt.replace(/ or \\] submission/, '');

	      var nop = document.createElement('option');
	      nop.value = txt;
	      nop.appendChild(document.createTextNode(txt));
	      nog.appendChild(nop);
	      ++len;
	    }
	}
    }

  nsel.id = 'nsel';
  nsel.size = len;

  var root = document.getElementById('mw-delete-table');
  var div = document.createElement('div');

  // Move the additional reason input, watch checkbox, and delete button above it, so they're always in the same place
  var reasonrow;
  var deletebutton;
  var imgtable = document.getElementById('mw-img-deleteconfirm-table');
  if (imgtable)
    {
      reasonrow = imgtable.getElementsByTagName('tr');
      deletebutton = imgtable.getElementsByTagName('tr').getElementsByClassName('mw-submit').firstElementChild;
    }
  else
    {
      reasonrow = document.getElementById('wpReason');
      deletebutton = document.getElementById('wpConfirmB');
    }

  var spacer = document.createElement('span');
  spacer.style = "margin-left:1em";

  var watch = document.getElementById('wpWatch').parentNode.parentNode;
  var watchlabel = watch.nextElementSibling;

  var clearbutton = makebutton('clear',
                               'var reason = document.getElementById("wpReason");'
                               + 'reason.value="";'
                               + 'reason.style="background-color:white";'
                               + 'reason.focus()');
  clearbutton.style = "margin-left:1em";

  div.appendChild(deletebutton);
  div.appendChild(spacer);
  div.appendChild(watch);
  div.appendChild(watchlabel);
  div.appendChild(clearbutton);
  div.appendChild(reasonrow);
  div.appendChild(nsel);

  for (var n = root.firstChild; n; n = n.nextChild)
    n.style.display='none';

  root.appendChild(div);

  /* Whenever clicking something in the reason dropdown, insert it into the
   * additional-reasons input, reselect "other reason", and unflag watched for
   * c1/g7/g8/g13/u1 */
  nsel.onchange = function()
    {
      var reason = document.getElementById('wpReason');
      var sel = this.selectedIndex;
      if (sel != 0)     // 0 is "Other reason"
        {
          var txt = this.firstChild.textContent;

          if (reason.value.length > 0)
            reason.value += '; ';
          reason.value += txt;
          check_reason_length(reason);

          document.getElementById('wpWatch').checked = should_watch(this.value);

          this.selectedIndex = 0;
          reason.focus();
        }
    };
  /* And set initial value of wpWatch for c1/g7/g8/g13/u1.  If initially g8, and
   * deleting a talk page, then don't change watched status. */
  var reasonv = document.getElementById('wpReason').value;
  if (reasonv.match(/^\alk page.*of a (deleted|non.?exist)/))
    document.getElementById('wpWatch').checked = !!document.getElementById("cb-ca-unwatch");
  else
    document.getElementById('wpWatch').checked = should_watch(reasonv);

  /* Whenever anything forces the length of the additional-reasons input above
   * its max, change its background color */
  var reason = document.getElementById('wpReason');
  reason.oninput = function(){ check_reason_length(this); };

  // And initially, e.g. from presets
  check_reason_length(reason);

  reason.focus();

  /* More oo-ui detritus - it keeps multiplying, and this one even unhides
   * itself *AND* reinserts itself if removed!!  Unbefuckinglievable.  So hide
   * it globally. */
  add_global_style('.oo-ui-dropdownWidget { display:none !important; }');
}

// Open up to 20 history pages from watchlist, starting here and going up
function openahah(kk)
{
  var as = document.getElementsByTagName('a');
  var j = 0, k = 0;
  ++kk;
  for (var n = 0; n < as.length; ++n)
    if (as.innerHTML == 'hist' && kk - k++ <= 20)
      {
        if (k <= kk)
          {
            as.setAttribute('class', as.getAttribute('class') + ' modified');
            window.open(as.href, '_blank');
          }
        else
          break;
      }
}

// Add "ahah" links to watchlist - "all histories above here"
function addahah()
{
  var as = document.getElementsByTagName('a');
  var k = 0;
  for (var n = 0; n < as.length; ++n)
    {
      if (as.innerHTML == 'diff')
        as.href = as.href.replace(/&curid=+/, '');
      else if (as.innerHTML == 'hist')
        {
          addexplicitlink('javascript:openahah(' + (k++) + ')', 'ahah', as.parentNode, as.nextSibling);
          pipe(as.parentNode, ') (', as.nextSibling);
        }
    }
}


// Shows only the most recent change to each article listed in Special:RecentChangesLinked/articlename, like on Special:Watchlist, instead of all changes
function fix_relatedchanges()
{
  var uls = document.getElementsByTagName('ul');
  var entries = new Object;
  for (var i = 0; i < uls.length; ++i)
    if (uls.className && uls.className.indexOf('special') >= 0)
      {
        var ul = uls;
        var li = ul.getElementsByTagName('li');
        for (var j = 0; j < li.length; ++j)
          {
            var a = li.getElementsByTagName('a');
            if (a.firstChild.data == 'diff')
              if (entries.href] == 'y')
                li.style.display = 'none';
              else
                {
                  a.href = a.href.replace(/&curid=+/, '');
                  entries.href] = 'y';
                }
          }
      }
}


// For each redirect link on Special:WhatLinksHere, add a "delete G8" link next to it to delete it with deletion summary prefilled
function add_g8redir_links()
{
  var tgt = null;
  var wlh = document.getElementById('mw-whatlinkshere-list');
  if (wlh)
    for (var li = wlh.firstChild; li; li = li.nextSibling)
      if (li.firstChild
          && li.firstChild.href
          && li.firstChild.href.indexOf('&redirect=no') != -1
          && li.firstChild.nextSibling.nodeValue.indexOf('(redirect page)') != -1)
        {
          if (!tgt)
            tgt = "&action=delete&wpReason="
                  + encodeURIComponent("]: Redirect to a deleted or nonexistent page: ]").replace('%20', '+', 'g')
                  + "&action=delete";

          var newnode = addexplicitlink(li.firstChild.href + tgt, 'delete G8');
          // Place on the same line, whether there are incoming links to this redirect or not
          var x;
          for (x = li.firstElementChild; x; x = x.nextElementSibling)
            if (x.tagName && x.tagName == 'UL')
              {
                li.insertBefore(newnode, x);
                break;
              }
          if (!x)
            li.appendChild(newnode);
        }
}


// Move the notifications indicator from the sidebar (which I've hidden in css) to the bottom
function move_notif(notif)
{
  var a = notif.firstChild;
  notif = a.firstChild;
  if (a.className.indexOf('mw-echo-notifications-badge-all-read') < 0 && a.className.indexOf('mw-echo-unseen-notifications') >= 0)
    {
      var search = document.getElementById("searchform-footer");
      if (search)
        {
          var newnode = document.createElement("a");
          newnode.setAttribute("class", "mw-echo-unread-notifications");
          newnode.setAttribute("href", "/Special:Notifications");
          newnode.appendChild(document.createTextNode("*"));
          search.parentNode.insertBefore(newnode, search);
        }
   }
}
function move_notifications_to_bottom()
{
  var notif1 = document.getElementById("pt-notifications-alert");
  if (notif1)
    move_notif(notif1);
  var notif2 = document.getElementById("pt-notifications-notice");
  if (notif2)
    move_notif(notif2);
}


// Replace the useless link to ] in the footer with the current time in UTC.  Click to update rather than automatic, since it's occasionally useful to know when I loaded the page.
function current_time_in_footer()
{
  var footer = document.getElementById("footer-navigation");
  if (footer)
    for (var n = footer.firstChild; n; n = n.nextSibling)
      if (n.tagName == 'A' && n.href.indexOf('Misplaced Pages:About') > 0)
        {
          var span = document.createElement('span');
          span.appendChild(document.createTextNode(new Date().toUTCString()));
          span.setAttribute('onClick', 'this.firstChild.textContent = new Date().toUTCString();');
          footer.insertBefore(span, n);
          footer.removeChild(n);
          break;
        }
}


// Add a "reduced" button when editing File: pages to edit away ]
function add_reduced_button()
{
  if (document.editform.wpTextbox1.value.match(/\{\{(on-free reduced|rphaned non-free revisions) *(\|*)*\}\}/))
    {
      var button = makebutton('reduced',
                              'var old = document.editform.wpTextbox1.value;'
                              + 'var s = old;'
                              + 's = s.replace(/^*\\{\\{(on-free reduced|rphaned non-free revisions) *(\\|*)*\\}\\}*/g, "");'
                              + 's = s.replace(/^\\{\\{(on-free reduced|rphaned non-free revisions) *(\\|*)*\\}\\}?/gm, "");'
                              + 's = s.replace(/^\\{\\{(on-free reduced|rphaned non-free revisions) *(\\|*)*\\}\\}/g, "");'
                              + 'if (s != old)'
                              + '  {'
                              + '    document.editform.wpTextbox1.value = s;'
                              + '    document.editform.wpSummary.value = "Previous version removed";'
                              + '    document.getElementById("wpWatchthis").checked = 0;'
                              + '    document.editform.wpSummary.focus();'
                              + '  }');
      var first = document.getElementById('firstHeading');
      first.appendChild(button, first.nextSibling);
      var button2 = makebutton('reducemore',
                               'var old = document.editform.wpTextbox1.value;'
                               + 'var s = old;'
                               + 's = s.replace(/^*\\{\\{(on-free reduced|rphaned non-free revisions) *(\\|*)*\\}\\}*/g, "");'
                               + 's = s.replace(/^\\{\\{(on-free reduced|rphaned non-free revisions) *(\\|*)*\\}\\}?/gm, "");'
                               + 's = s.replace(/^\\{\\{(on-free reduced|rphaned non-free revisions) *(\\|*)*\\}\\}/g, "");'
                               + 's = "{{non-free reduce}}\\n" + s;'
                               + 'if (s != old)'
                               + '  {'
                               + '    document.editform.wpTextbox1.value = s;'
                               + '    document.editform.wpSummary.value = "Previous version removed; but current version needs size reduction per ]";'
                               + '    document.getElementById("wpWatchthis").checked = 1;'
                               + '    document.editform.wpSummary.focus();'
                               + '  }');
      first.appendChild(button2, first.nextSibling.nextSibling);
    }
}


// Add a link to delete all but the current revision of a file
function add_del_prev_revisions()
{
  var filehistory = document.getElementById("filehistory");
  if (!filehistory)
    return;

  var target = '';
  var ids = '';

  var spans = document.getElementsByTagName("span");
  for (var i = 0; i < spans.length; ++i)
    {
      var span = spans;
      var classes = span.className.split(' ');
      for (var j = 0; j < classes.length; ++j)
        if (classes == 'mw-revdelundel-link')
          {
            var a = span.firstChild.nextSibling;
            if (a && a.title == 'Special:RevisionDelete')
              {
                var matches = a.href.match("&ids=(+)$");
                if (matches)
                  {
                    if (target == '')
                      {
                        target = a.href;
                        ids = matches;
                      }
                    else
                      {
                        target += ',';
                        target += matches;
                        ids += ',';
                        ids += matches;
                      }
                  }
              }
            break;
          }
    }

  if (target != '')
    {
      pipe(filehistory, ' - ');
      addexplicitlink(target + '&wpReason=' + encodeURIComponent(']: Orphaned revision of nonfree media'),
                      'Delete revisions ' + ids,
                      filehistory);
    }
}


// Autoselect "delete file content" at ] if we preselected the deleting-orphan-revisions reason
function revision_delete_stuff()
{
  var reason = document.getElementById('wpReason');
  if (reason && reason.value == ']: Orphaned revision of nonfree media')
    {
      var radios = document.getElementsByName('wpHidePrimary');
      if (radios.length == 1)   // one revision
        radios.checked = 1;
      else if (radios.length == 3)      // two or more revisions
        radios.checked = 1;
      reason.focus();   // so I can just hit enter to confirm
    }

  // Edit link
  var n = document.getElementById('firstHeading');
  n = n.nextElementSibling.nextElementSibling;
  var hist = n.firstElementChild.nextElementSibling;
  if (hist && hist.nodeName == 'A' && hist.href.match(/&action=history$/))
    {
      pipe(n);
      addexplicitlink(hist.href.replace(/action=history$/,"action=edit"), 'Edit', n);
    }
}


document.my = new Object;

function modifyWatchlist(title, action)
{
  if (title == null)
    title = mw.config.get('wgPageName');

  var reqData = {
    'action': 'watch',
    'format': 'json',
    'title': title,
    'token': mw.user.tokens.get('watchToken'),
    'uselang': mw.config.get('wgUserLanguage')
  };

  if (action === 'unwatch')
    reqData.unwatch = '';

  jQuery.ajax(
    {
      url: mw.util.wikiScript('api'),
      dataType: 'json',
      type: 'POST',
      data: reqData,
      success: function(data, textStatus, xhr)
        {
          if (action == 'unwatch')
            {
              document.my.watchlk.style.display="inline";
              document.my.unwatchlk.style.display="none";
            }
          else
            {
              document.my.watchlk.style.display="none";
              document.my.unwatchlk.style.display="inline";
            }
        }
    });
}
document.my.modifyWatchlist = modifyWatchlist;
function fix_watch_links()
{
  try
    {
      var watched = false;
      var bot = document.getElementById("cb-ca-watch");
      if (!bot)
        {
          bot = document.getElementById("cb-ca-unwatch");
          if (!bot)
            return;
          watched = true;
        }

      bot.firstChild.style.display = "none";

      var a = document.createElement("a");
      a.setAttribute("href", "javascript:document.my.modifyWatchlist(mw.util.getParamValue('title'), 'watch');");
      a.appendChild(document.createTextNode('Watch'));
      bot.appendChild(a);
      document.my.watchlk = a;
      if (watched)
        a.style.display = "none";

      a = document.createElement("a");
      a.setAttribute("href", "javascript:document.my.modifyWatchlist(mw.util.getParamValue('title'), 'unwatch');");
      a.appendChild(document.createTextNode('Unwatch'));
      bot.appendChild(a);
      document.my.unwatchlk = a;
      if (!watched)
        a.style.display = "none";
    }
  catch (e)
    {
    }
}


function add_open10()
{
  // >> on image categories
  if (mw.config.get('wgPageName').indexOf('Category:Orphaned_non-free_use_Wikipedia_files_as_of') == 0
      || mw.config.get('wgPageName').indexOf('Category:Wikipedia_files') == 0)
    {
      var div = document.getElementById('mw-category-media');
      if (div)
	for (var ul = div.firstChild; ul; ul = ul.nextSibling)
	  if (ul.tagName == 'UL' && ul.className.indexOf('mw-gallery-traditional') >= 0)
	    {
	      var count = 0;
	      var first = 0;
	      for (var node = ul.firstChild; ; node = node.nextSibling)
		{
		  if (node && node.tagName == 'LI')
		    if (++count == 1)
		      first = node;

		  if (count == 11 || (!node && count != 0))
		    {
		      var newnode = document.createElement("br");
		      ul.insertBefore(newnode, first);
		      newnode = document.createElement("a");
		      newnode.setAttribute("onClick", "var i,n=this;for(i=0;i<"+count+";++i){n=n.nextSibling;if(!n)break;if(n.tagName!='LI'){n=n.nextSibling;if(!n)break;}window.open(n.firstChild.firstChild.nextSibling.firstChild.firstChild.href,'_blank');}");
		      newnode.setAttribute("href", "#");
		      newnode.appendChild(document.createTextNode('>>'));
		      ul.insertBefore(newnode, first);
		      count = 0;
		      first = 0;
		    }

		  if (!node)
		    break;
		}
	    }
    }

  // open10 on non-image categories
  if (mw.config.get('wgPageName') == 'Category:Non-free_files_with_orphaned_versions_more_than_7_days_old'
      || mw.config.get('wgPageName').indexOf('Category:Candidates_for_speedy_deletion') == 0
      || mw.config.get('wgPageName').indexOf('Category:Candidates_for_uncontroversial_speedy_deletion') == 0
      || mw.config.get('wgPageName').indexOf('Category:Wikipedia_files') == 0)
    {
      var start = 1;
      var div = document.getElementById('mw-pages');
      if (!div)
	{
	  start = 0;
	  div = document.getElementById('mw-category-media');
	  if (!div)
	    div = document.getElementById('mw-subcategories');
	}
      if (div)
	{
	  var lks = document.getElementsByTagName('a');
	  var count = 0;
	  var hrefs = ;
	  var max_to_open = 10;
	  if (mw.config.get('wgPageName').indexOf('Category:Candidates_for_speedy_deletion_as_empty_categories') == 0)
	    max_to_open = 5;

	  for (var i = 0; i < lks.length; ++i)
	    {
	      for (var p = lks; p; p = p.parentNode)
		if (p == div
		    && lks.firstChild.nodeValue != 'next page'
		    && lks.firstChild.nodeValue != 'previous page')
		  {
		    ++count;
		    if (count != start)
		      hrefs = lks.href;
		    break;
		  }
	      if (count == max_to_open + start)
		break;
	    }

	  if (count > start)
	    {
	      count -= start;
	      var s = "";
	      for (var j = 0; j < count; ++j)
		s += 'window.open("' + hrefs + '", "_blank");';
	      var newnode = document.createElement("a");
	      newnode.setAttribute("onClick", s);
	      newnode.setAttribute("href", "#");
	      newnode.appendChild(document.createTextNode('open'+count));
	      div.insertBefore(newnode, div.firstChild.nextSibling.nextSibling);
	    }
	}
    }
}


function do_onload()
{
  fix_watch_links();
  add_open10();
  cesarb_fixDiffOverflowLoadListener();
  morelinks();
  move_notifications_to_bottom();
  current_time_in_footer();

  var pagename = mw.config.get('wgPageName');
  var action = mw.config.get('wgAction');

  if (pagename == 'Special:Watchlist')
    addahah();
  else if (pagename.indexOf('Special:RecentChangesLinked/') == 0)
    fix_relatedchanges();
  else if (pagename.indexOf('Special:WhatLinksHere/') == 0)
    add_g8redir_links();
  else if (action == 'delete')
    expand_delete_dropdown_etc();

  // Work ]
  else if (mw.config.get('wgNamespaceNumber') == 6)     // File:
    {
      if (action == 'edit')
        add_reduced_button();
      else
        add_del_prev_revisions();
    }
  else if (pagename == 'Special:RevisionDelete')
    revision_delete_stuff();

  var sum = document.getElementById("wpSummary");
  if (sum)
    sum.style.height = "18px";
}
$(do_onload);


// From ]
$('a.new').attr('href', function(index, value)
  {
    var fileTitle = mw.util.getParamValue('wpDestFile', value);
    var fileName = mw.config.get('wgFormattedNamespaces') + ':' + fileTitle;
    var params =
      {
        action: 'edit',
        redlink: 1
      };
    return mw.util.getUrl(fileName, params);
  });


// Make ffd notices more noticeable so I stop overlooking them when deleting orphaned images
$('table.imbox-delete:has(a)').each(function(){$(this).css("background-color", "red");});

// remove ugly styles from buttons:
$(".oo-ui-buttonInputWidget").removeClass("oo-ui-buttonInputWidget")
$(".oo-ui-buttonElement-button").removeClass("oo-ui-buttonElement-button")
// and dropdowns
$(".oo-ui-indicator-down").removeClass("oo-ui-indicator-down")
// and checkboxes:
$(".oo-ui-checkboxInputWidget").removeClass("oo-ui-checkboxInputWidget")
// and input fields:
$(".oo-ui-inputWidget").removeClass("oo-ui-inputWidget")
$(".oo-ui-inputWidget-input").removeClass("oo-ui-inputWidget-input")
$(".oo-ui-textInputWidget").removeClass("oo-ui-textInputWidget")
$(".oo-ui-textInputWidget-type-text").removeClass("oo-ui-textInputWidget-type-text")
$(".oo-ui-textInputWidget-php").removeClass("oo-ui-textInputWidget-php")
// remove inflated field margins:
$(".oo-ui-fieldLayout-header").removeClass("oo-ui-fieldLayout-header")
//</nowiki>