User:Ahecht/Scripts/refrescue.js
Appearance
< User:Ahecht | Scripts
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 code will be executed when previewing this page.
Documentation for this user script can be added at User:Ahecht/Scripts/refrescue.
/******************************************************************************/
/* This script is designed to run from the browser console ([[HELP:CONSOLE]]) */
/* on both pages with errors about missing references and on the page where */
/* the references can be found. */
/* */
/* This script is particularly useful when splitting pages that use named */
/* named references. getNames() is run on the new page created after the */
/* split, and getRefs() is run on the source page (in edit mode). The list of */
/* references produced by getRefs() can be inserted into the new page as */
/* [[Help:List-defined references]]. */
/******************************************************************************/
// Run from the console on the page with missing reference errors
function getNames() {
const errorSpans = document.querySelectorAll('span.mw-ext-cite-error');
const refNames = [];
errorSpans.forEach(span => {
const codeTag = span.querySelector('code');
if (codeTag) {
const refName = codeTag.textContent.trim();
if (refName) {
refNames.push(refName);
}
}
});
console.log(refNames.join());
}
// Edit the page that has the references you need and run the following script
// in the console with the text editor window visible. You can pre-load the
// list of names with var refNames = 'comma,separated,list,of,names';
function getRefs() {
if (!refNames) {
const refNames = prompt('Please enter a comma-separated list of names:');
}
const refs = [];
const notFound = [];
const names = refNames.split(',').map(name => name.trim());
const textareaContent = document.getElementById('wpTextbox1').value;
const refRegex = /<ref name\s*=\s*['"]?([^'"\/>]+)['"]?\s*>(.*?)<\/ref>/gim;
names.forEach(name => {
let found = false;
let match;
while ((match = refRegex.exec(textareaContent)) !== null) {
if (match[1] == name) {
refs.push(match[0]);
found = true;
break;
}
}
if (found === false) {notFound.push(name)}
});
console.log(refs.join('\n'));
if (notFound.length > 0) {
console.log("The following references were not found:", notFound.join());
}
}