Jump to content

User:JJPMaster/raddewikify.js

From Wikibooks, open books for an open world
Note: After saving, changes may not occur immediately. Click here to learn how to bypass your browser's cache.
  • Mozilla / Firefox / Safari: hold down Shift while clicking Reload, or press Ctrl-Shift-R (Cmd-Shift-R on Apple Mac);
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5;
  • Konqueror: simply click the Reload button, or press F5;
  • Opera users may need to completely clear their cache in Tools→Preferences.
if (mw.config.get("wgArticleId") > 0)
var dewikifyLink = mw.util.addPortletLink('luna-actions', '#', 'Dewikify', 'luna-dewiki', 'Remove all links from this page');
$(dewikifyLink).click(() => {
	dewikify();
});
async function getNewContent() {
    var doIt = confirm("Are you sure you want to remove all of the links on this page?");
    var pageContent;
    if (doIt) {
        var params = {
            action: 'parse',
            page: mw.config.get('wgPageName'),
            prop: 'wikitext',
            format: 'json'
        };
        var dewikifiedContent;
        var api = new mw.Api();
        const data = await api.get(params);
        dewikifiedContent = data.parse.wikitext["*"].replace(/\[\[([^|\]]+)(?:\|[^|\]]+)?\]\]/gm, "$1");
        return dewikifiedContent;
    }
}
async function dewikify() {
	var params = {
		action: 'edit',
		title: mw.config.get('wgPageName'),
		text: await getNewContent(),
		summary: 'Dewikifying page using [[User:JJPMaster/raddewikify.js|script]]',
		format: 'json'
	};
	var api = new mw.Api();
	const data = await api.postWithToken("csrf", params);
	console.log(data);
	alert("Done!");
}