Jump to content

User:Anne drew/gigawatch.js

From Wikipedia, the free encyclopedia
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.
/* Watch/unwatch all pages (up to 500) in a category */

// @param maxPages Number of pages to watch/unwatch
// @param act Action: watch or unwatch
function watchPages(maxPages, act) {
    // Make the ajax request to fetch category members
    $.ajax({
        url: mw.util.wikiScript('api'),
        data: {
            action: 'query',
            list: 'categorymembers',
            format: 'json',
            cmtitle: mw.config.get('wgPageName'),
            formatversion: 2,
            cmprop: 'title',
            cmtype: 'page',
            cmlimit: maxPages
        },
        success: function (data) {
            const categoryMembers = data.query.categorymembers;
            // Check if we got anything
            if (!categoryMembers) {
                console.log('none found');
            } else {
                let result = [];
                for (let i = 0; i < categoryMembers.length; i++) {
                    result.push(categoryMembers[i].title);
                }
                const api = new mw.Api();
                const apiCalls = [];

                // Post the watch request
                while (result.length) {
                    const sliced = result.slice(0, 50);
                    apiCalls.push(api.postWithToken('watch', {
                        action: 'watch',
                        titles: sliced.join('|'),
                        formatversion: 2,
                        format: 'json',
                        ...(act === 'unwatch' && { unwatch: 'true' })
                    }));
                    result = result.slice(50);
                }
                Promise.all(apiCalls).then(() => {
                    alert("Done!");
                    if (act === 'watch') {
                        $('#gigawatch').addClass('watched');
                    }
                    else {
                        $('#gigawatch').removeClass('watched');
                    }
                });
            }
        }
    });
}

$(document).ready(function () {
    if (mw.config.get('wgCanonicalNamespace') == 'Category') {
        $('#p-cactions').find('ul').append(
            "<li><a title=\"Watch all pages in this category\" href=\"#\" id=\"gigawatch\">Watch all</a></li>\n"
        );
        $('#gigawatch').click(function () {
            const maxPages = 500;
            if ($('#gigawatch').hasClass('watched')) {
                if (confirm('Are you sure you want to unwatch all pages in this category? (Restricted to top 500 pages only)')) {
                    watchPages(maxPages, 'unwatch');
                }
            } else {
                if (confirm('Are you sure you want to watch all pages in this category? (Restricted to top 500 pages only)')) {
                    watchPages(maxPages, 'watch');
                }
            }
        });
    }
});