User:Mikevoir/betterDiff.js

$(function {	var lib = window._LIB;	var api = new mw.Api;	var betterDiff = {		config: mw.config.get(['wgDiffNewId', 'wgAction', 'wgCanonicalSpecialPageName']),		init: function {			// Each mutation array represents a reload (e.g., through filter change)			var observer = new MutationObserver(function (mutations, me) { if (					mutations[0] &&					mutations[0].target &&					mutations[0].target.classList.contains('mw-rcfilters-ui-changesListWrapperWidget')				) {betterDiff.newDiffLink;} });			// Check we're in Special:RecentChanges			if (this.config.wgCanonicalSpecialPageName == 'Recentchanges') {				lib.waitFor('.mw-changeslist div', this.newDiffLink);				// start observing				observer.observe(document.querySelector('.mw-changeslist'), { childList: true, subtree: true });			}			// Check we're in Special:Contributions			else if (this.config.wgCanonicalSpecialPageName == 'Contributions') {				lib.waitFor('ul.mw-contributions-list li', this.newDiffLink);				// start observing				observer.observe(document.querySelector('ul.mw-contributions-list'), { childList: true, subtree: true });			}			// Check we're in a history page			else if (this.config.wgAction == 'history') {				lib.waitFor('.mw-history-histlinks', this.newDiffLink);			}			// Check we're in a diff or history page			else if (this.config.wgDiffNewId) {				lib.waitFor('#mw-diff-ntitle1', this.newDiff);			}		},		// Properly build diff links with page creation edits		newDiffLink: function {			var newDiffLink = {				searchRevid: function (marker) {					var O = {						row: marker.parentElement,						revid: [],						label: 'diff'					};					if ( O.row.nodeName == 'LI' && O.row.getAttribute('data-mw-revid')) { // Special:Contributions						O.revid.push(O.row.getAttribute('data-mw-revid')); // Page creation revision						O.label = 'prev';					} else {						O.row = O.row.parentElement;						if (O.row.classList.contains('mw-rcfilters-ui-highlights-enhanced-toplevel')) { // Special:RecentChanges top edit							console.log('top edit: ', O.row.nextElementSibling); O.revid.push(O.row.nextElementSibling.getAttribute('data-mw-revid')); // Last revision O.revid.push(O.row.parentElement.lastElementChild.getAttribute('data-mw-revid')); // Page creation revision } else if (O.row.classList.length == 0 && O.row.nodeName == 'TR') { // Special:RecentChanges sole edit console.log('sole edit: ', O.row.parentElement.parentElement); O.row = O.row.parentElement.parentElement; O.revid.push(O.row.getAttribute('data-mw-revid')); // Page creation revision }					}					if (O.revid.length>0) { return newDiffLink.buildLink(O); } else { return; }				},				buildLink: function(options) { var target = newDiffLink.getTarget(options.row); var page = newDiffLink.getTitle(options.row); if (target) { var link = document.createElement('a'); var from = options.revid[options.revid.length-1]; var to = options.revid[0]; var href = '/wiki/' + page + '?diff=' + to; if (from == to) { link.classList.add('mw-changeslist-diff'); } else { href = href + '&oldid=' + from; options.label = options.revid.length + ' changes'; link.classList.add('mw-changeslist-groupdiff'); }						link.href = href; link.title = page; link.innerHTML = options.label; if (target.nodeType == 3) { var split = /^([^\d\w]*)([\d\w\s]*)([^\d\w]*)$/.exec(target.textContent); if (split[1].length>0) {target.parentNode.insertBefore(document.createTextNode(split[1]), target);} target.parentNode.insertBefore(link, target); if (split[3].length>0) {target.parentNode.insertBefore(document.createTextNode(split[3]), target);} target.remove; return; } else { target.replaceChildren(link); return; }					} else { return; } },				getTitle: function(row) { var title; var queries = [ '.mw-changeslist-title', // Normal Special:RecentChanges '.mw-changeslist-date', // Nested Special:RecentChanges '.mw-enhanced-rc-time > a' // Special:Contributions ];					queries.forEach(function(query) {						if (!title && row.querySelector(query) && row.querySelector(query).getAttribute('title')) {							title = row.querySelector(query).getAttribute('title');						}					}); console.log('getTitle: ', title); return title || ''; },				getTarget: function(row) { var target; var queries = [ '.mw-changeslist-links:not(.mw-history-histlinks, .mw-usertoollinks) > span:first-child', // Special:Contributions && Normal Special:RecentChanges '.mw-changeslist-diff-cur + .mw-changeslist-separator', // Nested Special:RecentChanges '.mw-changeslist-links.mw-history-histlinks:not(.mw-usertoollinks) > span:last-child', // ?action=history ];					queries.forEach(function(query) {						var test = row.querySelector(query);						if (!target && test.classList.length==0) {							target = test.firstChild;						} else if (!target && test.classList.length>0 && test.classList.length>0) {							target = test.previousSibling;						}					}); console.log('getTarget: ', target); return target; }			};			// Start looking if (document.querySelector('abbr.newpage')){ var nodes = Array.prototype.slice.call(document.querySelectorAll('abbr.newpage'), 0); var i = 0; for (i; i strong > a')) { var table = document.querySelector('.diff'); var revid = document.querySelector('#mw-diff-ntitle1 > strong > a').href.replace(/^.+\?oldid=/g, ''); var api_opt = { action: 'compare', torev: revid, fromslots: 'main', 'fromtext-main': '', prop: 'diff|ids', formatversion: 2 };				if ( revid && document.querySelector('#mw-diff-otitle1 > strong > a') && !document.querySelector('#differences-prevlink') ) { //Add edit link to page creation diff table.querySelector('#mw-diff-otitle4').innerHTML = ' strong > a').title +					'?diff=' +					document.querySelector('#mw-diff-otitle1 > strong > a').href.replace(/^.+\?oldid=/g, '') +					'&oldid=0" title="' +					document.querySelector('#mw-diff-otitle1 > strong > a').title +					'" id="differences-prevlink">← Older edit'; } else if (revid && !document.querySelector('#differences-prevlink')) { table.querySelector('.diff-ntitle').colSpan = 4; table.querySelector('.diff-notice').parentNode.remove; api.get(api_opt).then(function(data) {						table.innerHTML = '  ' + table.innerHTML;						table.querySelector('tbody').innerHTML = table.querySelector('tbody').innerHTML + data.compare.body;					}); }			}		}	};	mw.loader.using('mediawiki.api').then(function{betterDiff.init;}); });