Jump to content

User:Nardog/Consecudiff.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nardog (talk | contribs) at 00:08, 25 January 2021 (Created page with 'mw.loader.using('mediawiki.util', () => { mw.util.addCSS('.consecudiff::before{content:" ["} .consecudiff::after{content:"]"} .consecudiff-top::before{content:"...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
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.
mw.loader.using('mediawiki.util', () => {
	mw.util.addCSS('.consecudiff::before{content:" ["} .consecudiff::after{content:"]"} .consecudiff-top::before{content:" ⟨"} .consecudiff-top::after{content:"⟩"}');
});

mw.hook('wikipage.content').add(function consecudiff($content) {
	if (!($content.hasClass('mw-changeslist') || $content.find('.mw-changeslist, .mw-contributions-list, #pagehistory').length)) return;
	
	$content.find('.consecudiff').remove();
	
	let isHist = mw.config.get('wgAction') === 'history';
	let isContribs = !isHist && $content[0].querySelector('.mw-contributions-list') && true;
	let isEnhanced = !(isHist || isContribs) && $content[0].querySelector('.mw-enhanced-rc') && true;
	let threshold = isContribs ? window.consecudiffContribsThreshold || 120 :
		isHist ? window.consecudiffHistThreshold || 720 :
		window.consecudiffThreshold || 720;
	let strict = !(isHist || isContribs) && window.consecudiffDetectInterruptions;
	let o = {};
	
	$content[0].querySelectorAll(
		isHist ? '#pagehistory > li' :
		isContribs ? '.mw-contributions-list > li' :
		isEnhanced ? '.mw-changeslist-src-mw-edit.mw-rcfilters-ui-highlights-enhanced-nested, .mw-changeslist-src-mw-new.mw-rcfilters-ui-highlights-enhanced-nested' :
		'.mw-changeslist-src-mw-edit, .mw-changeslist-src-mw-new'
	).forEach(li => {
		let title = isHist || (isEnhanced ? li.parentElement : li).querySelector(isContribs ? '.mw-contributions-title' : '.mw-title').innerText;
		let user = (li.querySelector('.mw-userlink') || {}).innerText;
		let date = Date.parse(
			(isHist || isContribs) ? li.querySelector('.mw-changeslist-date').innerText.replace(',', '') :
			li.dataset.mwTs.replace(/(....)(..)(..)(..)(..)(..)/, '$1-$2-$3T$4:$5:$6Z')
		) / 60000;
		let parent = isContribs && li.parentElement;
		if (o[title]) {
			if (o[title].lastUser && o[title].lastUser !== user ||
				o[title].lastDate && o[title].lastDate - date > threshold ||
				o[title].lastParent && o[title].lastParent !== parent ||
				o[title].nextRev && o[title].nextRev !== li.dataset.mwRevid
			) {
				o[title].liss.push([li]);
			} else {
				o[title].liss[o[title].liss.length - 1].push(li);
			}
		} else {
			o[title] = { liss: [[li]] };
		}
		if (user) o[title].lastUser = user;
		if (date) o[title].lastDate = date;
		if (parent) o[title].lastParent = parent;
		if (strict) {
			let diff = li.querySelector('.mw-changeslist-diff');
			if (diff) o[title].nextRev = diff.getAttribute('href').match(/&oldid=(\d+)/)[1];
		}
	});
	
	let diffSelector = isHist ? '.mw-history-histlinks > span:last-child > a' :
		isContribs ? '.mw-changeslist-links a' :
		'.mw-changeslist-diff';
	Object.values(o).forEach(v => {
		v.liss.filter(lis => lis.length > 1).forEach(lis => {
			let count = lis.length;
			let link = lis[0].querySelector(diffSelector);
			if (!link) return;
			link = link.getAttribute('href');
			let oldid;
			if (isContribs) {
				oldid = lis[count - 1].dataset.mwRevid;
				link = link.replace(/diff=prev&oldid=\d+/, `diff=${lis[0].dataset.mwRevid}&oldid=${oldid}&direction=prev`);
			} else {
				let bottomLink = lis[count - 1].querySelector(diffSelector);
				if (!bottomLink) return;
				oldid = bottomLink.getAttribute('href').match(/oldid=(\d+)/)[1];
				link = link.replace(/oldid=\d+/, 'oldid=' + oldid);
			}
			let classes = ['consecudiff'];
			if (!isHist &&
				lis[0].classList.contains(isContribs ? 'mw-contributions-current' : 'mw-changeslist-last')
			) {
				classes.push('consecudiff-top');
			}
			lis.forEach((li, i) => {
				$('<span>')
					.addClass(classes)
					.append($('<a>', {
						href: link,
						text: count - i + '/' + count
					}))
					.appendTo(isEnhanced ? li.lastElementChild : li);
			});
		});
	});
});