Jump to content

User:Ieditrandomarticles/bot.js

From Simple English Wikipedia, the free encyclopedia

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
mw.loader.using(['mediawiki.util', 'mediawiki.api'], function () {
    $(function () {
        const api = new mw.Api();

        function transformHeadings(wikitext) {
            let changed = false;
            const newText = wikitext.replace(/^(={2,})\s*External links\s*\1\s*$/gim, (_, equals) => {
                changed = true;
                return `${equals} Other websites ${equals}`;
            }).replace(/^(={2,})\s*See also\s*\1\s*$/gim, (_, equals) => {
                changed = true;
                return `${equals} Related pages ${equals}`;
            });
            return [changed, newText];
        }

        const box = $(`
            <div class="wikiHackWindow" style="top: 100px; left: 20px;">
                <div class="wikiHackHeader">Fix Headings (MoS)</div>
                <div class="wikiHackBody">
                    <div class="menu-item" id="headingFixerBtn">Replace bad headings</div>
                </div>
            </div>
        `);
        $('body').append(box);

        const header = box.find('.wikiHackHeader');
        let isDragging = false, offsetX, offsetY;
        header.on('mousedown', function (e) {
            isDragging = true;
            offsetX = e.clientX - box.offset().left;
            offsetY = e.clientY - box.offset().top;
            $('body').on('mousemove.wikiHackDrag', function (e) {
                if (isDragging) {
                    box.css({
                        left: (e.clientX - offsetX) + 'px',
                        top: (e.clientY - offsetY) + 'px'
                    });
                }
            }).on('mouseup.wikiHackDrag', function () {
                isDragging = false;
                $('body').off('.wikiHackDrag');
            });
        });

        $(document).on('keydown', function (e) {
            if (e.key === '`') {
                box.toggle();
            }
        });

        box.hide();

        $('#headingFixerBtn').on('click', async function () {
            const title = mw.config.get('wgPageName');
            const ns = mw.config.get('wgNamespaceNumber');

            if (ns !== 0) {
                alert("[WikiWare/HeaderMini] Not an article page! Only namespace 0 is supported.");
                return;
            }

            try {
                const data = await api.get({
                    action: 'query',
                    prop: 'revisions',
                    titles: title,
                    rvslots: 'main',
                    rvprop: 'content',
                    formatversion: 2
                });

                const content = data.query.pages[0].revisions[0].slots.main.content;
                const [changed, newText] = transformHeadings(content);

                if (!changed) {
                    console.log("[WikiWare/HeaderMini] No changable headings. Skipping to another...");
                    location.href = '/wiki/Special:Random';
                    return;
                }

                const token = await api.get({
                    action: 'query',
                    meta: 'tokens'
                });

                await api.postWithToken('csrf', {
                    action: 'edit',
                    title: title,
                    text: newText,
                    summary: 'Replaced section titles with simpler forms per [[WP:MOS]]',
                    tags: 'automated edit',
                    format: 'json'
                });

                alert("[WikiWare/HeaderMini] Headings fixed and saved.");
            } catch (e) {
                console.error("Error fixing headings:", e);
                alert("[WikiWare/HeaderMini] Failed Execution, Check console.");
            }
        });

        console.log("[WikiWare] MODULE HeaderMini loaded");
    });
});