User:Isaac (WMF)/citation count.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Isaac (WMF)/citation count. |
// 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);
}
}
})();