User:CamelCaseNick/statementCountsInCategories.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/**
* Scripts that should achieve the same effect are
* User:Lucas Werkmeister/addStatementCountsToCategories.js and User:Nikki/StatementCounts.js
*/
(async function() {
if (mw.config.get("wgCanonicalNamespace") !== "Category") return;
let links = [].slice.call(document.querySelectorAll("#mw-pages li a"))
let query = `select ?article ?item ?statements where \{
values ?article \{${links.map(link => link.href).map(iri => "<" + iri + ">").join("")}\}
?item ^schema:about ?article; wikibase:statements ?statements.
\}`;
let articlesStatementsMap = (await (await fetch("https://query.wikidata.org/sparql", {
"method": "POST",
headers: new Headers({
"Accept": "application/sparql-results+json",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"
}),
"body": "query=" + encodeURIComponent(query)
})).json()).results.bindings;
links.forEach(link => {
let bindings = articlesStatementsMap.filter(t => t.article.value === link.href)
if (bindings.length == 0) return;
if (bindings.length > 1) throw new Error(`statement counts: ${link} was included multiple times`)
const statements = bindings[0].statements.value
const item = bindings[0].item.value
let hint = document.createElement("a")
hint.href = item
hint.append(`${statements} st.`)
hint.style.verticalAlign = "text-bottom"
hint.style.marginLeft = "3px"
hint.style.fontSize = ".8em"
if (statements <= 5) hint.style.fontWeight = "bold"
if (statements === 0) hint.style.color = "red"
link.insertAdjacentElement("afterend", hint)
});
}).call(window).catch(console.error);