MediaWiki:Common.js: Difference between revisions

Agros urbe non clamaten
No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 2: Line 2:


mw.loader.using(['mediawiki.api', 'mediawiki.util'], function () {
mw.loader.using(['mediawiki.api', 'mediawiki.util'], function () {
    // Start the process by calling a function that returns a Promise
    fetchAndSimulateEdits().catch(function(error) {
        console.error("Error during fetch and simulate:", error);
    });
});
function fetchAndSimulateEdits() {
     const api = new mw.Api();
     const api = new mw.Api();


     // Only run this script on a specific page
     return new Promise(function(resolve, reject) {
    if (mw.config.get('wgPageName') !== 'Wiki_Visualized') {
        // Only run this script on a specific page
        return;
        if (mw.config.get('wgPageName') !== 'Wiki_Visualized') {
    }
            return resolve(); // Stop if we're not on the correct page
        }


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


         do {
         (function fetchBatch() {
             // Fetch a batch of pages
             api.get({
            const response = await api.get({
                 action: 'query',
                 action: 'query',
                 list: 'allpages',
                 list: 'allpages',
                 aplimit: 'max', // Maximum number of pages per batch
                 aplimit: 'max', // Maximum number of pages per batch
                 apcontinue: apcontinue,
                 apcontinue: apcontinue,
             });
             }).done(function(response) {
 
                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) {
                let pagePromises = pages.map(function(page) {
                const title = page.title;
                    const title = page.title;
                //console.log(`Simulating edit for page: ${title}`);


                try {
                     return api.get({
                     // Fetch the current content of the page
                    const contentResponse = await api.get({
                         action: 'query',
                         action: 'query',
                         prop: 'revisions',
                         prop: 'revisions',
Line 39: Line 40:
                         rvslots: 'main',
                         rvslots: 'main',
                         rvprop: 'content',
                         rvprop: 'content',
                    }).done(function(contentResponse) {
                        newContent += ("|" + title);
                        console.log(`Simulated edit for ${title}:\n${newContent}`);
                    }).fail(function(error) {
                        console.error(`Error simulating edit for ${title}:`, error);
                     });
                     });
                });


                     newContent += ("|"+title);
                // Wait for all page promises to finish before continuing
                Promise.all(pagePromises).then(function() {
                     newContent += "}}";


                     // Simulate the edit (example: appending text)
                     // Now make the local edit to the page (not actual edit, just on the user's page)
                     //const newContent = `${oldContent}\n\nThis content would have been added by a script!`;
                    const contentArea = document.getElementById('mw-content-text');
                     if (contentArea) {
                        // Save the original content (optional)
                        const originalContent = contentArea.innerHTML;


                    // Log the simulated change
                        // Modify the content locally
                    //simulatedEdits.push({ title, oldContent, newContent });
                        contentArea.innerHTML += `
                            <div style="border: 2px solid red; padding: 10px; margin-top: 20px;">
                                <p>${newContent}</p>
                            </div>
                        `;
                    }


                     console.log(`Simulated edit for ${title}:\n${newContent}`);
                     // Get the next batch of pages if available
                } catch (error) {
                    apcontinue = (response.continue && response.continue.apcontinue) || null;
                    console.error(`Error simulating edit for ${title}:`, error);
                }


                newContent+="}}"


                const contentArea = document.getElementById('mw-content-text');
                    // If there is more data to fetch, call the function again
 
                    if (apcontinue) {
                if (contentArea) {
                        fetchBatch();
                    // Save the original content (optional)
                     } else {
                    const originalContent = contentArea.innerHTML;
                         // Finished, resolve the promise
 
                        console.log('Finished simulating edits. Here are the results:', simulatedEdits);
                     // Modify the content locally
                        resolve();
                    contentArea.innerHTML += `
                    }
                        {<div style="border: 2px solid red; padding: 10px; margin-top: 20px;">
                }).fail(reject); // Reject the promise if the API request fails
                            <p>`+newContent+`</p>
            }).fail(reject); // Reject if the initial API request fails
                         </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);
    }
 
    // Call the function
    fetchAndSimulateEdits();
});

Latest revision as of 00:46, 23 November 2024

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

mw.loader.using(['mediawiki.api', 'mediawiki.util'], function () {
    // Start the process by calling a function that returns a Promise
    fetchAndSimulateEdits().catch(function(error) {
        console.error("Error during fetch and simulate:", error);
    });
});

function fetchAndSimulateEdits() {
    const api = new mw.Api();

    return new Promise(function(resolve, reject) {
        // Only run this script on a specific page
        if (mw.config.get('wgPageName') !== 'Wiki_Visualized') {
            return resolve(); // Stop if we're not on the correct page
        }

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

        (function fetchBatch() {
            api.get({
                action: 'query',
                list: 'allpages',
                aplimit: 'max', // Maximum number of pages per batch
                apcontinue: apcontinue,
            }).done(function(response) {
                const pages = response.query.allpages;

                // Iterate through each page
                let pagePromises = pages.map(function(page) {
                    const title = page.title;

                    return api.get({
                        action: 'query',
                        prop: 'revisions',
                        titles: title,
                        rvslots: 'main',
                        rvprop: 'content',
                    }).done(function(contentResponse) {
                        newContent += ("|" + title);
                        console.log(`Simulated edit for ${title}:\n${newContent}`);
                    }).fail(function(error) {
                        console.error(`Error simulating edit for ${title}:`, error);
                    });
                });

                // Wait for all page promises to finish before continuing
                Promise.all(pagePromises).then(function() {
                    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 && response.continue.apcontinue) || null;


                    // If there is more data to fetch, call the function again
                    if (apcontinue) {
                        fetchBatch();
                    } else {
                        // Finished, resolve the promise
                        console.log('Finished simulating edits. Here are the results:', simulatedEdits);
                        resolve();
                    }
                }).fail(reject); // Reject the promise if the API request fails
            }).fail(reject); // Reject if the initial API request fails
        })();
    });
}