Jump to content

User:Ieditrandomarticles/disambig.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.api', 'mediawiki.util'], function () {
    const api = new mw.Api();
    const links = document.querySelectorAll('#bodyContent a[href^="/wiki/"]:not([href*=":"])');
    const titles = Array.from(links).map(link => decodeURIComponent(link.getAttribute('href').replace('/wiki/', '')).replace(/#/g, '%23'));
    const disambigMap = new Map();
    const selectedTargets = new Map();

    function createDropdown(disambigTitle, linkElem) {
        const dropdown = document.createElement('select');
        dropdown.innerHTML = `<option value="">Select target...</option>`;
        dropdown.style.marginLeft = '8px';

        dropdown.addEventListener('change', () => {
            if (dropdown.value) {
                selectedTargets.set(disambigTitle, dropdown.value);
                checkIfAllSelected();
            }
        });

        linkElem.parentNode.insertBefore(dropdown, linkElem.nextSibling);
    }

    function checkIfAllSelected() {
        if (disambigMap.size === selectedTargets.size) {
            showCheckmark();
        }
    }

    function showCheckmark() {
        const check = document.createElement('div');
        check.textContent = '✔';
        check.style.position = 'fixed';
        check.style.bottom = '10px';
        check.style.left = '10px';
        check.style.fontSize = '24px';
        check.style.background = '#c8e6c9';
        check.style.padding = '8px';
        check.style.borderRadius = '50%';
        check.style.cursor = 'pointer';
        check.title = 'Submit edit';

        check.addEventListener('click', () => {
            const summary = 'Fixed disambiguation page links. [[User:Ieditrandomarticles/tools]]';
            const edits = Array.from(selectedTargets.entries()).map(([title, target]) => `* [[${title}]] → [[${target}]]`).join('\n');

            api.postWithToken('csrf', {
                action: 'edit',
                title: mw.config.get('wgPageName'),
                appendtext: `\n\n${edits}`,
                summary: summary,
                format: 'json'
            }).done(() => {
                alert('Submission sent!');
            });
        });

        document.body.appendChild(check);
    }

    function processDisambigs(batch, start = 0) {
        const slice = batch.slice(start, start + 50);
        api.get({
            action: 'query',
            titles: slice.join('|'),
            prop: 'categories',
            cllimit: 'max',
            format: 'json'
        }).then(response => {
            for (const pageId in response.query.pages) {
                const page = response.query.pages[pageId];
                if (!page.categories) continue;

                const isDisambig = page.categories.some(cat => cat.title === 'Category:Disambiguation pages');
                if (isDisambig) {
                    const selector = `a[href="/wiki/${encodeURIComponent(page.title).replace(/%20/g, '_')}"]`;
                    const matches = document.querySelectorAll(selector);
                    disambigMap.set(page.title, matches);

                    matches.forEach(link => {
                        link.style.backgroundColor = '#fff59d';
                        link.title += ' (disambiguation)';
                        createDropdown(page.title, link);
                    });
                }
            }

            if (start + 50 < batch.length) {
                setTimeout(() => processDisambigs(batch, start + 50), 500);
            }
        });
    }

    processDisambigs(titles);
});