Jump to content

User:GoldenRing/generate-diffs.js

From Wikipedia, the free encyclopedia
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() {
  // User contribution lists don't already have checkboxes
  var versions = $('ul.mw-contributions-list').find('li[data-mw-revid]');
  versions.prepend(function(i, el) {
    return '<input name="ids[' + $(this).attr('data-mw-revid') + ']" type="checkbox" value="1">';
  });

  $('div.mw-history-revisionactions').prepend('<button name="copydiffs" id="copydiffs" type="button" value="1" class="historysubmit mw-history-copydiffs-button">Copy diffs to clipboard</button>');
  $('form.mw-contributions-form').after('<div><button name="copydiffs" id="copydiffs" type="button" value="1" class="historysubmit mw-history-copydiffs-button">Copy diffs to clipboard</button></div>');
  $('button#copydiffs').after('<input id="diff-list-copy-field" name="diff-list-copy-field">');
  $('button#copydiffs').click(function() {
    var checked_boxes = $($('input[type="checkbox"][name^="ids["]:checked').get().reverse());
    var text =
      checked_boxes.map(function (index, element) {
        var name = $(element).attr('name');
        var id = name.substring(4, name.length-1);
        return '[[Special:Diff/' + id + '|diff]]';
      }).get().join(', ');
    var target = document.getElementById('diff-list-copy-field');
    target.value = text;
    target.focus();
    console.log(target.value);
    target.setSelectionRange(0, target.value.length);
    console.log(document.execCommand('copy'));
  });
});