User:Isaac (WMF)/citation count.js

// Code largely borrowed from: https://en.wikipedia.org/wiki/User:TayIorRobinson/wikigender.js // and https://en.wikipedia.org/wiki/User:SuperHamster/CiteUnseen.js

(async => {

const CITATION_API = "https://citation-linking.wmcloud.org/api/check-citations?"; function processIcon(refNode, api_url, num_matches, pages) { let linkNode = document.createElement("a"); linkNode.href = api_url; linkNode.id = 'cite-finder-' + refNode.id.substring(10); linkNode.title = pages.join('\n'); if (num_matches == 1) { linkNode.text = "Found in " + num_matches + " other article." } else { linkNode.text = "Found in " + num_matches + " other articles." }	   let imgNode = document.createElement("img"); imgNode.alt = "Search icon"; imgNode.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/OOjs_UI_indicator_search-ltr.svg/17px-OOjs_UI_indicator_search-ltr.svg.png"; imgNode.decoding = "async"; imgNode.height = 17; imgNode.width = 17; imgNode.paddingRight = 5; linkNode.prepend(imgNode); refNode.appendChild(linkNode); }

async function fetchData(lang, pageid) { // https://citation-linking.wmcloud.org/api/check-citations?lang=en&page_id=53418&max_pages=3 let request_url = CITATION_API + "lang=" + lang + "&page_id=" + pageid + "&max_pages=3"; let response = await fetch(request_url); let data = await response.json; return data; }   function addCitationStats(lang, pageid, results) { let baseurl = CITATION_API + "lang=" + lang + "&page_id=" + pageid; let refs = document.querySelectorAll('li[id^=cite_note]'); for (let i = 0; i < refs.length; i++) { let citation_id = refs.item(i).id.substring(10); let citation_url = baseurl + "&citation_id=" + citation_id; for (let ref of results) { if (ref['citation-id'] == citation_id) { processIcon(refs.item(i), citation_url, ref['total-matches'], ref['matching-pages']); break }   		}    	}    }	// article but not Main Page if (mw.config.get('wgNamespaceNumber') == 0 && mw.config.get('wgWikibaseItemId') != 'Q5296') { let lang = mw.config.get("wgDBname").replace("wiki",""); if (lang == "en") { let pageid = mw.config.get("wgArticleId"); let mwIndicator = document.createElement("div"); mwIndicator.className = "mw-indicator"; mwIndicator.id = "mw-indicator-citationcount"; document.querySelector(".mw-indicators").appendChild(mwIndicator); let link = document.createElement("a"); link.href = "javascript:void(0)"; link.onclick = async => { link.remove; let data = await fetchData(lang, pageid); addCitationStats(lang, pageid, data.results); };		   link.title = "Gather statistics on where else this page's sources appear on English Wikipedia"; mwIndicator.appendChild(link); let img = document.createElement("img"); img.alt = "Search icon"; img.src = "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/OOjs_UI_indicator_search-ltr.svg/17px-OOjs_UI_indicator_search-ltr.svg.png"; img.decoding = "async"; img.height = 20; img.width = 20; link.appendChild(img); }	} });