Forked from wragge/add-wikidata-links-to-adb.user.js
Created
August 20, 2025 07:57
-
-
Save ross-spencer/b4ceec2579a818f512032c57d39d59fd to your computer and use it in GitHub Desktop.
Add links from Wikidata to entries in the Australian Dictionary of Biography
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Add Wikidata links to ADB | |
// @namespace wraggelabs.com/add-wikidata-links-to-adb | |
// @match https://adb.anu.edu.au/biography/* | |
// @version 1.1 | |
// @author Tim Sherratt | |
// @description 20/08/2025, 10:39:27 | |
// @grant GM_xmlhttpRequest | |
// @connect query.wikidata.org | |
// ==/UserScript== | |
function addWikidataLinks(query) { | |
// Query the Wikidata SPARQL service | |
const endpointUrl = 'https://query.wikidata.org/sparql'; | |
const fullUrl = endpointUrl + '?query=' + encodeURIComponent(query); | |
const headers = { 'Accept': 'application/sparql-results+json' }; | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: fullUrl, | |
headers: headers, | |
onload: function(response) { | |
const data = JSON.parse(response.responseText); | |
const bindings = data.results.bindings; | |
if (bindings.length > 0) { | |
// Add a new heading to the life summary | |
let lifeSummary = document.querySelector("div.lifeSummary"); | |
let heading = document.createElement("h5"); | |
heading.textContent = "Related links from WikiData"; | |
lifeSummary.appendChild(heading); | |
// Create a list to contain the links | |
let linkList = document.createElement("ul"); | |
for (let link of bindings) { | |
// Add the links to the list (ignoring the ADB link) | |
if (link.propertyLabel.value != "Australian Dictionary of Biography ID") { | |
let linkItem = document.createElement("li"); | |
linkItem.innerHTML = '<a href="' + link.url.value + '">' + link.propertyLabel.value.replace(" ID", "") + '</a>'; | |
linkList.appendChild(linkItem); | |
} | |
} | |
// Add the list of links to the life summary | |
lifeSummary.appendChild(linkList); | |
} | |
} | |
}); | |
} | |
// Get the ADB id from the url | |
const adb_id = window.location.href.match(/\/biography\/([a-zA-Z0-9\-]+)\//)[1]; | |
// This query gets all the Australian external identifiers that are linked to the person who has this ADB id. | |
const query = `SELECT DISTINCT ?property ?propertyLabel ?value ?url where { | |
?item p:P1907 ?statement. | |
?statement (ps:P1907) "${adb_id}". | |
?item ?wdt ?value. | |
?property wikibase:directClaim ?wdt; | |
wikibase:propertyType wikibase:ExternalId; | |
wdt:P17 wd:Q408. | |
OPTIONAL { ?property wdt:P1630 ?formatter } | |
BIND(uri(REPLACE(?formatter,"\\\\\$1",?value)) as ?url) | |
SERVICE wikibase:label {bd:serviceParam wikibase:language "[AUTO LANGUAGE],en" .} | |
}`; | |
addWikidataLinks(query); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment