-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetty.js
More file actions
47 lines (47 loc) · 1.79 KB
/
getty.js
File metadata and controls
47 lines (47 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// ==UserScript==
// @name Getty Vocabs - Copy Page Link
// @namespace https://libraries.cca.edu
// @version 0.3.0
// @description Add a button to easily copy canonical page link from Getty vocabs.
// @author @phette23
// @match https://www.getty.edu/vow/*
// @grant none
// @updateURL https://raw.githubusercontent.com/cca/libraries_tampermonkey/main/getty.js
// @downloadURL https://raw.githubusercontent.com/cca/libraries_tampermonkey/main/getty.js
// ==/UserScript==
(function () {
let d = document
// there is a different .page on the search results and we cannot run this script on full
// diplay pages without adding URLs for each vocab (ULANFullDisplay, AATFullDisplay, ...)
// but by validating the URL below we avoid adding a nonfunctional button
let pages = d.querySelectorAll('.page')
if (pages.length) {
// URL is a child node, iterate to find it
let parent = null
let url = null
for (let page of pages) {
if (url) break
try {
console.log(page.lastChild.textContent.trim())
url = new URL(page.lastChild.textContent.trim())
parent = page
break
} catch {
// not a valid URL, continue
}
}
if (url) {
// Create button
let btn = d.createElement('button')
btn.textContent = 'Copy'
btn.style.marginLeft = '1em'
parent.appendChild(btn)
// On click, copy URL to clipboard
btn.addEventListener('click', function () {
navigator.clipboard.writeText(url.toString()).then(function () {
btn.textContent = 'Copied!'
})
})
}
}
})()