User:Qwerfjkl/scripts/hiddenURLs.js
Appearance
< User:Qwerfjkl | 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. |
Documentation for this user script can be added at User:Qwerfjkl/scripts/hiddenURLs. |
// See User:Qwerfjkl/scripts/BareURLinline.js for credits & explanation.
// This is just a minor modification of that script to work on references like <ref>[https://example.com]</ref>.
// Most credits go to User:BrownHairedGirl.
const regexp = /(<ref[^>]*?>)\s*\[\s*(https?:[^>< \|\[\]]+\s*)\]\s*(<\s*\/\s*ref)/g;
const count = (str, regexp) => {
return ((str || '').match(regexp) || []).length
}
let hiddenURLs = count(oldText, regexp);
if (!hiddenURLs) return;
oldText=oldText.replaceAll(regexp, "$1$2$3")
// Now BareURLinline.js
function buiMonthyearDatestamp() {
var d = new Date();
var month = new Array();
month[0] = "January";
month[1] = "February";
month[2] = "March";
month[3] = "April";
month[4] = "May";
month[5] = "June";
month[6] = "July";
month[7] = "August";
month[8] = "September";
month[9] = "October";
month[10] = "November";
month[11] = "December";
var myyear = d.getFullYear();
let mydatestamp = month[d.getMonth()] + " " + myyear.toString();
return mydatestamp;
}
var edit_summary = " tag(s) using [[User:BrownHairedGirl/BareURLinline.js|a script]]. " +
"For other recently-tagged pages with [[WP:Bare URLs|bare URLs]], " +
"see [[:Category:Articles with bare URLs for citations from " + buiMonthyearDatestamp() + "]]";
var buiTagsAdded = 0;
var buPDFtagsAdded = 0;
var buimgtagsAdded = 0;
var buspreadsheettagsAdded = 0;
var wgPageName = title;
var text = oldText;
const PDFURLregex = /\.pdf$/i;
const spreadsheetURLregex = /\.(xlsx?|ods)$/i;
const imageURLregex = /\.(JPG|JPEG|JP2|JPX|JPE|JFIF|JIF|TIFF|TIF|GIF|BMP|PNG|APNG|MNG|PBM|PGM|PPM|PNM|WEBP|HDR|HEIF|HEIFS|HEIC|HEICS|AVCI|AVCS|AVIF|AVIFS)$/i;
var newText = text
.replace(/(?<begin><ref[^>]*?\>)\s*\[?\s*(?<url>https?:[^>< \|\[\]]+)\s*\]?\s*(?<end><\s*\/\s*ref)/gi,
function(match, p1, p2, p3) {
if (p2.match(PDFURLregex)) {
buPDFtagsAdded += 1;
return p1 + p2 + ' {{Bare URL PDF|date=' + buiMonthyearDatestamp() + '}}' + p3;
} else if (p2.match(imageURLregex)) {
buimgtagsAdded += 1;
return p1 + p2 + ' {{Bare URL image|date=' + buiMonthyearDatestamp() + '}}' + p3;
} else if (p2.match(spreadsheetURLregex)) {
buspreadsheettagsAdded += 1;
return p1 + p2 + ' {{Bare URL spreadsheet|date=' + buiMonthyearDatestamp() + '}}' + p3;
} else {
buiTagsAdded += 1;
return p1 + p2 + ' {{Bare URL inline|date=' + buiMonthyearDatestamp() + '}}' + p3;
}
}
);
if (text == newText) {
// nothing was changed
//alert("[[" + wgPageName + "]]: No untagged bare URLs");
return;
}
newText = newText.replaceAll("<br>", "<br />");
newText = newText.replaceAll("<br/>", "<br />");
var tagsAdded = buiTagsAdded + buPDFtagsAdded + buimgtagsAdded + buspreadsheettagsAdded;
if (true) {
var summaryExtra = "";
var mytagsummary = "";
if (buiTagsAdded > 0) {
mytagsummary = mytagsummary + buiTagsAdded + " {{[[Template:Bare URL inline|Bare URL inline]]}} ";
}
if (buPDFtagsAdded > 0) {
summaryExtra = summaryExtra + " and [[:Category:Articles with PDF format bare URLs for citations]]";
if (mytagsummary != "") {
mytagsummary = mytagsummary + "and ";
}
mytagsummary = mytagsummary + buPDFtagsAdded + " {{[[Template:Bare URL PDF|Bare URL PDF]]}} ";
}
if (buimgtagsAdded > 0) {
summaryExtra = summaryExtra + " and [[:Category:Articles with image file bare URLs for citations]]";
if (mytagsummary != "") {
mytagsummary = mytagsummary + "and ";
}
mytagsummary = mytagsummary + buimgtagsAdded + " {{[[Template:Bare URL image|Bare URL image]]}} ";
}
if (buspreadsheettagsAdded > 0) {
summaryExtra = summaryExtra + " and [[:Category:Articles with spreadsheet file bare URLs for citations]]";
if (mytagsummary != "") {
mytagsummary = mytagsummary + "and ";
}
mytagsummary = mytagsummary + buspreadsheettagsAdded + " {{[[Template:Bare URL spreadsheet|Bare URL spreadsheet]]}} ";
}
}
return {
text: newText,
summary: "Added " + mytagsummary + " " + edit_summary + summaryExtra
};