Jump to content

User:GoldenRing/generate-diffs.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by GoldenRing (talk | contribs) at 11:45, 14 October 2017. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
// {{pp-template}}
$(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>').after('<input id="diff-list-copy-field" name="diff-list-copy-field" style="display: none">');
  $('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>').after('<input id="diff-list-copy-field" name="diff-list-copy-field" style="display: none">');

  $('button#copydiffs').click(function() {
    var checked_boxes = $('input[type="checkbox"][name^="ids["]:checked');
    var text = ', '.join(
      checked_boxes.map(function (index, element) {
        var name = $(element).attr('name');
        var id = name.substring(4, name.length-2);
        return '[[Special:Diff/' + id + '|diff]]';
      })
    );
    var target = document.getElementById('diff-list-copy-field');
    target.focus();
    target.setSelectionRange(0, target.value.length);
    document.execCommand('copy');
  });
});