User:Euphoria/common.js: Difference between revisions

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


// Function to fetch page creator
(function() {
function fetchPageCreator() {
'use strict';
$.ajax({

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

// Main function to find articles without images
},
dataType: 'json',
async function findArticlesWithoutImages() {
success: function(response) {
const categoryTitle = document.getElementById('firstHeading').innerText;
if (response.query && response.query.pages) {
const endpoint = `https://en.wikipedia.org/w/api.php?action=query&list=categorymembers&cmtitle=${encodeURIComponent(categoryTitle)}&cmlimit=max&format=json&origin=*`;
var pages = response.query.pages;
const response = await fetch(endpoint);
const data = await response.json();
var pageId = Object.keys(pages)[0];
if (pages[pageId].revisions && pages[pageId].revisions[0]) {
const articles = data.query.categorymembers;
var creator = pages[pageId].revisions[0].user;

displayCreator(creator);
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 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'
});


$('#firstHeading').prepend(creatorInfo); // Add the creator info to the top of the page
if (sidebar) {
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);
}
}
}


fetchPageCreator();
// Add the button when the script loads
});
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();
});