This is an old revision of this page, as edited by Polygnotus(talk | contribs) at 12:03, 9 July 2025(←Created page with '// Wikipedia API Explorer Module const WikipediaAPI = { // Fetch Wikipedia userinfo data async fetchUserInfo() { const params = { action: 'query', meta: 'userinfo', uiprop: 'options', format: 'json', origin: '*' }; const url = 'https://en.wikipedia.org/w/api.php?' + new URLSearchParams(params); try { const response = await fetch(...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.Revision as of 12:03, 9 July 2025 by Polygnotus(talk | contribs)(←Created page with '// Wikipedia API Explorer Module const WikipediaAPI = { // Fetch Wikipedia userinfo data async fetchUserInfo() { const params = { action: 'query', meta: 'userinfo', uiprop: 'options', format: 'json', origin: '*' }; const url = 'https://en.wikipedia.org/w/api.php?' + new URLSearchParams(params); try { const response = await fetch(...')
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Wikipedia API Explorer ModuleconstWikipediaAPI={// Fetch Wikipedia userinfo dataasyncfetchUserInfo(){constparams={action:'query',meta:'userinfo',uiprop:'options',format:'json',origin:'*'};consturl='https://en.wikipedia.org/w/api.php?'+newURLSearchParams(params);try{constresponse=awaitfetch(url);if(!response.ok){thrownewError(`HTTP error! status: ${response.status}`);}returnawaitresponse.json();}catch(error){console.error('Wikipedia API error:',error);throwerror;}},// Create JSON tree viewercreateJSONTree(containerId,data){constcontainer=document.getElementById(containerId);if(!container){console.error(`Container with id '${containerId}' not found`);return;}// Clear containercontainer.innerHTML='';// Create tree structureconsttree=document.createElement('div');tree.className='json-tree';tree.appendChild(this.createNode('root',data,0));container.appendChild(tree);// Add styles if not already presentthis.addStyles();},// Create individual nodecreateNode(key,value,depth){constnode=document.createElement('div');node.className='json-node';if(value===null){node.innerHTML=`<span class="json-key">${key}:</span> <span class="json-null">null</span>`;}elseif(typeofvalue==='string'){node.innerHTML=`<span class="json-key">${key}:</span> <span class="json-string">"${this.escapeHtml(value)}"</span>`;}elseif(typeofvalue==='number'){node.innerHTML=`<span class="json-key">${key}:</span> <span class="json-number">${value}</span>`;}elseif(typeofvalue==='boolean'){node.innerHTML=`<span class="json-key">${key}:</span> <span class="json-boolean">${value}</span>`;}elseif(Array.isArray(value)){consttoggleId=`toggle-${Date.now()}-${Math.random()}`;consttoggle=document.createElement('span');toggle.className='json-toggle';toggle.textContent='▼';toggle.id=toggleId;constkeySpan=document.createElement('span');keySpan.className='json-key';keySpan.textContent=`${key}:`;constsummary=document.createElement('span');summary.className='json-collapsed';summary.textContent=` [${value.length} items]`;node.appendChild(toggle);node.appendChild(keySpan);node.appendChild(summary);constchildren=document.createElement('div');children.className='json-children';value.forEach((item,index)=>{children.appendChild(this.createNode(`[${index}]`,item,depth+1));});node.appendChild(children);toggle.onclick=()=>this.toggleNode(toggleId);}elseif(typeofvalue==='object'){constkeys=Object.keys(value);consttoggleId=`toggle-${Date.now()}-${Math.random()}`;consttoggle=document.createElement('span');toggle.className='json-toggle';toggle.textContent='▼';toggle.id=toggleId;constkeySpan=document.createElement('span');keySpan.className='json-key';keySpan.textContent=`${key}:`;constsummary=document.createElement('span');summary.className='json-collapsed';summary.textContent=` {${keys.length} properties}`;node.appendChild(toggle);node.appendChild(keySpan);node.appendChild(summary);constchildren=document.createElement('div');children.className='json-children';keys.forEach(objKey=>{children.appendChild(this.createNode(objKey,value[objKey],depth+1));});node.appendChild(children);toggle.onclick=()=>this.toggleNode(toggleId);}returnnode;},// Toggle node visibilitytoggleNode(toggleId){consttoggle=document.getElementById(toggleId);constchildren=toggle.parentNode.querySelector('.json-children');if(children.classList.contains('hidden')){children.classList.remove('hidden');toggle.textContent='▼';}else{children.classList.add('hidden');toggle.textContent='▶';}},// Expand all nodesexpandAll(containerId){constcontainer=document.getElementById(containerId);consttoggles=container.querySelectorAll('.json-toggle');toggles.forEach(toggle=>{constchildren=toggle.parentNode.querySelector('.json-children');if(children){children.classList.remove('hidden');toggle.textContent='▼';}});},// Collapse all nodescollapseAll(containerId){constcontainer=document.getElementById(containerId);consttoggles=container.querySelectorAll('.json-toggle');toggles.forEach(toggle=>{constchildren=toggle.parentNode.querySelector('.json-children');if(children){children.classList.add('hidden');toggle.textContent='▶';}});},// Escape HTMLescapeHtml(text){constdiv=document.createElement('div');div.textContent=text;returndiv.innerHTML;},// Add required CSS stylesaddStyles(){if(document.getElementById('json-tree-styles'))return;conststyle=document.createElement('style');style.id='json-tree-styles';style.textContent=` .json-tree { font-family: 'Courier New', monospace; background: #f8f9fa; border: 1px solid #e1e5e9; border-radius: 5px; padding: 15px; margin: 10px 0; overflow-x: auto; } .json-node { margin: 2px 0; } .json-key { color: #0645ad; font-weight: bold; } .json-string { color: #d73a49; } .json-number { color: #005cc5; } .json-boolean { color: #6f42c1; } .json-null { color: #6a737d; } .json-toggle { cursor: pointer; user-select: none; display: inline-block; width: 20px; color: #666; font-weight: bold; } .json-toggle:hover { color: #0645ad; } .json-collapsed { color: #666; } .json-children { margin-left: 20px; border-left: 1px solid #e1e5e9; padding-left: 10px; } .json-children.hidden { display: none; } `;document.head.appendChild(style);},// Main function to fetch and displayasyncdisplayWikipediaData(containerId){try{constdata=awaitthis.fetchUserInfo();this.createJSONTree(containerId,data);console.log('Wikipedia data loaded successfully');}catch(error){constcontainer=document.getElementById(containerId);if(container){container.innerHTML=`<div style="color: red; padding: 10px;">Error loading data: ${error.message}</div>`;}}}};// Usage examples:/*// Fetch and display dataWikipediaAPI.displayWikipediaData('my-container');// Or fetch data manually and create treeWikipediaAPI.fetchUserInfo().then(data => { WikipediaAPI.createJSONTree('my-container', data);});// Control functionsWikipediaAPI.expandAll('my-container');WikipediaAPI.collapseAll('my-container');*/