User:Ruakh/common.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.

This JavaScript is executed for Ruakh on every page load.


importScript('MediaWiki:SectionWatchLinks.js');

importScript('User:Ruakh/CodePointRedirects.js');


// importScript('User:Ruakh/Tbot.js'); 
// addOnloadHook(function() { Tbot.greenifyTranslinks('he'); });
// addOnloadHook(function() { Tbot.greenifyTranslinks('nl'); });
// addOnloadHook(function() { Tbot.greenifyTranslinks('ru'); });
// addOnloadHook(function() { Tbot.greenifyTranslinks('yi'); });


/*</pre>
==Rollback-message==
<pre>*/

// Change rollback-message to point users at my talk-page:
addOnloadHook
( function ()
  { $('span.mw-rollback-link a').each
    ( function ()
      { if(this.href.indexOf('&action=rollback&') == -1)
          return;
        if(this.href.indexOf('summary=') > -1)
          return;
        this.href +=
          '&summary=' +
          encodeURIComponent
          ( 'Reverted edits by [[Special:Contributions/$2|$2]] ' +
            '([[User talk:$2|talk]]), restoring last version by ' +
            '$1. If you disagree with this rollback, please ' +
            'leave a message on my talk-page.'
          );
      }
    );
  }
);


/*</pre>

==Uselang-propagation==
<pre>*/

