User:NguoiDungKhongDinhDanh/LiveDiffLink.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.
// 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) {
		return (new mw.Uri(sc)).extend({
			title: pn,
			diff: diff,
			oldid: oldid,
			unhide: 1
		}).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');
});