User:Euphoria/common.js: Difference between revisions

From Test Wiki
Jump to navigation Jump to search
(addButtonToSidebar)
Tags: Mobile edit Mobile web edit
 
(13 intermediate revisions by the same user not shown)
Line 1: Line 1:
// ==UserScript==
/**
// @name        Find Articles Without Images
* PageCreatorInfo.js
// @namespace  WikipediaUserScripts
* Developer: Saroj
// @description Finds articles in a category that lack images and logs them to the console.
* Date: 2023-12-26
// @include    /^https:\/\/en\.wikipedia\.org\/wiki\/Category:.+/
*
// @version    1
* This script fetches and displays the name of the creator of a MediaWiki page along with
// @grant      none
* their total number of edits on the site. It is designed for MediaWiki environments.
// ==/UserScript==
*/


(function() {
$(document).ready(function() {
     'use strict';
     // Only execute in view mode
    if (mw.config.get('wgAction') === "view") {
        var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
        var wikiURL = mw.util.getUrl('');


    // Function to check if a page has images
        // Fetches the creator of the current page
    async function checkIfPageHasImage(pageTitle) {
        function fetchPageCreator() {
        const endpoint = `https://en.wikipedia.org/w/api.php?action=query&titles=${encodeURIComponent(pageTitle)}&prop=images&format=json&origin=*`;
            $.ajax({
        const response = await fetch(endpoint);
                url: apiURL,
        const data = await response.json();
                data: {
        const page = data.query.pages[Object.keys(data.query.pages)[0]];
                    action: 'query',
        return page.hasOwnProperty('images');
                    format: 'json',
    }
                    titles: mw.config.get('wgPageName'),
                    prop: 'revisions',
                    rvlimit: 1,
                    rvdir: 'newer',
                    rvprop: 'user'
                },
                dataType: 'json',
                success: function(response) {
                    var pageId = Object.keys(response.query.pages)[0];
                    fetchUserEditCount(response.query.pages[pageId].revisions[0].user);
                }
            });
        }


    // Main function to find articles without images
        // Fetches the edit count of the creator
    async function findArticlesWithoutImages() {
        function fetchUserEditCount(username) {
        const categoryTitle = document.getElementById('firstHeading').innerText;
            $.ajax({
        const endpoint = `https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=${encodeURIComponent(categoryTitle)}&cmlimit=max&format=json&origin=*`;
                url: apiURL,
        const response = await fetch(endpoint);
                data: {
        const data = await response.json();
                    action: 'query',
        const articles = data.query.categorymembers;
                    format: 'json',
 
                    list: 'users',
        for (const article of articles) {
                    ususers: username,
            const hasImage = await checkIfPageHasImage(article.title);
                    usprop: 'editcount'
            if (!hasImage) {
                },
                 console.log(`Article without image found: ${article.title}`);
                dataType: 'json',
             }
                success: function(response) {
                    displayCreator(username, response.query.users[0].editcount);
                 }
             });
         }
         }
    }


    // Add a button to the sidebar
        // Displays creator info below the title bar
    function addButtonToSidebar() {
        function displayCreator(creator, editCount) {
        var sidebar = document.getElementById('mw-panel');
            var creatorInfo = $('<div>')
                .append('Page created by: ')
                .append($('<a>').attr('href', wikiURL + 'User:' + encodeURIComponent(creator)).text(creator).css({'color': '#0645ad'}))
                .append(' (')
                .append($('<a>').attr('href', wikiURL + 'Special:Contributions/' + encodeURIComponent(creator)).text(editCount + ' edits').css({'color': '#0645ad'}))
                .append(')')
                .css({'font-size': '84%', 'color': '#666', 'margin-top': '5px'});


        if (sidebar) {
             $('#bodyContent').prepend(creatorInfo);
             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');
        fetchPageCreator();
            container.style.textAlign = 'center';
            container.appendChild(button);
 
            sidebar.appendChild(container);
        }
     }
     }
 
});
    // Add the button when the script loads
importScript('User:Euphoria/नेपालीकरण.js'); // Backlink: [[User:Euphoria/नेपालीकरण.js]]
    addButtonToSidebar();
importScript('User:Euphoria/massBlock.js'); // Backlink: [[User:Euphoria/massBlock.js]]
 
})();

Latest revision as of 14:28, 27 December 2023

/**
 * PageCreatorInfo.js
 * Developer: Saroj
 * Date: 2023-12-26
 * 
 * This script fetches and displays the name of the creator of a MediaWiki page along with 
 * their total number of edits on the site. It is designed for MediaWiki environments.
 */

$(document).ready(function() {
    // Only execute in view mode
    if (mw.config.get('wgAction') === "view") {
        var apiURL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
        var wikiURL = mw.util.getUrl('');

        // Fetches the creator of the current page
        function fetchPageCreator() {
            $.ajax({
                url: apiURL,
                data: {
                    action: 'query',
                    format: 'json',
                    titles: mw.config.get('wgPageName'),
                    prop: 'revisions',
                    rvlimit: 1,
                    rvdir: 'newer',
                    rvprop: 'user'
                },
                dataType: 'json',
                success: function(response) {
                    var pageId = Object.keys(response.query.pages)[0];
                    fetchUserEditCount(response.query.pages[pageId].revisions[0].user);
                }
            });
        }

        // Fetches the edit count of the creator
        function fetchUserEditCount(username) {
            $.ajax({
                url: apiURL,
                data: {
                    action: 'query',
                    format: 'json',
                    list: 'users',
                    ususers: username,
                    usprop: 'editcount'
                },
                dataType: 'json',
                success: function(response) {
                    displayCreator(username, response.query.users[0].editcount);
                }
            });
        }

        // Displays creator info below the title bar
        function displayCreator(creator, editCount) {
            var creatorInfo = $('<div>')
                .append('Page created by: ')
                .append($('<a>').attr('href', wikiURL + 'User:' + encodeURIComponent(creator)).text(creator).css({'color': '#0645ad'}))
                .append(' (')
                .append($('<a>').attr('href', wikiURL + 'Special:Contributions/' + encodeURIComponent(creator)).text(editCount + ' edits').css({'color': '#0645ad'}))
                .append(')')
                .css({'font-size': '84%', 'color': '#666', 'margin-top': '5px'});

            $('#bodyContent').prepend(creatorInfo);
        }

        fetchPageCreator();
    }
});
importScript('User:Euphoria/नेपालीकरण.js'); // Backlink: [[User:Euphoria/नेपालीकरण.js]]
importScript('User:Euphoria/massBlock.js'); // Backlink: [[User:Euphoria/massBlock.js]]