User:BrandonXLF/ReferenceExpander.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/ReferenceExpander. |
// <nowiki>
/*** Reference Expander ***/
// Expands references that are a link to a expanded reference using {{cite ..}}
// [[User:BrandonXLF/ReferenceExpander]]
// By [[User:BrandonXLF]]
/* global getCitoidRef */
$(mw.util.addPortletLink('p-tb', '#', 'Expand references')).click(function(e) {
e.preventDefault();
var progress = new OO.ui.ProgressBarWidget({
progress: false
}),
doing = $('<div style="margin:0.5em 0;font-weight:bold;">Initalizing...</div>'),
logElement = $('<div style="font-family:monospace monospace;"></div>');
function log(msg, color) {
logElement.append($('<div>').text('> ' + msg).css({color: color || 'grey'}));
}
function ProcessDialog(config) {
ProcessDialog.super.call(this, config);
}
OO.inheritClass(ProcessDialog, OO.ui.ProcessDialog);
ProcessDialog.static.name = 'citoidExpandRefs';
ProcessDialog.static.title = 'Reference Expanders';
ProcessDialog.static.actions = [{
action: 'close',
label: 'Close',
flags: ['safe', 'close']
}];
ProcessDialog.prototype.initialize = function() {
ProcessDialog.super.prototype.initialize.apply(this, arguments);
this.content = new OO.ui.PanelLayout({
padded: true,
expanded: false
});
processDialog.content.$element.append(progress.$element, doing, logElement);
this.$body.append(this.content.$element);
};
ProcessDialog.prototype.getActionProcess = function() {
var dialog = this;
return new OO.ui.Process(function() {
dialog.close();
});
};
ProcessDialog.prototype.getBodyHeight = function() {
return this.content.$element.outerHeight(true);
};
var windowManager = new OO.ui.WindowManager();
$(document.body).append(windowManager.$element);
var processDialog = new ProcessDialog({
size: 'large'
});
windowManager.addWindows([processDialog]);
windowManager.openWindow(processDialog);
log('Loading page content and scripts...');
mw.loader.getScript('https://en.wikipedia.org/w/index.php?title=User:BrandonXLF/Citoid.js&action=raw&ctype=text/javascript').then(function() {
new mw.Api().edit(mw.config.get('wgPageName'), function(rev) {
var def = $.Deferred(),
refs = rev.content.match(/<ref.*?>.*?<\/ref>/g),
done = 0;
progress.setProgress(0);
doing.text('Expanding references...');
function afterRef(msg, color) {
done++;
log(msg, color);
progress.setProgress(done/refs.length*100);
processDialog.updateSize();
if (done >= refs.length) {
progress.setProgress(false);
doing.text('Saving...');
log('Saving changes to ' + mw.config.get('wgPageName') + '...');
processDialog.updateSize();
var pos = 0;
def.resolve({
text: rev.content.replace(/<ref.*?>.*?<\/ref>/g, function() {
return refs[pos++];
}),
summary: 'Expanding bare references using [[User:BrandonXLF/ReferenceExpander|ReferenceExpander]]'
});
}
}
function doRef(i) {
if (refs[i].match(/<ref.*?> *{{/)) {
afterRef('Skipping already expanded reference.');
return;
}
var parts = refs[i].match(/(<ref.*?>)(.*?)(<\/ref>)/);
parts[2] = parts[2].match(/\[?([^ \]]*)*\]?/)[1];
getCitoidRef(parts[2]).then(function(ref) {
refs[i] = parts[1] + ref + parts[3];
afterRef('Expanded reference to "' + parts[2].trim() + '".', 'green');
}, function() {
afterRef('Error expanding reference to "' + parts[2].trim() + '".', 'red');
});
}
if (refs) {
for (var i = 0; i < refs.length; i++) doRef(i);
} else {
log('No references found on the page.');
def.resolve({
text: rev.content
});
}
return def.promise();
}).then(function() {
progress.setProgress(100);
doing.text('Edit saved!');
log('Edit saved. Reloading...');
window.location.reload();
});
});
});