Skip to content

Instantly share code, notes, and snippets.

@wragge
Last active December 3, 2021 09:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save wragge/b2af9dc56f7cb0a9476b to your computer and use it in GitHub Desktop.
Save wragge/b2af9dc56f7cb0a9476b to your computer and use it in GitHub Desktop.
RecordSearch Show Pages Userscript
// ==UserScript==
// @name RecordSearch Show Pages
// @namespace http://timsherratt.org/recordsearch_show_pages
// @description Enhances functionality of NAA's RecordSearch database
// @version 0.11
// @date 2021-12-03
// @creator Tim Sherratt
// @include https://recordsearch.naa.gov.au/SearchNRetrieve/Interface/ListingReports/ItemsListing.aspx*
// @include https://recordsearch.naa.gov.au/SearchNRetrieve/Interface/DetailsReports/ItemDetail.aspx*
// @include https://recordsearch.naa.gov.au/SearchNRetrieve/Interface/ViewImage.aspx*
// @include https://recordsearch.naa.gov.au//SearchNRetrieve/Interface/ViewImage.aspx*
// @include https://recordsearch.naa.gov.au/NameSearch/Interface/ItemDetail.aspx*
// @include https://recordsearch.naa.gov.au/NameSearch/Interface/ItemsListing.aspx*
// @grant GM_xmlhttpRequest
// @connect recordsearch.naa.gov.au
// ==/UserScript==
function getPageType() {
let type;
if (document.location.href.indexOf('ItemDetail.aspx') > 0) {
type = 'single';
} else if (document.location.href.indexOf('ViewImage.aspx') > 0) {
type = 'digitised';
}
else {
type = 'multiple';
}
return type;
}
function addPermalink() {
let barcode_label = document.evaluate('//div[@class="field"][text()="Item ID"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null ).snapshotItem(0);
let barcode = barcode_label.parentElement.nextSibling.nextSibling.innerText;
let permalink = 'https://recordsearch.naa.gov.au/scripts/AutoSearch.asp?O=I&Number=' + barcode;
let link_tag = document.createElement('link');
link_tag.setAttribute('rel', 'canonical');
link_tag.setAttribute('href', permalink);
document.head.appendChild(link_tag);
}
function updateTitle() {
let citation_label = document.evaluate('//div[@class="field"][text()="Citation"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null ).snapshotItem(0);
let citation = citation_label.parentElement.nextSibling.nextSibling.innerText;
let title_label = document.evaluate('//div[@class="field"][text()="Title"]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null ).snapshotItem(0);
let title = title_label.parentElement.nextSibling.nextSibling.innerText;
let page_title = citation + ', ' + title;
let title_tag = document.getElementsByTagName('title')[0];
title_tag.innerText = page_title;
}
function processPage() {
let type = getPageType();
if (type == 'single' || type == 'multiple') {
let rs_links = document.evaluate("//a[contains(@href, '/ViewImage.aspx?B=')]", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if (type == 'single') {
addPageLinks(rs_links.snapshotItem(0), type);
addPermalink();
updateTitle();
}
else if (type == 'multiple') {
let total = rs_links.snapshotLength;
for (let i = 0; i < total; i++) {
addPageLinks(rs_links.snapshotItem(i), type);
}
}
}
else if (type == 'digitised') {
redirectToPageUrl();
addBarcodeLink();
}
}
function addBarcodeLink() {
let barcodeElement = document.evaluate("//span[@id='lblBarcode']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
let barcode = barcodeElement.innerText;
barcodeElement.innerHTML = '<a href="https://recordsearch.naa.gov.au/scripts/AutoSearch.asp?O=I&Number=' + barcode + '">' + barcode + '</a>';
}
function redirectToPageUrl() {
let page_link
let page_num = document.getElementById('lblStartPage').innerText;
let barcode = document.getElementById('lblBarcode').innerText;
let page_url = window.location.href;
if (page_url.includes('&S=')) {
page_link = page_url.replace(/&S=\d+/i, '&S=' + page_num);
} else {
page_link = page_url + '&S=' + page_num;
}
if (page_url != page_link) {
window.location.replace(page_link);
}
// Set canonical url - not necessary as we're redirecting above
//let link_tag = document.createElement('link');
//link_tag.setAttribute('rel', 'canonical');
//link_tag.setAttribute('href', page_link);
// document.head.appendChild(link_tag);
// Set page title -- this change doesn't show up in Hypothesis
let item_citation = document.getElementById('lblCitation').innerText;
let item_title = document.getElementById('lblTitle').innerText;
let page_title = item_citation + ', ' + item_title + ', page ' + page_num;
let title_tag = document.getElementsByTagName('title')[0];
title_tag.innerText = page_title;
// These tags should together create a unique id in Hypothesis, but I'm not sure if it's working
// See: https://web.hypothes.is/help/how-hypothesis-interacts-with-document-metadata/#dublin-core-metadata
let id_tag = document.createElement('meta');
id_tag.setAttribute('name', 'dc:identifier');
id_tag.setAttribute('content', 'item/' + barcode + '/page/' + page_num);
document.head.appendChild(id_tag);
let rel_tag = document.createElement('meta');
rel_tag.setAttribute('name', 'dc:relation.ispartof');
rel_tag.setAttribute('content', 'naa.gov.au');
document.head.appendChild(rel_tag);
}
function addPageLinks(rs_link, type) {
let pages;
let barcode = rs_link.href.match(/\/ViewImage.aspx\?B=(\d+)/)[1];
let url = 'https://recordsearch.naa.gov.au/SearchNRetrieve/Interface/ViewImage.aspx?B=' + barcode;
GM_xmlhttpRequest({
method: 'GET',
url: url,
onload: function(response) {
var item = response.responseText;
if (item.match(/<span id="lblEndPage">(\d+)<\/span>/i)) {
pages = item.match(/<span id="lblEndPage">(\d+)<\/span>/i)[1];
} else {
pages = '1';
}
if (type == 'single') {
rs_link.innerHTML = rs_link.innerHTML + ' (' + pages + ' pages)';
rs_link.href = url;
} else {
rs_link.innerHTML = rs_link.innerHTML + '<br />' + pages + ' pages';
rs_link.setAttribute('onclick','');
}
}
});
}
processPage();
@wragge
Copy link
Author

wragge commented Jun 8, 2015

This script runs on search results, item details, and digitised file view pages in the National Archives of Australia's database RecordSearch.

On an item details or search results page:

  • If a file is digitised, it retrieves the number of pages in the file and displays it as part of the link to the digitised file. This means you know without clicking through whether the file contains 1 or 300 pages.
  • It also rewrites the link to the digitised file to STOP IT OPENING IN A NEW WINDOW. The default behaviour is just so annoying.

On a digitised file view page:

  • The barcode is turned into a link back to the item details page.
  • The url is rewritten to include the page number. This makes it easier to share links, and also allows Hypothes.is to associate annotations with specific pages.

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 either the Tampermonkey or Greasemonkey plugins. Then just click on the 'Raw' button above. Your userscript manager 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