User:Novem Linguae/Scripts/PurgeButton.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. |
Documentation for this user script can be added at User:Novem Linguae/Scripts/PurgeButton. |
// <nowiki>
// isolating this until I can debug it. Then I'll add it to NPPLinks.js
mw.loader.using( ['mediawiki.util', 'mediawiki.api'] ).then( function () {
return; // this code doesn't work because `require` is not available here.
// this code borrowed from https://www.mediawiki.org/wiki/API:Purge
var request = require('request').defaults({jar: true}),
url = "https://wiki.riteme.site/w/api.php";
function purge(title) {
var params = {
action: "purge",
titles: title,
format: "json"
};
request.post({ url: url, form: params }, function (error, res, body) {
if (error) {
return;
}
console.log(body);
});
}
// end borrowed code
function escapeDoubleQuotes(input) {
return input.replace(/"/g, '"');
}
let pageName = mw.config.get('wgPageName'); // has underscores instead of spaces. has namespace prefix
let underscores = encodeURIComponent(pageName);
let links = `<li><a onclick="purge("`+escapeDoubleQuotes(pageName)+`");">Purge (for orphan check)</a></li>`;
// using this ugly monstrosity because mw.util.addPortletLink does not support adding entire menus, and the parent script needs to add a menu
$('#p-navigation').after(`
<nav id="p-purge-button" class="mw-portlet mw-portlet-purge-button vector-menu vector-menu-portal portal" aria-labelledby="p-purge-button-label" role="purge-button">
<h3 id="p-purge-button-label" class="vector-menu-heading">
<span>Purge Test</span>
</h3>
<div class="vector-menu-content">
<ul class="vector-menu-content-list">
`+links+`
</ul>
</div>
</nav>
`);
});
// </nowiki>