Last active
November 7, 2017 23:35
-
-
Save wragge/d7b4e2f379b63860cd1c36b33d9a88af to your computer and use it in GitHub Desktop.
Userscript to add a handy button to Trove works and versions to save permalink to clipboard.
This file contains 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 Trove Copy Permalink | |
// @namespace http://timsherratt.org/trovecopypermalink | |
// @version 0.1.2 | |
// @description Add a button to Trove works/versions to easily save permalink to clipboard | |
// @author Tim Sherratt (@wragge) | |
// @match http://trove.nla.gov.au/work/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function copyToClipboard() { | |
// This function will be called when you click on the 'copy to clipboard' button | |
// Clipboard stuff copied from https://developers.google.com/web/updates/2015/04/cut-and-copy-commands | |
var range = document.createRange(); | |
range.selectNode(link); | |
window.getSelection().addRange(range); | |
try { | |
// Now that we've selected the link, execute the copy command | |
var successful = document.execCommand('copy'); | |
var msg = successful ? 'successful' : 'unsuccessful'; | |
// Log the result of the copy command | |
console.log('Copy link command was ' + msg); | |
// Change the button text | |
button.textContent = 'copied!'; | |
} catch(err) { | |
console.log('Oops, unable to copy'); | |
} | |
// Remove the selections | |
window.getSelection().removeAllRanges(); | |
} | |
// Find the permalink bookmark | |
var link = document.querySelector('dl.details dd a'); | |
// If there's a permalink, add a button | |
if (link !== null) { | |
// Create a button element | |
var button = document.createElement('button'); | |
// Text for the button | |
var copyText = document.createTextNode('copy to clipboard'); | |
// A bit of space to offset button from link | |
var spacer = document.createTextNode(' '); | |
// Add the text to the button | |
button.appendChild(copyText); | |
// Add the copyToClipboard() function to the button's click event | |
button.onclick = copyToClipboard; | |
// Reset the text of the button | |
button.onmouseenter = function() { button.textContent = 'copy to clipboard'; }; | |
// Set the style of the button | |
button.style.cursor = "pointer"; | |
button.style.color = "#ffffff"; | |
button.style.backgroundColor = "#690"; | |
button.style.border = "None"; | |
button.style.padding = "4px"; | |
button.style.borderRadius = "4px"; | |
// Add the spacer after the bookmark link | |
link.parentElement.appendChild(spacer); | |
// Add the button after the spacer | |
link.parentElement.appendChild(button); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See here for some information on installing userscripts: http://timsherratt.org/digital-heritage-handbook/docs/installing-userscripts/