User:Beao/HotTemplate.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
Documentation for this user script can be added at User:Beao/HotTemplate. |
(function ($, mw) {
"use strict";
const hideTemplates = [
"Own",
];
// Check if in File namespace
if (mw.config.get("wgCanonicalNamespace") !== "File") {
return;
}
// Check if on edit page
if (mw.config.get("wgAction") !== "view") {
return;
}
const pageName = mw.config.get("wgPageName");
mw.loader.using(["mediawiki.util", "mediawiki.api"]).then(function () {
// Get wikitext of current page
const api = new mw.Api();
api.get({
action: "query",
prop: "revisions",
rvprop: "content",
titles: pageName
}).done((data) => {
// Get wikitext of current page
const wikitext = data.query.pages[Object.keys(data.query.pages)[0]].revisions[0]["*"];
console.log(wikitext);
// Regex for simple templates
const templateRegex = /\{\{([a-z ]+)\}\}/gi;
let templates = [];
// Check if wikitext contains any simple templates
if (templateRegex.test(wikitext)) {
const matches = wikitext.match(templateRegex);
for (const match of matches) {
const templateName = match.replace(/\{\{|\}\}/g, "");
templates.push({
name: templateName,
canonicalName: templateName
});
}
}
// Use only accepted templates
templates = templates.filter(x => !hideTemplates.includes(x.canonicalName));
// Stop if no templates found
if (templates.length === 0) {
return;
}
// Check for redirected templates
api.get({
action: "query",
titles: templates.map(x => "Template:" + x.name).join("|")
}).done((data) => {
console.log(data);
for (const page of data.query.normalized || []) {
const template = templates.find(x => x.name === page.from.replace("Template:", ""));
if (template) {
template.canonicalName = page.to.replace("Template:", "");
}
}
// Use only accepted templates
templates = templates.filter(x => !hideTemplates.includes(x.canonicalName));
// Stop if no templates found
if (templates.length === 0) {
return;
}
// Add to category listing with Templates: label
const container = $("<div>")
.css("font-size", "87% !important")
.css("border", "1px solid #aaa")
.css("padding", "0.5em")
.css("margin", "0.5em 0");
container.text("Templates: ")
const list = $("<ul>");
for (const template of templates) {
const li = $("<li>");
li.append($("<a>").attr("href", mw.util.getUrl("Template:" + template.canonicalName)).text(template.canonicalName));
li.append(" ");
li.append($("<a>").text("(-)").click(function () {
api.edit(pageName, function (revision) {
return {
text: revision.content
.replace(new RegExp("\{\{" + template.name + "\}\}\s*(\n?)", "gi"), "$1")
.replace(/^\s*\n|\n\s*$/g, ""),
summary: "Removing template " + template.canonicalName + " with HotTemplate.js",
minor: true,
}
}).then(function () {
window.onbeforeunload = null;
window.location.reload();
});
}));
list.append(li);
}
container.append(list);
$("#catlinks").append(container);
});
});
});
})(jQuery, mw);