User:BrandonXLF/CompareRevisions.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:BrandonXLF/CompareRevisions. |
/*** Compare Revisions ***/
// Adds a button to the diff page to compare two revisions side-by-side
// Documentation at [[en:w:User:BrandonXLF/CompareRevisions]]
// By [[en:w:User:BrandonXLF]]
$.when(mw.loader.using('oojs-ui'), $.ready).then(function() {
var cache = {};
mw.hook('wikipage.diff').add(function() {
var oldId = mw.config.get('wgDiffOldId'),
newId = mw.config.get('wgDiffNewId'),
bar = new OO.ui.ButtonSelectWidget({items: [
new OO.ui.ButtonOptionWidget({
label: 'Diff',
data: 'diff'
}),
new OO.ui.ButtonOptionWidget({
label: 'Compare',
data: 'compare'
}),
new OO.ui.ButtonOptionWidget({
label: 'Compare Wikitext',
data: 'wikitext'
})
]}),
compareArea = $('<div>').attr('id', 'comparerevisions-area');
if (!oldId || !newId) return;
function showComparison(cachePrefix, displayFunc, getFunc) {
var compareOld = $('<div>').addClass('comparerevisions-side'),
compareNew = $('<div>').addClass('comparerevisions-side');
function showRevision(revId, el) {
var cacheKey = cachePrefix + '-' + revId;
compareArea.append(
el.append(
$('<div>').css('text-align', 'center').text('Loading...')
)
);
if (cache[cacheKey]) {
displayFunc(el.empty(), cache[cacheKey], revId);
} else {
getFunc(revId).done(function(data) {
cache[cacheKey] = data;
displayFunc(el.empty(), cache[cacheKey], revId);
});
}
}
showRevision(oldId, compareOld);
showRevision(newId, compareNew);
}
$('.diff-title').after(
$('<tr>').append(
$('<td>').attr('colspan', '4').append(compareArea)
)
);
bar.$element.css({
float: 'left',
margin: $('.ve-init-mw-diffPage-diffMode').length ? '0 0 0 8px' : '8px 0'
});
if ($('.ve-init-mw-diffPage-diffMode').length) {
$('.ve-init-mw-diffPage-diffMode').append(bar.$element);
} else if ($('.mw-revslider-container').length) {
$('.mw-revslider-container').after(bar.$element);
} else {
$('#mw-content-text').prepend(bar.$element);
}
bar.on('select', function(e) {
mw.storage.set('comparerevisions-lastview', e.data);
compareArea.empty();
if (e.data == 'compare') {
showComparison(
'compare',
function(el, html, revId) {
el.html(html);
el.find('[for="toctogglecheckbox"]').attr('for', 'toctogglecheckbox-' + revId);
el.find('#toctogglecheckbox').attr('id', 'toctogglecheckbox-' + revId);
},
function(revId) {
return $.get(mw.config.get('wgScriptPath') + '/api.php', {
action: 'parse',
oldid: revId,
prop: 'text',
format: 'json'
}).then(function(res) {
return res.parse.text['*'];
});
}
);
} else if (e.data == 'wikitext') {
showComparison(
'wikitext',
function(el, text) {
el.append(
$('<pre></pre>').text(text)
);
},
function(revId) {
return $.get(mw.config.get('wgScript'), {
action: 'raw',
oldid: revId
});
}
);
}
});
bar.selectItemByData(mw.storage.get('comparerevisions-lastview') || 'diff');
});
mw.loader.addStyleTag(
'#comparerevisions-area { border-spacing: 0; }' +
'.comparerevisions-side { width: 50%; padding: 3px 10px; box-sizing: border-box; float: left; }'
);
});