User:Enterprisey/fancy-diffs.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | This user script seems to have a documentation page at User:Enterprisey/fancy-diffs. |
// vim: ts=4 sw=4 et ai
( function () {
var api;
function makeExpand( type, name ) {
return '<span class="fd-expand" data-' + type + '="' + name.replace( /"/g, """ ) + '">(show)</span>';
}
function processText( text ) {
return text
.replace( /\[\[(.+?)(?:\|.+?)?\]\]/g, function ( match, name ) {
var html = "<a href='" + mw.util.getUrl( name ) + "'>" + match +
"</a>";
if( name.indexOf( "File:" ) === 0 ||
name.indexOf( "Image:" ) === 0 ) {
html += makeExpand( "img", name );
}
return html;
})
.replace( /\{\{(.+?)(?:\|.+?)?\}\}/g, function ( match, name ) {
var fullName = name;
if( name.indexOf( "#" ) === 0 ) {
fullName = name.replace( /^#invoke:/, "Module:" );
} else if( name.indexOf( ":" ) < 0 ) {
fullName = "Template:" + name;
}
return "{{<a href='" + mw.util.getUrl( fullName ) + "'>" + name + "</a>" + match.substring( 2 + name.length );
})
//.replace( /<ref\s+name\s*=\s*(\w+?|"[^"]+?")\s*\/>/g, function ( match, name ) {
// name = name.indexOf( '"' ) === 0 ? name.substring( 1, name.length - 1 ) : name;
// return match.substring( 0, match.length - 6 ) + makeExpand( "ref", name ) + "/>";
//})
.replace( /(?:(?:https|http|gopher|irc|ircs|ftp|news|nnttp|worldwind|telnet|svn|git|mms):\/\/|mailto:)([!#$&-;=?-\[\]_a-z~]|%[0-9a-fA-F]{2})+/g, function ( match ) {
return '<a href="' + match + '">' + match + '</a>';
});
}
function processDiff( diffTable ) {
var rows = diffTable.querySelectorAll( "tr" );
rowLoop:
for( var rowIdx = 0, numRows = rows.length; rowIdx < numRows; rowIdx++ ) {
var row = rows[rowIdx];
if( row.tagName.toLowerCase() === "colgroup" ) {
return;
}
for( var cellIdx = 0, numCells = row.children.length; cellIdx < numCells; cellIdx++ ) {
var td = row.children[cellIdx];
switch( td.className ) {
case "diff-context":
if( td.children && td.children.length ) {
var text = processText( td.children[0].innerHTML );
td.children[0].innerHTML = text;
row.children[cellIdx + 2].innerHTML = text;
continue rowLoop;
}
break;
case "diff-deletedline":
case "diff-addedline":
if( td.children && td.children.length ) {
td.children[0].innerHTML = processText( td.children[0].innerHTML );
}
break;
}
}
}
var expandSpans = diffTable.querySelectorAll( "span.fd-expand" );
for( var spanIdx = 0, numSpans = expandSpans.length; spanIdx < numSpans; spanIdx++ ) {
var span = expandSpans[spanIdx];
span.addEventListener( "click", function () {
if( !span.nextElementSibling || span.nextElementSibling.tagName.toLowerCase() !== "div" || span.nextElementSibling.className !== "fd-img" ) {
api.get( {
action: "query",
titles: span.dataset.img,
prop: "imageinfo",
iiprop: "url"
} ).done( function ( data ) {
if( data.query && data.query.pages ) {
var url = Object.values( data.query.pages )[0].imageinfo[0].url;
var div = document.createElement( "div" );
div.className = "fd-img";
var img = document.createElement( "img" );
img.src = url;
div.appendChild( img );
span.parentNode.insertBefore( div, span.nextSibling );
}
} );
span.textContent = "(hide)";
} else {
if( span.nextElementSibling.style.display === "none" ) {
span.nextElementSibling.style.display = "";
span.textContent = "(hide)";
} else {
span.nextElementSibling.style.display = "none";
span.textContent = "(show)";
}
}
} );
}
}
$.when(
$.ready,
mw.loader.using( [ "mediawiki.api", "mediawiki.util" ] )
).then( function () {
var table = document.querySelector( "table.diff" );
if( table ) {
api = new mw.Api();
mw.util.addCSS( ".fd-expand { cursor: pointer; text-decoration: underline; }" );
processDiff( table );
}
} );
} )();