MediaWiki:Gadget-RcPopularPages.js

// Creates a list of popular pages on Special:RecentChanges // by Espyo

$(function {   'use strict';

const PAGES_TO_SHOW = 10;

var popularListSpan = null;

function addScanButton { var popularSpan = document.createElement('span'); document.querySelector('.rcoptions').appendChild(popularSpan);

var popularLabelSpan = document.createElement('span'); popularSpan.appendChild(popularLabelSpan); popularLabelSpan.innerHTML = 'Popular pages in this list: ';

popularListSpan = document.createElement('span'); popularSpan.appendChild(popularListSpan);

var scanA = document.createElement('a'); popularListSpan.appendChild(scanA); scanA.onclick = scan; scanA.innerHTML = '[Show]'; }

function scan { var lines = document.querySelectorAll('div.mw-changeslist a.mw-changeslist-title'); if(lines.length == 0) return;

var editsByPage = {};

for(var i = 0; i < lines.length; i++) { var page = lines[i].innerHTML; if(page in editsByPage) { editsByPage[page][1]++; } else { editsByPage[page] = [lines[i], 1]; }       }

var pairs = Object.keys(editsByPage).map(function(key) {           return [key, editsByPage[key][0], editsByPage[key][1]];        }); pairs.sort(function(a, b) {           return b[2] - a[2];        });

popularListSpan.innerHTML = '';

var pagesToShow = Math.min(PAGES_TO_SHOW, pairs.length); for(var p = 0; p < pagesToShow; p++) { var newLink = pairs[p][1].cloneNode; newLink.innerHTML = pairs[p][1].innerHTML; var text = ' (' + pairs[p][2] + ')'; if(p < pagesToShow - 1) { text += ' | '; }           popularListSpan.appendChild(newLink); popularListSpan.appendChild(document.createTextNode(text)); }   }

if(!mw || mw.config.get('wgPageName') != 'Special:RecentChanges') return;

addScanButton; });