User:NguoiDungKhongDinhDanh/LiveDiffLink.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:NguoiDungKhongDinhDanh/LiveDiffLink. |
// Globalized LiveDiffLink.
// For attribution: [[User:Equazcion/LiveDiffLink.js]]
$(function() {
// Don't do anything unless we're on a History page.
if (mw.config.get('wgAction') !== 'history') return;
var sc = mw.config.get('wgScript');
var pn = mw.config.get('wgPageName');
var getlink = function(diff, oldid) {
let params = {
title: pn,
diff: diff,
oldid: oldid,
unhide: 1
};
let url = new URL(sc, location.href);
Object.entries(params).forEach(([key, value]) => {
url.searchParams.set(key, value);
});
return url.toString();
};
// Grab the Compare buttons, create an empty link after each, and grab the empty links
$('.mw-history-compareselectedversions-button').after(
$('<a>').attr({
class: 'LDL-diffurl'
}).css({
border: '1px dashed #999999',
padding: '2px 4px'
})
);
$('.LDL-diffurl').before($('<b>').text(' → '));
// Event listener for copy buttons.
$('#pagehistory').on('click', '.LDL-copybuttons', function(e) {
e.preventDefault();
navigator.clipboard.writeText($($('.LDL-diffurl')[0]).text());
mw.notify('Added diff wikilink to clipboard.', {
title: 'Copied!',
tag: 'livedifflinknotification',
});
});
// Set the Click event function for radio buttons
$('#pagehistory').on('click', '[name="diff"]:radio, [name="oldid"]:radio', function() {
// Clear existing revision size links
$('.LDL-difflinks').remove();
// Grab the revision selection, place in appropriate var
var diff = $('[name="diff"]:checked').val();
var oldid = $('[name="oldid"]:checked').val();
var link = getlink(diff, oldid);
// Update the main diff link URL, text, & tooltip
$('.LDL-diffurl').attr({
href: link,
title: link
}).text('[[Special:Diff/' + oldid + '/' + diff + ']]');
// Add diff link before revision sizes
$('[name="diff"]:checked, [name="oldid"]:checked').closest('li').find('.history-size').before(
$('<span>').attr({
'class': 'LDL-difflinks'
}).append(
'(',
$('<a>').attr({
href: link,
title: link
}).text('diff'),
') ',
$('<button>').attr({
class: 'LDL-copybuttons'
}).css({
padding: '1px',
'font-size': '0.75em'
}).text('Copy'),
' . . '
)
);
});
$('[name="diff"]:radio, [name="oldid"]:radio').not('[disabled]').first().trigger('click');
});