User:Novem Linguae/Scripts/PurgeButton.js

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// <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://en.wikipedia.org/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, '&quot;');
	}

	let pageName = mw.config.get('wgPageName'); // has underscores instead of spaces. has namespace prefix
	
	let underscores = encodeURIComponent(pageName);

	let links = `<li><a onclick="purge(&quot;`+escapeDoubleQuotes(pageName)+`&quot;);">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>