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 UserOptions API Explorer with Tools Menu Integration$(document).ready(function(){// Add link to Tools menu on all pagesmw.hook('wikipage.content').add(function(){mw.loader.using(['mediawiki.util'],function(){// Only add the link if it doesn't already existif(!document.getElementById('t-useroptions-api')){mw.util.addPortletLink('p-tb',// Target portlet ID (Tools menu)mw.config.get('wgServer')+'/wiki/Special:BlankPage/UserOptions',// Link URL'User Options API',// Link text't-useroptions-api',// Link ID'Explore user options via MediaWiki API'// Tooltip);}});});// Only run the main interface if we're on the specific pageif(mw.config.get('wgCanonicalSpecialPageName')==='Blankpage'&&mw.config.get('wgPageName')==='Special:BlankPage/UserOptions'){$('#firstHeading').text('User Options API Explorer');varhtml='<div id="useroptions-container">'+'<div class="api-info" style="background: #e8f4f8; border: 1px solid #bee5eb; border-radius: 5px; padding: 15px; margin-bottom: 20px;">'+'<strong>API Endpoint:</strong> '+mw.config.get('wgServer')+'/w/api.php<br>'+'<strong>Parameters:</strong> action=query, meta=userinfo, uiprop=options, format=json'+'</div>'+'<div class="controls" style="margin-bottom: 20px;">'+'<button id="fetchBtn" class="btn btn-primary">Fetch User Options</button>'+'<button id="expandBtn" class="btn btn-success">Expand All</button>'+'<button id="collapseBtn" class="btn btn-danger">Collapse All</button>'+'</div>'+'<div id="status"></div>'+'<div id="jsonResult"></div>'+'</div>';$('#mw-content-text').html(html);// Wikipedia UserOptions API ModulevarUserOptionsAPI={fetchUserOptions:function(){varparams={action:'query',meta:'userinfo',uiprop:'options',format:'json'};returnnewmw.Api().get(params);},createJSONTree:function(containerId,data){varcontainer=document.getElementById(containerId);if(!container)return;container.innerHTML='';vartree=document.createElement('div');tree.className='json-tree';tree.appendChild(this.createNode('response',data,0));container.appendChild(tree);this.addStyles();},createNode:function(key,value,depth){varnode=document.createElement('div');node.className='json-node';varself=this;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)){vartoggleId='toggle-'+Date.now()+'-'+Math.random();vartoggle=document.createElement('span');toggle.className='json-toggle';toggle.textContent='▼';toggle.id=toggleId;varkeySpan=document.createElement('span');keySpan.className='json-key';keySpan.textContent=key+':';varsummary=document.createElement('span');summary.className='json-collapsed';summary.textContent=' ['+value.length+' items]';node.appendChild(toggle);node.appendChild(keySpan);node.appendChild(summary);varchildren=document.createElement('div');children.className='json-children';for(vari=0;i<value.length;i++){children.appendChild(this.createNode('['+i+']',value[i],depth+1));}node.appendChild(children);toggle.onclick=function(){self.toggleNode(toggleId);};}elseif(typeofvalue==='object'){varkeys=Object.keys(value);vartoggleId='toggle-'+Date.now()+'-'+Math.random();vartoggle=document.createElement('span');toggle.className='json-toggle';toggle.textContent='▼';toggle.id='toggle-'+Date.now()+'-'+Math.random();varkeySpan=document.createElement('span');keySpan.className='json-key';keySpan.textSize=key+':';varsummary=document.createElement('span');summary.className='json-collapsed';summary.textContent=' {'+keys.length+' properties}';node.appendChild(toggle);node.appendChild(keySpan);node.appendChild(summary);varchildren=document.createElement('div');children.className='json-children';for(varj=0;j<keys.length;j++){varobjKey=keys[j];children.appendChild(this.createNode(objKey,value[objKey],depth+1));}node.appendChild(children);toggle.onclick=function(){self.toggleNode(toggleId);};}returnnode;},toggleNode:function(toggleId){vartoggle=document.getElementById(toggleId);varchildren=toggle.parentNode.querySelector('.json-children');if(children.classList.contains('hidden')){children.classList.remove('hidden');toggle.textContent='▼';}else{children.classList.add('hidden');toggle.textContent='▶';}},expandAll:function(containerId){varcontainer=document.getElementById(containerId);vartoggles=container.querySelectorAll('.json-toggle');for(vari=0;i<toggles.length;i++){vartoggle=toggles[i];varchildren=toggle.parentNode.querySelector('.json-children');if(children){children.classList.remove('hidden');toggle.textContent='▼';}}},collapseAll:function(containerId){varcontainer=document.getElementById(containerId);vartoggles=container.querySelectorAll('.json-1toggle');for(vari=0;i<toggles.length;i++){vartoggle=toggles[i];varchildren=toggle.parentNode.querySelector('.json-children');if(children){children.classList.add('hidden');toggle.textContent='▶';}}},escapeHtml:function(text){vardiv=document.createElement('div');div.textContent=text;returndiv.innerHTML;},addStyles:function(){if(document.getElementById('json-tree-styles'))return;varstyle=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;'+'max-height: 600px;'+'overflow-y: 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;'+'}'+'.btn {'+'display: inline-block;'+'padding: 6px 12px;'+'margin: 0 5px 5px 0;'+'font-size: 14px;'+'font-weight: normal;'+'line-height: 1.42857143;'+'text-align: center;'+'white-space: nowrap;'+'vertical-align: middle;'+'cursor: pointer;'+'border: 1px solid transparent;'+'border-radius: 4px;'+'text-decoration: none;'+'}'+'.btn-primary {'+'color: #fff;'+'background-color: #0645ad;'+'border-color: #0645ad;'+'}'+'.btn-success {'+'color: #fff;'+'background-color: #28a745;'+'border-color: #28a745;'+'}'+'.btn-danger {'+'color: #fff;'+'background-color: #dc3545;'+'border-color: #dc3545;'+'}'+'.btn:hover {'+'opacity: 0.8;'+'}';document.head.appendChild(style);},displayUserOptions:function(containerId){varstatusDiv=document.getElementById('status');varfetchBtn=document.getElementById('fetchBtn');varself=this;fetchBtn.disabled=true;statusDiv.innerHTML='<div style="color: #666; font-style: italic;">Fetching user options...</div>';this.fetchUserOptions().done(function(data){statusDiv.innerHTML='<div style="color: #28a745;">✓ User options loaded successfully</div>';self.createJSONTree(containerId,data);fetchBtn.disabled=false;}).fail(function(error){statusDiv.innerHTML='<div style="color: #d33; background: #ffeaea; padding: 10px; border-radius: 5px;">Error: '+error+'</div>';console.error('Fetch error:',error);fetchBtn.disabled=false;});}};// Event listeners (only when on the correct page)$('#fetchBtn').on('click',function(){UserOptionsAPI.displayUserOptions('jsonResult');});$('#expandBtn').on('click',function(){UserOptionsAPI.expandAll('jsonResult');});$('#collapseBtn').on('click',function(){UserOptionsAPI.collapseAll('jsonResult');});// Auto-load on page loadUserOptionsAPI.displayUserOptions('jsonResult');}});