Wikipedia:WikiProject User scripts/Scripts/Sort Image Links

Description edit

The script sorts File Links (list of pages using the image) on Image pages. Articles are shown first, all other pages are sorted by namespace.

Limitation: for English projects only (namespaces are hardcoded).

You can test the script for example here: Image:Crypto_key.svg.

Questions or suggestions are welcome either on this talk page or on the author talk page.


Installation edit

All the code below should go to your monobook.js

Step 1 edit

Main function:

//Sort Image Links
function sortImageFileLinks(){
 var rg = /^(Talk|User|Wikipedia|Image|MediaWiki|Template|Help|Category|Portal)( talk)?:/
 var li, i, removed = [], ul = document.getElementById('filelinks')
 if (!ul) return
 while ((ul=ul.nextSibling) && ul.nodeName != 'UL')
 if (!ul) return
 li = ul.getElementsByTagName('LI')
 for (i=0; i<li.length; i++)
   if (li[i].firstChild.title.match(rg))
     removed.push(li[i])
 removed.sort(function(a,b){ return (a.firstChild.title > b.firstChild.title)?1:-1})
 ul.appendChild(document.createElement('hr'))
 for (i=0; i<removed.length; i++)
   ul.appendChild(removed[i])
}

Step 2 edit

Depends on how you want the script to work.

  • immediately on page load
if (mw.config.get('wgNamespaceNumber') == 6) 
 addOnloadHook(sortImageFileLinks);


  • when you click new sort links tab on top
if (mw.config.get('wgNamespaceNumber') == 6) 
 addOnloadHook(function(){
  addPortletLink('p-cactions', 'javascript:sortImageFileLinks()', 'sort links')
 })


  • when you click new [sort] link next to "File links" heading
if (mw.config.get('wgNamespaceNumber') == 6) 
 addOnloadHook(function(){
   var fl = document.getElementById('filelinks')
   if (fl) fl.innerHTML += ' <small>[<a href="javascript:sortImageFileLinks()">sort</a>]</small>'
})