User:Euphoria/common.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
(addButtonToSidebar)
Tags: Mobile edit Mobile web edit
(PageCreatorInfo)
Line 1: Line 1:
// ==UserScript==
// This script assumes you are on a Wikimedia page and have jQuery available
// @name        Find Articles Without Images
$(document).ready(function() {
// @namespace  WikipediaUserScripts
     var pageTitle = mw.config.get('wgPageName'); // Get the current page title
// @description Finds articles in a category that lack images and logs them to the console.
     var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
// @include     /^https:\/\/en\.wikipedia\.org\/wiki\/Category:.+/
// @version     1
// @grant      none
// ==/UserScript==


(function() {
     // Function to fetch page creator
    'use strict';
     function fetchPageCreator() {
 
         $.ajax({
     // Function to check if a page has images
            url: apiURL,
     async function checkIfPageHasImage(pageTitle) {
            data: {
         const endpoint = `https://en.wikipedia.org/w/api.php?action=query&titles=${encodeURIComponent(pageTitle)}&prop=images&format=json&origin=*`;
                action: 'query',
        const response = await fetch(endpoint);
                format: 'json',
        const data = await response.json();
                titles: pageTitle,
        const page = data.query.pages[Object.keys(data.query.pages)[0]];
                prop: 'revisions',
        return page.hasOwnProperty('images');
                rvlimit: 1,
    }
                rvdir: 'newer',
 
                rvprop: 'user'
    // Main function to find articles without images
            },
    async function findArticlesWithoutImages() {
            dataType: 'json',
        const categoryTitle = document.getElementById('firstHeading').innerText;
            success: function(response) {
        const endpoint = `https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=${encodeURIComponent(categoryTitle)}&cmlimit=max&format=json&origin=*`;
                if (response.query && response.query.pages) {
        const response = await fetch(endpoint);
                    var pages = response.query.pages;
        const data = await response.json();
                    var pageId = Object.keys(pages)[0];
        const articles = data.query.categorymembers;
                    if (pages[pageId].revisions && pages[pageId].revisions[0]) {
 
                        var creator = pages[pageId].revisions[0].user;
        for (const article of articles) {
                        displayCreator(creator);
            const hasImage = await checkIfPageHasImage(article.title);
                    }
            if (!hasImage) {
                 }
                 console.log(`Article without image found: ${article.title}`);
             }
             }
         }
         });
     }
     }


     // Add a button to the sidebar
     // Function to display the creator
     function addButtonToSidebar() {
     function displayCreator(creator) {
         var sidebar = document.getElementById('mw-panel');
         var creatorInfo = $('<div>')
            .text('Page created by: ' + creator)
            .css({
                'font-weight': 'bold',
                'margin-bottom': '10px'
            });


         if (sidebar) {
         $('#firstHeading').prepend(creatorInfo); // Add the creator info to the top of the page
            var button = document.createElement('button');
            button.textContent = 'Find Articles Without Images';
            button.style.margin = '5px';
            button.style.width = '90%';
            button.onclick = findArticlesWithoutImages;
 
            var container = document.createElement('div');
            container.style.textAlign = 'center';
            container.appendChild(button);
 
            sidebar.appendChild(container);
        }
     }
     }


     // Add the button when the script loads
     fetchPageCreator();
    addButtonToSidebar();
});
 
})();

Revision as of 09:01, 26 December 2023

// This script assumes you are on a Wikimedia page and have jQuery available
$(document).ready(function() {
    var pageTitle = mw.config.get('wgPageName'); // Get the current page title
    var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';

    // Function to fetch page creator
    function fetchPageCreator() {
        $.ajax({
            url: apiURL,
            data: {
                action: 'query',
                format: 'json',
                titles: pageTitle,
                prop: 'revisions',
                rvlimit: 1,
                rvdir: 'newer',
                rvprop: 'user'
            },
            dataType: 'json',
            success: function(response) {
                if (response.query && response.query.pages) {
                    var pages = response.query.pages;
                    var pageId = Object.keys(pages)[0];
                    if (pages[pageId].revisions && pages[pageId].revisions[0]) {
                        var creator = pages[pageId].revisions[0].user;
                        displayCreator(creator);
                    }
                }
            }
        });
    }

    // Function to display the creator
    function displayCreator(creator) {
        var creatorInfo = $('<div>')
            .text('Page created by: ' + creator)
            .css({
                'font-weight': 'bold',
                'margin-bottom': '10px'
            });

        $('#firstHeading').prepend(creatorInfo); // Add the creator info to the top of the page
    }

    fetchPageCreator();
});