Created
September 29, 2025 03:28
-
-
Save wragge/33b5bb306869a7d0d5a837beb28dbd2f to your computer and use it in GitHub Desktop.
Add links from SLV digitised items to entries in Wikimedia Commons
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 SLV add Wikimedia Commons links | |
| // @namespace wraggelabs.com/slv_add_wikimedia_commons_links | |
| // @match *://viewer.slv.vic.gov.au/?entity=* | |
| // @connect commons.wikimedia.org | |
| // @grant GM_xmlhttpRequest | |
| // @version 1.0 | |
| // @author - | |
| // @description 29/09/2025, 10:37:18 | |
| // @require https://cdn.jsdelivr.net/gh/CoeJoder/[email protected]/waitForKeyElements.js | |
| // ==/UserScript== | |
| waitForKeyElements("#handleURL", (handleInput) => { | |
| // Get the handle url | |
| const handle = document.querySelector("input.handle"); | |
| // Create a url to search for the handle in Wikimedia Commons | |
| const searchUrl = "https://commons.wikimedia.org/w/api.php?action=query&format=json&list=exturlusage&euquery=" + handle.value.replace("http://", ""); | |
| const headers = {"User-Agent": "Userscript/SLV add Wikimedia Commons links ([email protected])"} | |
| // Query the Wikimedia API | |
| GM_xmlhttpRequest({ | |
| method: "GET", | |
| url: searchUrl, | |
| responseType: "json", | |
| headers: headers, | |
| onload: function(response) { | |
| // Extract page ids from the results (if any) | |
| let pageIds = []; | |
| for (let page of response.response["query"]["exturlusage"]) { | |
| pageIds.push(page["pageid"]); | |
| } | |
| // If there are results, we'll get more info using the page ids | |
| if (pageIds.length > 0) { | |
| // Create a url using the page ids to get more info | |
| let infoUrl = "https://commons.wikimedia.org/w/api.php?action=query&pageids=" + pageIds.join("|") + "&prop=info&inprop=url&format=json"; | |
| GM_xmlhttpRequest({ | |
| method: "GET", | |
| url: infoUrl, | |
| responseType: "json", | |
| headers: headers, | |
| onload: function(response) { | |
| // Get the titles and urls from the results and put them in an HTML list | |
| let linkList = document.createElement("ul"); | |
| for (const [key, value] of Object.entries(response.response["query"]["pages"])) { | |
| let pageUrl = value["canonicalurl"]; | |
| let pageTitle = value["title"]; | |
| let listItem = document.createElement("li"); | |
| listItem.innerHTML = "<a href='" + pageUrl + "' style='color: #1779ba;'>" + pageTitle + "</a>"; | |
| linkList.appendChild(listItem); | |
| } | |
| // Add a new row in the further details section of the page and add the list | |
| // New heading in dt | |
| let dt = document.createElement("dt"); | |
| dt.className = "cell small-6 medium-2 large-2"; | |
| dt.innerHTML = "Wikimedia Commons"; | |
| // New dd to contain list | |
| let dd = document.createElement("dd"); | |
| dd.className = "cell small-6 medium-10 large-10"; | |
| dd.appendChild(linkList); | |
| // Get the current dl list | |
| let infoList = document.querySelector("dl#moreInfoList"); | |
| // Add the new row | |
| infoList.appendChild(dt); | |
| infoList.appendChild(dd); | |
| } | |
| }); | |
| } | |
| } | |
| }); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add links from SLV digitised items to entries in Wikimedia Commons
There are more than 490 digitised images from the State Library of Victoria that have been uploaded to Wikimedia Commons. This userscript searches for the handle url of a digitised image in Wikimedia Commons. If it's found, links to the Commons pages are listed in the 'Further details' section of the SLV digitised image viewer.
Installation