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);				//////////				// 				// TODO: add updating after RC reload, code below doesnt work but may serve as reference				// 					////////// 				// 					//	document.addEventListener('mouseup', function(event){ //		// console.log(event); //		if (				//			event.target &&				//			event.target.classList.contains('oo-ui-labelElement-label') &&				//			event.target.innerHTML.match(/^View new changes since/g)				//		) { //			setTimeout(newDiffLink, 7000); //		}				//	});				// 					//////////			}			// 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 {			console.log('newDiffLink triggered: ', this);			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, 0], pageName)); }else{ // multiple edit diff var revisions = []; row.querySelectorAll('tr.mw-rcfilters-ui-highlights-enhanced-nested.mw-changeslist-edit').forEach(function(edit) {								// console.log(edit); //debug								var edit_revid = edit.getAttributeNode('data-mw-revid');								if (edit_revid && edit_revid.value) {									revisions[revisions.length] = edit_revid.value;								}							}); // console.log(revisions); //debug 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]; // console.log(targetNode); var row_revid = row.getAttributeNode('data-mw-revid').value; targetNode.parentNode.insertBefore(document.createTextNode(' | '), targetNode); targetNode.parentNode.insertBefore(buildLink([row_revid.value, 0], pageName, 'prev'), targetNode); targetNode.parentNode.insertBefore(document.createTextNode(')'), targetNode);						targetNode.remove;					}				}			); function buildLink(revisions, page, label) { // console.log(revisions); //debug var link = document.createElement('a'); var from = revisions[revisions.length-1]; var to = revisions[0]; if (from == 0) { if (!label){label = 'diff';} link.classList.add('mw-changeslist-diff'); } else { if (!label){label = revisions.length + ' changes';} link.classList.add('mw-changeslist-groupdiff'); }				link.href = '/wiki/' + page + '?diff=' + to + '&oldid=' + from; 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')) { //Render proper diff¡ table.querySelector('.diff-ntitle').colSpan = 4; table.querySelector('.diff-notice').parentNode.remove; api.get(api_opt).then(function(data) {						console.log('api triggered: ', data);						table.innerHTML = '  ' + table.innerHTML;						table.querySelector('tbody').innerHTML = table.querySelector('tbody').innerHTML + data.compare.body;					}); }			}		}	};	betterDiff.init; });