Skip to content

Instantly share code, notes, and snippets.

@catrope
Last active June 25, 2024 22:01
Show Gist options
  • Save catrope/916fb1d4699bdc728c7d4fd42c537c61 to your computer and use it in GitHub Desktop.
Save catrope/916fb1d4699bdc728c7d4fd42c537c61 to your computer and use it in GitHub Desktop.
Browser console script to get grouped template usage from a Global Search results page
data = Array.from( document.querySelector( 'tbody' ).children )
.map( ( tr ) => [ tr.children[0].textContent, tr.children[1].textContent.trim() ] );
dataWithCounts = [];
for ( const [ wiki, pagename ] of data ) {
if ( !wiki.endsWith( '.wikipedia' ) ) {
continue;
}
let count = -1;
try {
const fetched = await fetch( `https://linkcount.toolforge.org/api/?page=${pagename}&project=${wiki}.org&namespaces=0` );
const fetchedAsJson = await fetched.json();
count = fetchedAsJson.transclusions.all;
console.log(wiki,pagename,count);
} catch ( e ) {
console.log(wiki, pagename, e);
}
dataWithCounts.push( [ wiki, pagename, count ] );
}
groupedByName = {};
for ( const [wiki, pagename, count] of dataWithCounts ) {
const unprefixed = pagename.replace( /^[^:]+:/, '' );
groupedByName[unprefixed] = groupedByName[unprefixed] || { total: 0, wikis: [] };
if ( count >= 0 ) {
groupedByName[unprefixed].total += count;
}
groupedByName[unprefixed].wikis.push( wiki );
}
copy( Object.entries( groupedByName ).map( ( [ pagename, { total, wikis } ] ) =>
`"${pagename}",${total},${wikis.length},${wikis.join(',')}`
).join( '\n' ) );
// Paste into a .csv file
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment