Jump to content

User:GoldenRing/difflist.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
$(document).ready( function() {
  var diff_list = $('li[data-mw-revid]');
  if(diff_list.length == 0) return;

  $('div#p-views ul').append($('<li id="ca-fetch"><span><a href="#" title="Fetch Revisions" accesskey="f" id="ca-fetch">Diffs</a></span></li>'));
  $('div#p-views').removeClass('emptyPortlet');

$('a#ca-fetch').click( function() {
  var api_url = "https://en.wikipedia.org/w/api.php";
  var table = $('<table class="diff-table" style="word-wrap: break-word; overflow-wrap: break-word; word-break: break-word;"></table>');
  table.css('width', '100%');
  var form = $('form#mw-history-compare');
  if(form.length == 0) form = $('form.mw-contributions-form');

  form.append(table);

  $('li[data-mw-revid]').each(function( id, element ) {
    $.ajax(api_url, {
      data: {
        action: 'query',
        format: 'json',
        prop: 'revisions',
        rvprop: 'user|parsedcomment|timestamp|flags|size|tags',
        rvdiffto: 'prev',
        revids: $(element).attr('data-mw-revid')
      },
      success: function(data) {
        tr = $('<tr class="diff-summary"><td colspan="4">' + $(element).html() + '</td></tr>')
        table.append(tr);
        pageids = $.map(data.query.pages, function(_,i) { return i; } );
        table.append(data.query.pages[pageids[0]].revisions[0].diff['*']);
        $(element).remove();
        $('tr:has(td.diff-context)').remove();
//        $('li>table>tbody>tr>td.diff-empty').attr('width', '47%');
//        $('td.diff-marker').attr('width', '2%');
        $('td.diff-addedline').css( {
          'border-color': '#a3d3ff',
          'border-style': 'solid',
          'border-width': '1px 1px 1px 4px'
        });//.attr('width', '47%');
        $('td.diff-deletedline').css( {
          'border-color': '#ffe49c',
          'border-style': 'solid',
          'border-width': '1px 1px 1px 4px'
        });//.attr('width', '47%');
        $('ins').css({
          'text-decoration': 'none',
          'background-color': '#a3d3ff'
        });
        $('del').css({
          'text-decoration': 'none',
          'background-color': '#ffe49c'
        });
        $('table.diff-table td').css({
          'word-wrap': 'break-word'
        });
      }
    });
  });
});
});