User:Galaktos/checkMoveDiff.js

From Wikidata
Jump to navigation Jump to search

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.
/*
 * On diff pages for page moves,
 * check if the page actually moved.
 * If there is a redirect from the old link to the new one,
 * a green checkmark is prepended to the edit comment.
 */
if ($("body").hasClass("wb-diffpage")) {
  var comment = $(".diff-ntitle span.autocomment");
  var match = comment.text().match(/Page moved from \[[^:]*:(.*)\] to \[[^:]*:(.*)\]/);
  if (match && match[1] && match[2]) {
    var oldpage = match[1];
    var newpage = match[2];
    var targetA = $("table.diff td.diff-addedline ins.diffchange-inline a")[0];
    var baseurl = targetA.href.replace(/\/wiki\/.*/, "");
    $.ajax(baseurl + "/w/api.php", {
      data: {
        "format": "json",
        "action": "query",
        "titles": newpage,
        "prop": "redirects",
        "rdprop": "title",
        "origin": "https://www.wikidata.org"
      },
      beforeSend: function(xhr) {
        xhr.setRequestHeader("Origin", "https://www.wikidata.org");
      },
      success: function(data) {
        for (var revid in data.query.pages) {
          var page = data.query.pages[revid];
          for (var redIndex in page.redirects) {
            var redirect = page.redirects[redIndex];
            if (redirect.title === oldpage) {
              comment.before('<span style="color:green">✔</span>');
            }
          }
        }
      }
    });
  }
}