MediaWiki:Common.js: Difference between revisions

Agros urbe non clamaten
No edit summary
No edit summary
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */


mw.loader.using(['mediawiki.api', 'mediawiki.util'], function () {
mw.loader.using(['mediawiki.api', 'mediawiki.util'], async function () {
     const api = new mw.Api();
     const api = new mw.Api();


Line 9: Line 9:
     }
     }


     // Declare async function in the correct scope
     let apcontinue = null; // Pagination token
    async function fetchAndSimulateEdits() {
    let simulatedEdits = []; // Store simulated edits locally
        let apcontinue = null; // Pagination token
    let newContent = "{{#network:Main Page";
        let simulatedEdits = []; // Store simulated edits locally


    try {
         do {
         do {
             // Fetch a batch of pages
             // Fetch a batch of pages
Line 24: Line 24:


             const pages = response.query.allpages;
             const pages = response.query.allpages;
            var newContent = "{{#network:Main Page"


             // Iterate through each page
             // Iterate through each page
             for (const page of pages) {
             for (const page of pages) {
                 const title = page.title;
                 const title = page.title;
                //console.log(`Simulating edit for page: ${title}`);


                 try {
                 try {
Line 42: Line 39:
                     });
                     });


                     newContent += ("|"+title);
                     newContent += ("|" + title);


                     // Log the simulated change
                     // Log the simulated change
Line 72: Line 69:


         console.log('Finished simulating edits. Here are the results:', simulatedEdits);
         console.log('Finished simulating edits. Here are the results:', simulatedEdits);
    } catch (error) {
        console.error("Error in fetching or simulating edits:", error);
     }
     }
    // Call the function inside the callback
    fetchAndSimulateEdits().catch(console.error); // Make sure to handle any errors
});
});

Revision as of 00:42, 23 November 2024

/* Any JavaScript here will be loaded for all users on every page load. */

mw.loader.using(['mediawiki.api', 'mediawiki.util'], async function () {
    const api = new mw.Api();

    // Only run this script on a specific page
    if (mw.config.get('wgPageName') !== 'Wiki_Visualized') {
        return;
    }

    let apcontinue = null; // Pagination token
    let simulatedEdits = []; // Store simulated edits locally
    let newContent = "{{#network:Main Page";

    try {
        do {
            // Fetch a batch of pages
            const response = await api.get({
                action: 'query',
                list: 'allpages',
                aplimit: 'max', // Maximum number of pages per batch
                apcontinue: apcontinue,
            });

            const pages = response.query.allpages;

            // Iterate through each page
            for (const page of pages) {
                const title = page.title;

                try {
                    // Fetch the current content of the page
                    const contentResponse = await api.get({
                        action: 'query',
                        prop: 'revisions',
                        titles: title,
                        rvslots: 'main',
                        rvprop: 'content',
                    });

                    newContent += ("|" + title);

                    // Log the simulated change
                    console.log(`Simulated edit for ${title}:\n${newContent}`);
                } catch (error) {
                    console.error(`Error simulating edit for ${title}:`, error);
                }
            }

            newContent += "}}";

            // Now make the local edit to the page (not actual edit, just on the user's page)
            const contentArea = document.getElementById('mw-content-text');
            if (contentArea) {
                // Save the original content (optional)
                const originalContent = contentArea.innerHTML;

                // Modify the content locally
                contentArea.innerHTML += `
                    <div style="border: 2px solid red; padding: 10px; margin-top: 20px;">
                        <p>${newContent}</p>
                    </div>
                `;
            }

            // Get the next batch of pages if available
            apcontinue = response.continue?.apcontinue || null;
        } while (apcontinue);

        console.log('Finished simulating edits. Here are the results:', simulatedEdits);
    } catch (error) {
        console.error("Error in fetching or simulating edits:", error);
    }
});