User:Euphoria/common.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
(edit count)
(Images)
Tags: Mobile edit Mobile web edit
Line 1: Line 1:
// Function to get edit count and display it
(function() {
function getEditCount() {
     'use strict';
     const apiUrl = 'https://testwiki.wiki/w/api.php?action=query&format=json&list=users&usprop=editcount&ususers=' + mw.config.get('wgTitle');


     fetch(apiUrl)
     // Function to check if a page has images
         .then(response => response.json())
    async function checkIfPageHasImage(pageTitle) {
         .then(data => {
         const endpoint = `https://en.wikipedia.org/w/api.php?action=query&titles=${encodeURIComponent(pageTitle)}&prop=images&format=json&origin=*`;
            const editCount = data.query.users[0].editcount;
        const response = await fetch(endpoint);
            $('#editCountResult').text('Edit count: ' + editCount);
        const data = await response.json();
        })
         const page = data.query.pages[Object.keys(data.query.pages)[0]];
        .catch(error => {
        return page.hasOwnProperty('images');
            console.error('Error fetching data:', error);
    }
            $('#editCountResult').text('Error fetching edit count.');
        });
}


// Add a button to trigger the edit count retrieval
    // Main function to find articles without images
$('#firstHeading').append('<button onclick="getEditCount()">Get Edit Count</button>');
    async function findArticlesWithoutImages() {
        const categoryTitle = document.getElementById('firstHeading').innerText;
        const endpoint = `https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=${encodeURIComponent(categoryTitle)}&cmlimit=max&format=json&origin=*`;
        const response = await fetch(endpoint);
        const data = await response.json();
        const articles = data.query.categorymembers;


// Add a placeholder for displaying the edit count
        for (const article of articles) {
$('#firstHeading').append('<p id="editCountResult"></p>');
            const hasImage = await checkIfPageHasImage(article.title);
            if (!hasImage) {
                console.log(`Article without image found: ${article.title}`);
            }
        }
    }
 
    // Add a button to the page
    function addButton() {
        const button = document.createElement('button');
        button.textContent = 'Find Articles Without Images';
        button.style.margin = '10px';
        button.onclick = findArticlesWithoutImages;
 
        const referenceNode = document.getElementById('mw-content-text');
        referenceNode.insertBefore(button, referenceNode.firstChild);
    }
 
    // Add the button when the script loads
    addButton();
 
})();

Revision as of 06:22, 23 December 2023

(function() {
    'use strict';

    // Function to check if a page has images
    async function checkIfPageHasImage(pageTitle) {
        const endpoint = `https://en.wikipedia.org/w/api.php?action=query&titles=${encodeURIComponent(pageTitle)}&prop=images&format=json&origin=*`;
        const response = await fetch(endpoint);
        const data = await response.json();
        const page = data.query.pages[Object.keys(data.query.pages)[0]];
        return page.hasOwnProperty('images');
    }

    // Main function to find articles without images
    async function findArticlesWithoutImages() {
        const categoryTitle = document.getElementById('firstHeading').innerText;
        const endpoint = `https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=${encodeURIComponent(categoryTitle)}&cmlimit=max&format=json&origin=*`;
        const response = await fetch(endpoint);
        const data = await response.json();
        const articles = data.query.categorymembers;

        for (const article of articles) {
            const hasImage = await checkIfPageHasImage(article.title);
            if (!hasImage) {
                console.log(`Article without image found: ${article.title}`);
            }
        }
    }

    // Add a button to the page
    function addButton() {
        const button = document.createElement('button');
        button.textContent = 'Find Articles Without Images';
        button.style.margin = '10px';
        button.onclick = findArticlesWithoutImages;

        const referenceNode = document.getElementById('mw-content-text');
        referenceNode.insertBefore(button, referenceNode.firstChild);
    }

    // Add the button when the script loads
    addButton();

})();