User:Ruakh/common.js

From Wiktionary, the free dictionary
Archived revision by Ruakh (talk | contribs) as of 19:59, 10 August 2012.
Jump to navigation Jump to search

Note – after saving, you may have to bypass your browser’s cache to see the changes.

  • 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.


function unwatchPage(token, title)
{
  token = encodeURIComponent(token);
  title = encodeURIComponent(title);
  var succeeded = false;
  $.ajax
  ({
    async: false,
    type: 'POST',
    url: '/w/api.php?format=json&action=watch&unwatch=&token=' + token + '&title=' + title,
    data: '',
    dataType: 'json',
    success:
      function(data, textStatus, jqXHR)
      {
        succeeded = data && data.watch && data.watch.unwatched === '';
        if(! succeeded)
          alert('13451345\n' + textStatus + '\n' + jqXHR.responseText);
      },
    error:
      function(jqXHR, textStatus, errorThrown)
      {
        alert('123412624\n' + textStatus + '\n' + jqXHR.responseText + '\n' + errorThrown);
      }
  });
  return succeeded;
}

function unwatchRange(token, ns, lb, ub)
  // ns = namespace-number, e.g. 0 for mainspace
  // token = watch-token
  // lb = lower-bound, e.g. '\u00A0' to unwatch titles that start with
  //          non-ASCII characters - is a "loose" lower bound, title >= lb
  // ub = upper-bound, e.g. '\u00A0' to unwatch titles that start with
  //          ASCII characters - is a "strict" upper bound, title < ub
{
  if(! /^\d+$/.test(ns)) // invalid namespace-number
    return false;
  if(lb === window.undefined)
    lb = '';
  else
    lb = encodeURIComponent(lb);
  while(true)
  {
    var titles = null;
    $.ajax
    ({
      async: false,
      type: 'GET',
      url: '/w/api.php?format=json&action=query&unwatch=&list=watchlistraw&wrcontinue=' + ns + '|' + lb,
      dataType: 'json',
      success:
        function(data, textStatus, jqXHR)
        {
          if(data && data.watchlistraw)
          {
            titles = [];
            for(var i = 0; i < data.watchlistraw.length; ++i)
              if(data.watchlistraw[i].ns == ns)
                if(ub === window.undefined || data.watchlistraw[i].title < ub)
                  titles[titles.length] = data.watchlistraw[i].title;
          }
          else
          {
            alert('23462346\n' + textStatus + '\n' + jqXHR.responseText);
          }
        },
      error:
        function(jqXHR, textStatus, errorThrown)
        {
          alert('2368884\n' + textStatus + '\n' + jqXHR.responseText + '\n' + errorThrown);
        }
    });
    if(! titles)
      return false;
    if(titles.length == 0)
      return true;
    for(var i = 0; i < titles.length; ++i)
      if(! unwatchPage(token, titles[i]))
      {
        alert('1325657856\nerror unwatching [[' + titles[i] + ']].');
        return false;
      }
  }
}

 
addOnloadHook
(
  function ()
  {
    if(mediaWiki.config.get('wgPageName') != 'Special:TrimWatchList')
      return;
    var jqXHR =
      $.getJSON
      (
        '/w/api.php?format=json&action=query&prop=info&intoken=watch&titles=Main%20page',
        function (data, textStatus, jqXHR)
        {
          if(! data || ! data.query || ! data.query.pages
             || ! data.query.pages[139449]
             || ! data.query.pages[139449].watchtoken)
          {
            alert('error obtaining watch-token\n' + textStatus + '\n' + jqXHR.responseText);
            return;
          }

          var watchtoken = data.query.pages[139449].watchtoken;

          alert('successfully obtained watch-token . . .');

          if(unwatchRange(watchtoken, 0, '\u00A0'))
            alert('successfully unwatched all entries whose pagenames start with non-ASCII characters');
          else
            alert('error unwatching entries whose pagenames start with non-ASCII characters');

          if(unwatchRange(watchtoken, 2))
            alert('successfully unwatched all user- and user-talk pages');
          else
            alert('error unwatching user- and user-talk pages');

          if(unwatchRange(watchtoken, 10))
            alert('successfully unwatched all template- and template-talk pages');
          else
            alert('error unwatching template- and template-talk pages');

          if(unwatchRange(watchtoken, 100))
            alert('successfully unwatched all appendix- and appendix-talk pages');
          else
            alert('error unwatching appendix- and appendix-talk pages');

          if(unwatchRange(watchtoken, 114))
            alert('successfully unwatched all citations- and citations-talk pages');
          else
            alert('error unwatching citations- and citations-talk pages');
        }
      );
    jqXHR.error
    (
      function ()
      {
        alert('error obtaining watch-token', jqXHR.responseText);
      }
    );
  }
);