User:Dr pda/templatecheck.js
From Wikipedia, the free encyclopedia
If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
//This function adds a link to the toolbox which, when clicked on a Category page, prompts for the name of a template, //checks if the pages listed contain that template, and changes the bullet to a tick mark if so. //To use this function add <nowiki>{{subst:js|User:Dr pda/templatecheck.js}}</nowiki> to your monobook.js // function loadXMLDocPassingTemplate(url,handler,template) { // branch for native XMLHttpRequest object if (window.XMLHttpRequest) { var req = new XMLHttpRequest(); } // branch for IE/Windows ActiveX version else if (window.ActiveXObject) { var req = new ActiveXObject("Microsoft.XMLHTTP"); } if (req) { req.onreadystatechange = function () {handler(req,template)}; req.open("GET", url, true); req.send(""); } } function getTemplateList(req,template) { // only if req shows "loaded" if (req.readyState == 4) { // only if "OK" if (req.status == 200) { // ...processing statements go here... var response = req.responseXML.documentElement; var pages = response.getElementsByTagName('page'); if(pages.length > 0){ var pagesList = new Object(); for(var i=0;i<pages.length; i++){ var tl=pages[i].getElementsByTagName('tl'); if(tl.length > 0){ for(var j=0;j<tl.length;j++){ if(tl[j].getAttribute('title')==template){ pagesList[pages[i].getAttribute('title')] = 'true'; } } } } if(!list){ mwPagesDiv=document.getElementById('mw-pages'); liList = mwPagesDiv.getElementsByTagName('li'); } if(liList.length>0){ for(var i=0; i<liList.length; i++){ var pageName = liList[i].firstChild.title; if(pagesList[pageName]){ liList[i].style.cssText = "list-style-image:url('http://upload.wikimedia.org/wikipedia/commons/thumb/5/52/%E2%98%91.svg/11px-%E2%98%91.svg.png')"; } else{ liList[i].style.cssText = ''; } } } } } else { alert("There was a problem retrieving the XML data:\n" + req.statusText); } } } function checkTemplates(){ var template=prompt("Enter the template you want to check for\n (Don't include Template:)",""); template = "Template:"+template.toUpperCase().substr(0,1)+template.substr(1); if(list){ listNumber = prompt("Enter the number of the section you want to check for "+template+"\n(See table of contents)"); listNumber--;//array starts at 0 var olList = document.getElementsByTagName('ol'); if(!(olList.length>0 && olList[listNumber])) {alert("Error. There are only"+olList.length+" lists on this page");return 0;} liList = olList[listNumber].getElementsByTagName('li'); var titleString = ''; for(var i=0; i<liList.length; i++){ titleString += encodeURIComponent(liList[i].firstChild.title + '|'); } titleString = titleString.substring(0,titleString.length-1); queryURL = '/w/api.php?action=query&titles='+titleString+'&prop=templates&format=xml'; } else{ queryURL = '/w/api.php?action=query&generator=categorymembers&gcmcategory=' + wgPageName.substr(9) + '&gcmlimit=200&prop=templates&format=xml'; } loadXMLDocPassingTemplate(queryURL,getTemplateList,template); } addOnloadHook(function () { if(wgCanonicalNamespace == 'Category'){ list = false; addPortletLink('p-tb', 'javascript:checkTemplates()', 'Check for template', 't-check-template', 'Check pages in category for use of a template', '', ''); } else if(document.location.href.indexOf('List_of_biographies/') != -1){ list = true; addPortletLink('p-tb', 'javascript:checkTemplates()', 'Check for template', 't-check-template', 'Check pages in list for use of a template', '', ''); } });