MediaWiki:SectionWatchLinks.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.

// {{documentation}}
/* To use this, add this line:

importScript('MediaWiki:SectionWatchLinks.js');

 * to <http://en.wiktionary.org/wiki/Special:Mypage/common.js>.
 */

$(function ()
{
  if(mw.config.get('wgAction') !== 'view')
    return;

  var prevTitle = '';
  var prevH = '';

  var links = $('span.mw-editsection > a');
  var relevantLinks = [];
  for(var i = 0; i < links.length; ++i)
  {
    var link = links[i];
    if(! /^[hH][1-6]$/.test(link.parentNode.parentNode.nodeName))
      continue;
    var title =
      /\/w\/index.php\?title=([^&]+)&action=edit&section=T-\d+$/.exec(link.href);
    if(title === null)
      continue;
    title = title[1];
    if(title === prevTitle)
    {
      if(link.parentNode.parentNode.nodeName > prevH)
        continue;
    }
    else
    {
      prevTitle = title;
      prevH = link.parentNode.parentNode.nodeName;
    }
    relevantLinks.push(link);
  }

  var watchMsg = 'watch';
  var unwatchMsg = 'unwatch';

  for(var i = 0; i < relevantLinks.length; ++i) 
  {
    var link = relevantLinks[i];
    var title = link.getAttribute('title');
    var pagename = /\/w\/index.php\?title=([^&]+)/.exec(link.href)[1];

    var nodes =
    [
      newNode('a', { href: '/w/index.php?title='+pagename+'&action=watch', title: title }, watchMsg),
      document.createTextNode(' \xB7 '),
      newNode('a', { href: '/w/index.php?title='+pagename+'&action=unwatch', title: title }, unwatchMsg),
      document.createTextNode(' \xB7 ')
    ];

    for(var j = 0; j < nodes.length; ++j)
      link.parentNode.insertBefore(nodes[j], link);
  }
});