// At pages whose URLs include uselang=..., add uselang= to all links and forms:
addOnloadHook
( function ()
  { document.location.href.replace
    ( /[?](?:.*&)?uselang=([^&#]+)/,
      function (s, langcode)
      { var forms = document.getElementsByTagName('form');
        for(var i = 0; i < forms.length; ++i)
          if(forms[i].method == 'post')
            if(forms[i].action.indexOf('?') > -1)
              forms[i].action = forms[i].action += '&uselang=' + langcode;
            else
              forms[i].action = forms[i].action += '?uselang=' + langcode;
          else
            forms[i].appendChild
            ( newNode
              ( 'input',
                { name: 'uselang',
                  value: langcode,
                  type: 'hidden'
                }
              )
            );
        var links = document.getElementsByTagName('a');
        for(var i = 0; i < links.length; ++i)
          if(links[i].href)
            if(links[i].href.indexOf('?') > -1)
              links[i].href =
                links[i].href.replace(/#|$/, '&uselang=' + langcode + '$&');
            else
              links[i].href =
                links[i].href.replace(/#|$/, '?uselang=' + langcode + '$&');
      }
    );
  }
);


/*</pre>
==node2str==
<pre>*/

function node2str(node, indent)
{
  if(! indent)
    indent = '';
  if(node.nodeType == Node.TEXT_NODE)
    return indent + '"' + node.data + '"\n';
  if(node.nodeType != Node.ELEMENT_NODE)
    return indent + node + '\n';
  var ret = indent + '<' + node.tagName + '>\n';
  for(var child = node.firstChild; child; child = child.nextSibling)
    ret = ret + node2str(child, indent + '    ');
  ret = ret + indent + '</' + node.tagName + '>\n';
  return ret;
}


/*</pre>
==Useful links on Special:NewPages==
<pre>*/

addOnloadHook
( function ()
  { if(mediaWiki.config.get('wgPageName') != 'Special:NewPages')
      return;
    var links =
      document.getElementsByTagName('ul')[0].getElementsByTagName('a');
    for(var i = links.length - 1; i >= 0; --i)
    { if(links[i].className != 'mw-newpages-pagename')
        continue;
      var bgc_link =
      ( newNode
        ( 'a',
          { className: 'external strike-if-visited',
            href: 'http://books.google.com/books?q=%22' +
                  encodeURIComponent(links[i].title) + '%22'
          },
          'b.g.c.'
        )
      );
      links[i].parentNode.insertBefore(bgc_link, links[i].nextSibling);
      links[i].parentNode.insertBefore
        (document.createTextNode(' · '), bgc_link);
    }
  }
);



/*</pre>
==Page-creation preloads==
<pre>*/

// When preparing to edit a non-existent page in the main mainspace,
// add links for some preloads:
/*
addOnloadHook
(
   function ()
   {
     if(window.mediaWiki.config.get('wgAction') != 'edit')
       return;
     if(window.mediaWiki.config.get('wgNamespaceNumber') != 0)
       return;
     if(window.mediaWiki.config.get('wgArticleId') != 0)
       return;
     var mwNewarticleText = $("div.mw-newarticletext");
     if(mwNewarticleText.length == 0)
       return;
     mwNewarticleText = mwNewarticleText.first();
     var p = newNode('p', {className: 'plainlinks'}, newNode('b', 'Preloads:'), ' ');
     var preloads = ['foo', 'bar', 'new baz'];
     for(var i = 0; i < preloads.length; ++i)
     {
       var link = preloads[i].replace(/ /g, '_');
       if(link.indexOf(':') == -1)
         link = 'Template:' + link;
       link = window.location.href + '&preload=' + link;
       link = newNode('a', {href: link, className: 'external text'}, preloads[i]);
       if(i > 0)
         p.appendChild(document.createTextNode(' · '));
       p.appendChild(link);
     }
     mwNewarticleText.append(p);
   }
);
*/



/*</pre>
==Struck-out/redlinked headers==
<pre>*/

// make <s> and <strike> and redlinks in headers apparent in TOC:
addOnloadHook
(
   function ()
   {
     if(! document.getElementById)
       return;
     var toc = document.getElementById('toc');
     if(! toc)
       return;
     if(! toc.getElementsByTagName)
       return;
     toc = toc.getElementsByTagName('a');
     for(var i = 0; i < toc.length; ++i)
     {
       var link = toc[i];
       var target = link.hash.substr(1);
       if(! target || ! target.length)
         continue;
       target = document.getElementById(target);
       if(! target)
         continue;
       if(target.getElementsByTagName('a').length > 0)
         if(target.getElementsByTagName('a')[0].className == 'new')
           link.className = 'new';
       var firstChild = target.childNodes[0];
       if(firstChild.nodeType != Node.ELEMENT_NODE)
         continue;
       if(firstChild.tagName == 'S' || firstChild.tagName == 'STRIKE')
       {
         var s = document.createElement('s');
         while(link.childNodes.length > 0)
           s.appendChild(link.childNodes[0]);
         link.appendChild(s);
       }
     }
   }
);



/*</pre>
==Watchlist diff-links==
<pre>*/

// make "diff" links on watchlist match corresponding links in revision history:
addOnloadHook
(
  function()
  {
    if(wgPageName != 'Special:Watchlist')
      return;
    var specials = document.getElementsByClassName('special');
    for(var i = 0; i < specials.length; ++i)
    {
      var lis = specials[i].getElementsByTagName('li');
      for(var j = 0; j < lis.length; ++j)
      {
        var link = lis[j].getElementsByTagName('a')[0];
        if(! link) // shouldn't happen
          continue;
        if(link.innerHTML != 'diff') // new pages, log entries, etc.
          continue;
        link.href = link.href.replace(/&(amp;)?curid=\d+/, '');
      }
    }
  }
);



/*</pre>
==Edit-tools==
<pre>*/

// Maps (for example) 'IPA and enPR' to the page element with ID
// 'edittools-IPA_and_enPR', if it  exists.
function findEdittoolsSection(name)
{
  return document.getElementById('edittools-' + name.replace(/ /g, '_'));
}

// "call out" the edit-tools I ever actually use:
addOnloadHook
(
  function()
  {
    window.setTimeout
    (
      function()
      {
        var edittools = document.getElementById('editpage-specialchars');
        if(! edittools)
          return;
        var calloutNames =
          ['Miscellaneous', 'Hebrew', 'Latin/Roman', 'IPA and enPR'];
        var callout = document.createElement('div');
        for(var i = 0; i < calloutNames.length; ++i)
        {
          var section = findEdittoolsSection(calloutNames[i]);
          var calloutSection = document.createElement('p');
          calloutSection.style.border = '1px dotted #339966';
          calloutSection.style.margin = '0.5em';
          calloutSection.innerHTML = '<big><b>' + calloutNames[i] + ':</b></big> &nbsp; ';
          if(section)
          {
            while(section.firstChild)
              calloutSection.appendChild(section.firstChild);
            section.appendChild(document.createTextNode('[see above]'));
          }
          else
            calloutSection.innerHTML += '[no such section]';
          callout.appendChild(calloutSection);
        }
        edittools.parentNode.insertBefore(callout, edittools);
      },
      1000
    );
  }
);