User:Spike/watchgadget-v3.js

From Miraheze Meta, Miraheze's central coordination wiki

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
window.spike = window.spike || {};
 
spike.watchGadget = {
  cells: {},
  uwLink: "<a class=unwatchButton href=\"javascript:spike.watchGadget.unwatch('URI')\">u</a><span> </span>",
  rwLink: "<a class=rewatchButton href=\"javascript:spike.watchGadget.rewatch('URI')\">r</a><span> </span><A style=\"text-decoration:line-through\" href=\"" + mw.config.get('wgServer') + "/wiki/URI\">ARTICLE</A> (unwatched)",

  init: function() {
    var pn = mw.config.get('wgCanonicalSpecialPageName');
    if ((pn != "Watchlist") && (pn != "Recentchanges")) { return };
// Load stuff
    mw.loader.using(['mediawiki.util', 'mediawiki.api.watch']);
// Cosmetic tweaks
//
    $("A.mw-changeslist-history").text("hist");
    $("A.mw-changeslist-groupdiff").each( function () {
      $(this).text($(this).text().replace(/\ changes/, "×"));
      });
// Same change for entries on new articles with multiple edits ("10 changes" is not an element)
    $("TD.mw-changeslist-line-inner").each( function () {
      $(this).html($(this).html().replace(/\ changes/, "×"));
      });
    $("SPAN.mw-changeslist-separator").text("•");
// Replace photo with arrow for collapsible entries, for those of us running with images disabled
// Just so I know where to click; am not going to try to make this modal
    $("TABLE.mw-collapsible SPAN.mw-collapsible-toggle").html("▶");

    var gadget = spike.watchGadget;
// Loop through every anchor that is the title of a wiki page
    $( ".mw-changeslist-title").each( function() {
      var obj = $( this ), article = obj.text();
// Limit it further to buttons whose text is the article title (ignoring "hist" and "diff" links)
// And when in RecentChanges, limit it to pages also on the Watchlist
      if ((article == obj.attr("title"))
      && ((pn == "Watchlist") || (obj.closest("TABLE.mw-changeslist-line").hasClass("mw-changeslist-line-watched")))
         )
        {
// Using encodeURIComponent() below encodes pages with single or double quotes in the page title
// to prevent premature string termination in uwLink and rwLink
        ( gadget.cells[article] || ( gadget.cells[article] = [] )).push(
          $( $( gadget.uwLink.replace( /URI/, encodeURIComponent(article) ))
            .insertBefore( this ).closest("td"))
          );
        }
      });
    },

  unwatch: function( article ) {
    new mw.Api().unwatch( article, function( response ) {} );

// Formulate a legend to replace the page name.
    var unwatchedLegend = spike.watchGadget.rwLink
      .replace(/URI/g,    encodeURIComponent(article))
      .replace(/ARTICLE/, article);
// Save the original entry (the entire line) at "oldHtml" and replace the page name with the new message
// "this" is the table cell being manipulated
    $( spike.watchGadget.cells[article] ).each( function() {
        this.data("oldHtml", this.html()).find("A.unwatchButton").closest("TD").html(unwatchedLegend);
      });
    },
 
  rewatch: function( article ) {
    new mw.Api().watch( article, function( response ) {} );
// After "watch" returns, restore the table cell to its original state (before unwatching)
    $( spike.watchGadget.cells[article] ).each( function() {
      this.html(this.data("oldHtml"));
      });
    }
  };
 
$(document).ready(spike.watchGadget.init);