User:Swayam Agrahari/common.js

(function (mw, $) { "use strict";

$(document).ready(function {    // Create a new div element for the sidebar    var sidebar = $(' ', { class: 'customSidebar' }).appendTo('body');

var arrow = $(' ', {     class: 'toggleArrow',      text: '<'    }).appendTo('body');

sidebar.css({     'position': 'fixed',      'top': '20px',      'right': '0',      'width': '400px',      'max-height': '90vh', // Limit height to 90% of viewport height      'background-color': '#f0f0f0',      'padding': '20px',      'box-shadow': '2px 0 5px rgba(0,0,0,0.1)',      'z-index': '1000',      'overflow-y': 'scroll', // Allow vertical scrolling      'display': 'none'    });

arrow.css({     'position': 'fixed',      'top': '10px',      'right': '10px',      'padding': '10px 20px',      'background-color': '#000',      'color': '#fff',      'border': 'none',      'border-radius': '5px',      'cursor': 'pointer',      'z-index': '1001',      'font-size': '16px',      'font-weight': 'bold'    });

function toggleSidebar { sidebar.toggle; if (sidebar.is(':visible')) { arrow.text('>'); } else { arrow.text('<'); }   }

// Bind the toggle function to the arrow arrow.click(toggleSidebar);

// Fetch the label from Wikidata API and then fetch the infobox from Wikipedia API const id = mw.config.get("wgPageName"); // Replace with the desired Wikidata ID   const wikidataUrl = `https://www.wikidata.org/w/api.php?action=wbgetentities&format=json&ids=${id}&formatversion=2&origin=*`;

fetch(wikidataUrl) .then(response => response.json) .then(data => {       const label = data.entities[id].labels.en.value;

const wikiUrl = `https://en.wikipedia.org/w/api.php?action=parse&page=${label}&format=json&origin=*`; return fetch(wikiUrl); })     .then(response => response.json)      .then(data => { const parsedHTML = data.parse.text['*'];

// Create a jQuery object from the parsed HTML const $parsedHTML = $(parsedHTML);

// Find the table with class "infobox" and append it to the sidebar const infoboxTable = $parsedHTML.find('.infobox'); sidebar.append(infoboxTable); })     .catch(error => console.error("Error fetching data:", error));  }); }(mediaWiki, jQuery));