User:Guywan/Scripts/Headings.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:Guywan/Scripts/Headings. |
// [[Category:Wikipedia scripts]]
// <nowiki>
$(function()
{
if(mw.config.get("wgAction") != "view") return;
const VERSION = "03.06.19";
var level = "==";
// Setup links.
$("#p-cactions ul").append("<li id='ca-hm'><a href='#' title='Convert pseudo-headings to headings'>PH2H</a></li>");
$("#ca-hm").on("click", function()
{
mw.notify("Starting conversion process.", "info");
$.ajax(
{
url : mw.util.getUrl(mw.config.get("wgPageName")) + "?action=raw", dataType : "text"
})
.fail(function(result) { mw.notify("Failed to load: " + result, "error"); })
.done(function(text)
{
// Split the page into lines of text.
text = text.split("\n");
// Run a regex (or few) over each line.
var modified = false;
for(var i = 0; i < text.length; i++)
{
if(text[i].match(/=====[^=]*=====/g))
{
level = "======";
}
else if(text[i].match(/====[^=]*====/g))
{
level = "=====";
}
else if(text[i].match(/===[^=]*===/g))
{
level = "====";
}
else if(text[i].match(/==[^=]*==/g))
{
level = "===";
}
else
{
// Don't touch definition lists ...
if(text[i].match(/^;.*/g) && !text[i + 1].match(/^:.*/g))
{
text[i] = text[i].replace(/^;.*/g, hm_replace);
modified = true;
}
}
}
// If nothing is modified, abort.
if(!modified)
{
mw.notify("There are no pseudo-headings to convert!", "warn");
return;
}
mw.notify("Completed. Showing preview.");
// Put it back together!
text = text.join("\n");
// Setup the preview.
new mw.Api().parse(text)
.fail(function(result) { mw.notify("Failed to parse preview: " + result, "error"); })
.done(function(data)
{
$("#mw-content-text").html(data).prepend("<div style='border-bottom:1px solid #a2a9b1;text-align:center'>"
+ "<h3>This is a <strong>preview</strong> of the changes you will make.</h3><br/>"
+ "<button id='hd-submit' class='mw-ui-button mw-ui-progressive'>Submit</button>"
+ "<button id='hd-cancel' class='mw-ui-button mw-ui-quiet mw-ui-destructive'>Cancel</button></div>");
$("#hd-submit").on("click", function()
{
// Post edited text.
api.post(
{
"action" : "edit",
"title" : mw.config.get("wgPageName"),
"text" : text,
"summary" : "Replacing pseudo-headings with headings using [[User:Guywan/Scripts/Headings.js|Headmaster]] V" + VERSION + ".",
"minor" : true,
"token" : mw.user.tokens.get("editToken")
})
.fail(function(result) { mw.notify("Failed to post: " + result, "error"); })
.done(function()
{
mw.notify("Success! Refreshing.", "info");
window.location.href = mw.util.getUrl(mw.config.get("wgPageName"));
});
});
$("#hd-cancel").on("click", function()
{
window.location.href = mw.util.getUrl(mw.config.get("wgPageName"));
});
});
});
});
function hm_replace(match)
{
// If there is a trailing ':', destroy it!
if(match[match.length - 1] == ":")
{
return level + " " + match.substring(1, match.length - 1) + " " + level;
}
return level + " " + match.substr(1) + " " + level;
}
});
// </nowiki>