Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Userscript to insert pictures of people extracted from Trove newspaper articles in the search results.
// ==UserScript==
// @name Trove -- People Inside
// @namespace http://wraggelabs.com/trove_show_people
// @description Newspapers are about people. This userscript enriches Troves's newspaper results by inserting images of some of those people.
// @version 0.1
// @date 2015-06-9
// @creator Tim Sherratt
// @include http://trove.nla.gov.au/newspaper/result?*&l-illustrated=true*&l-illtype=Photo*
// @grant GM_xmlhttpRequest
// @connect faceapi.herokuapp.com
// ==/UserScript==
function getFaces(row) {
var href = document.evaluate(".//a[contains(@href, '/ndp/del/article/')]", row, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.href;
var article_id = href.match(/\/ndp\/del\/article\/(\d+)/)[1];
var url = 'http://faceapi.herokuapp.com/faces?article_id=' + article_id;
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function(response) {
var data = JSON.parse(response.responseText);
if (data.length > 0) {
row.innerHTML = "<img style='float:right; width:90px; height:90px; padding:10px;' src=" + data[0]['image_url'] + ">" + row.innerHTML;
}
}
});
}
var links = [];
rows = document.evaluate("//li[descendant::a[contains(@href, '/ndp/del/article/')]]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for ( var i=0 ; i < rows.snapshotLength; i++ ) {
row = rows.snapshotItem(i);
getFaces(row);
}
Owner

wragge commented Jun 9, 2015

This userscript operates on search results in the Trove newspapers zone. If the Illustrated and Illustration type - Photo facets are selected it will:

  • query my Face API with the article id for each result
  • if there are any faces from the article it will insert the first as a thumbnail in the result

Currently there are only about 4000 faces in the Face API database and more than 4 million articles with photos in Trove -- so your chances of coming across a face in your results might be slim. But that's part of the fun!

If you want to see the userscript in action try searching in Table Talk and selecting the 1900-1909 decade facet. You should see plenty of examples.

To install this in Chrome, first add the TamperMonkey extension. Then just click on the 'Raw' button above. Tampermonkey will then ask you if you really want to install it. Click install!

For Firefox, you'll need to install the Greasemonkey plugin. Then just click on the 'Raw' button above. Greasemonkey will then ask you if you really want to install it. Click install!

Before

Before

After

After

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment