User:Mikevoir/betterDiff.js

mw.loader.using('mediawiki.api').then(function {	var lib = this._LIB;	var api = new mw.Api;	var betterDiff = {		config: mw.config.get(['wgDiffNewId', 'wgAction', 'wgCanonicalSpecialPageName']),		init: function {			// Check we're in RC			if (this.config.wgCanonicalSpecialPageName == 'Recentchanges') {				lib.waitFor('.mw-changeslist-links > span:first-child', this.newDiffLink);				// Each mutation array represents an RC reload (e.g., through filter change)				var observer = new MutationObserver(function (mutations, me) { betterDiff.newDiffLink; });				// start observing				observer.observe(document.querySelector('.mw-changeslist'), { childList: true, subtree: true });			}			// Check we're in a diff or history page			if (this.config.wgDiffNewId || this.config.wgAction == 'history') {				lib.waitFor('#mw-diff-ntitle1', this.newDiff);			}		},		// Make RC properly give links on diffs that include page creations		newDiffLink: function {			document.querySelectorAll('.mw-changeslist-links > span:first-child').forEach( function(el) { if (el.childNodes.length === 1 && el.childElementCount === 0){ var row = el.parentNode.parentNode.parentNode.parentNode.parentNode; if (row.nodeName == 'TBODY') { row = row.parentNode; } var pageName = row.querySelector('.mw-changeslist-title').attributes.title.nodeValue; el.removeChild(el.firstChild); // delete plain text var row_revid = row.getAttributeNode('data-mw-revid'); if (row_revid && row_revid.value) { // single edit diff el.appendChild(buildLink([row_revid.value], pageName)); }else{ // multiple edit diff var revisions = []; row.querySelectorAll('tr.mw-rcfilters-ui-highlights-enhanced-nested.mw-changeslist-edit').forEach(function(edit) {								var edit_revid = edit.getAttributeNode('data-mw-revid');								if (edit_revid && edit_revid.value) {									revisions[revisions.length] = edit_revid.value;								}							}); el.appendChild(buildLink(revisions, pageName)); }					}				}			);			document.querySelectorAll('tr.mw-changeslist-src-mw-new.mw-rcfilters-ui-highlights-enhanced-nested').forEach( function(row) { if (!row.querySelector('.mw-changeslist-diff')){ var el = row.querySelector('td.mw-enhanced-rc-nested'); var pageName = row.querySelector('.mw-enhanced-rc-time a').title; var targetNode = el.childNodes[3]; var row_revid = row.getAttributeNode('data-mw-revid').value; targetNode.parentNode.insertBefore(document.createTextNode(' | '), targetNode); targetNode.parentNode.insertBefore(buildLink([row_revid.value], pageName, 'prev'), targetNode); targetNode.parentNode.insertBefore(document.createTextNode(')'), targetNode);						targetNode.remove;					}				}			); function buildLink(revisions, page, label) { var link = document.createElement('a'); var from = revisions[revisions.length-1]; var to = revisions[0]; var href = '/wiki/' + page + '?diff=' + to; if (from == to) { if (!label){label = 'diff';} link.classList.add('mw-changeslist-diff'); } else { href = href + '&oldid=' + from; if (!label){label = revisions.length + ' changes';} link.classList.add('mw-changeslist-groupdiff'); }				link.href = href; link.title = page; link.innerHTML = label; return link; }		},		// Properly display diff for page creation edits newDiff: function { if (document.querySelector('#mw-diff-ntitle1 > 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;					}); }			}		}	};	betterDiff.init; });