Skip to content

Instantly share code, notes, and snippets.

@dalf
Last active November 10, 2022 15:47
Show Gist options
  • Save dalf/d4fefce72449da46a4a05fd19c03908c to your computer and use it in GitHub Desktop.
Save dalf/d4fefce72449da46a4a05fd19c03908c to your computer and use it in GitHub Desktop.

Add an eBioDiv link to the GBIF occurrence page using a Firefox extension and a Javascript: image

Installation

  • Install Tampermonkey Firefox extension
  • Below this README.md file, click on "Raw" button next to the file ebiodiv_into_gbif.user.js.
  • Tampermonkey will show a popup to install the script: image
  • Click on the "Install" button
  • Open https://www.gbif.org/occurrence/1066015170 in another tab image
  • Click on "Always allow"
  • The extension is installed

Usage

When related occurrences are found

As shown above, the GBIF occurrence pages will show a button on the right side of the screen.

The button goes directly to ebiodiv: image

Example: https://www.gbif.org/occurrence/1066015170

Occurrence without occurrences

The button is shown in grey when no related occurrences are found: image

// ==UserScript==
// @name eBioDiv link on GBIF
// @version 1
// @include https://www.gbif.org/*
// @grant GM.xmlHttpRequest
// ==/UserScript==
function getHtmlContainer() {
return document.querySelector('.stripe-comment');
}
function getEbioDivLink(occurrenceKey, active) {
const link = document.createElement('a');
link.innerHTML = "eBioDiv Matching Service";
link.setAttribute('id', 'eBioDivLink')
if (active) {
link.setAttribute('href', 'https://prod.ebiodiv.org/occurrence/' + occurrenceKey);
link.setAttribute('class', 'gb-button--brand');
link.setAttribute('style', 'margin-top: 2rem');
} else {
link.setAttribute('href', '#');
link.setAttribute('class', 'gb-button--discreet');
link.setAttribute('style', 'margin-top: 2rem; text-decoration: line-through;');
}
return link;
}
function get(url) {
return new Promise(resolve => {
GM.xmlHttpRequest({
method: "GET",
url,
onload: resolve
});
})
}
let currentPage = null;
let CHECK_INTERVAL = 1500;
async function checkUrlChange() {
setTimeout(checkUrlChange, CHECK_INTERVAL);
if (currentPage != location.href) {
// check if the button was already added
if (document.getElementById('eBioDivLink') != null) {
return;
}
//
let retry = false;
// yes, the URL has changed
try {
const pathname = document.location.pathname.split('/');
if (pathname.length >= 3 && pathname[0] == '' && pathname[1] == 'occurrence' && parseInt(pathname[2]) > 0) {
// this is an occurrence page
const occurrenceKey = pathname[2];
const container = getHtmlContainer();
if (container == null) {
// can't find where to add the link
// --> the page is not ready, try again later
retry = true;
} else {
// the page is ready
// check if there are some related occurrences
const tbOccResp = await get("https://tb.plazi.org/GbifOccLink/data/occurrences?occurrenceKeys=" + occurrenceKey);
const tbOcc = JSON.parse(tbOccResp.responseText)
let relatedOccCount = 0;
if (tbOcc != null && tbOcc.occurrences != null) {
relatedOccCount = Object.keys(tbOcc.occurrences).length;
}
// create the link
const link = getEbioDivLink(occurrenceKey, relatedOccCount);
// add the link
container.appendChild(link);
}
}
} catch (e) {
console.log(e);
}
// page has changed, update the current page except if we could not add the button
// (the page was not ready --> we try again later)
if (!retry) {
currentPage = location.href;
}
}
}
// Since the GBIF website is an Angular app, check periodically if the URL has changed.
setTimeout(checkUrlChange, CHECK_INTERVAL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment