User:Euphoria/batchCategoryAssignment.js

From Test Wiki
Jump to navigation Jump to search

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)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
(function() {
    'use strict';

    const openButton = document.createElement('button');
    openButton.innerText = 'Batch Category Assignment';
    openButton.style.position = 'fixed';
    openButton.style.top = '10px';
    openButton.style.right = '10px';
    openButton.style.zIndex = 1000;
    document.body.appendChild(openButton);

    const modal = document.createElement('div');
    modal.style.display = 'none';
    modal.style.position = 'fixed';
    modal.style.top = '50%';
    modal.style.left = '50%';
    modal.style.transform = 'translate(-50%, -50%)';
    modal.style.backgroundColor = 'white';
    modal.style.padding = '20px';
    modal.style.border = '1px solid black';
    modal.style.zIndex = 1001;
    document.body.appendChild(modal);

    const articleListTextarea = document.createElement('textarea');
    articleListTextarea.placeholder = 'Enter article titles, one per line';
    articleListTextarea.style.width = '100%';
    articleListTextarea.style.height = '100px';
    modal.appendChild(articleListTextarea);

    const categoryInput = document.createElement('input');
    categoryInput.type = 'text';
    categoryInput.placeholder = 'Enter category to assign';
    categoryInput.style.width = '100%';
    categoryInput.style.marginTop = '10px';
    modal.appendChild(categoryInput);

    const submitButton = document.createElement('button');
    submitButton.innerText = 'Assign Category';
    submitButton.style.marginTop = '10px';
    modal.appendChild(submitButton);

    const cancelButton = document.createElement('button');
    cancelButton.innerText = 'Cancel';
    cancelButton.style.marginTop = '10px';
    cancelButton.style.marginLeft = '10px';
    modal.appendChild(cancelButton);

    openButton.addEventListener('click', () => {
        modal.style.display = 'block';
    });

    cancelButton.addEventListener('click', () => {
        modal.style.display = 'none';
    });

    const checkExistence = async (title, type) => {
        const response = await fetch(`https://testwiki.wiki/w/api.php?action=query&titles=${encodeURIComponent(title)}&format=json&origin=*`);
        const data = await response.json();
        return !data.query.pages[-1];
    };

    const assignCategory = async () => {
        const articles = articleListTextarea.value.trim().split('\n');
        const category = categoryInput.value.trim();

        if (articles.length === 0 || category === '') {
            alert('Please enter both article titles and a category.');
            return;
        }

        if (!(await checkExistence(`Category:${category}`, 'category'))) {
            alert(`Category "${category}" does not exist.`);
            return;
        }

        for (const article of articles) {
            const title = article.trim();
            if (title === '') continue;

            if (!(await checkExistence(title, 'article'))) {
                console.warn(`Article "${title}" does not exist.`);
                continue;
            }

            try {
                const response = await fetch(`https://testwiki.wiki/w/api.php?action=edit&title=${encodeURIComponent(title)}&appendtext=[[Category:${category}]]&format=json`, {
                    method: 'POST',
                    credentials: 'include'
                });
                const data = await response.json();
                if (data.edit && data.edit.result === 'Success') {
                    console.log(`Category "${category}" assigned to ${title}`);
                } else {
                    console.error(`Failed to assign category to ${title}:`, data);
                }
            } catch (error) {
                console.error(`Error assigning category to ${title}:`, error);
            }
        }

        alert('Category assignment completed.');
        modal.style.display = 'none';
    };

    submitButton.addEventListener('click', assignCategory);
})();