Skip to content

Instantly share code, notes, and snippets.

@atotic
Last active January 26, 2020 19:00
Show Gist options
  • Save atotic/6970951 to your computer and use it in GitHub Desktop.
Save atotic/6970951 to your computer and use it in GitHub Desktop.
Tampermonkey: link Amazon Books to Palo Alto Library
// ==UserScript==
// @name Palo Alto Library And Amazon
// @namespace http://www.totic.org/
// @version 1.0.1
// @description Link Amazon Books to Palo Alto library
// @copyright 2012+ [email protected]
// @match http://www.amazon.com/*
// @match https://www.amazon.com/*
// @require http://code.jquery.com/jquery-1.9.1.min.js
// ==/UserScript==
(function() {
function getLibraryUrl() {
if ($("#storeID").attr('value') == "books")
{
var title, author, isbn;
try {
title = $('#productTitle').contents()[0].data;
}
catch(e) {
}
try {
author = $('.author > *:visible').first().text();
}
catch(e) {
}
try {
isbn = $('b:contains("ISBN-10")').parent().contents().get(1).data;
}
catch(e) {
}
if (title) {
debugger;
title = title.replace(/\n/g, ""); // No CR
title = title.replace(/\([^)]+\)/g, ""); // (Book 1) is removed
title = title.replace(/[\?\!\;\)\(]/g, ""); // Remove punctuation
title = title.replace(/ /g, "+"); // url sub
var url = 'http://webcat.cityofpaloalto.org/ipac20/ipac.jsp?menu=search&ri=&index=.TW&term=' + title;
return url;
}
isbn = isbn.match(/\d+/g)[0];
if (isbn) {
var url = "http://webcat.cityofpaloalto.org/ipac20/ipac.jsp?menu=search&profile=pacl&index=ISBNEX&term=" + isbn;
return url;
}
}
return null;
}
$(document).ready(function() {
var libraryUrl = getLibraryUrl();
if (!libraryUrl) {
console.log("not a book");
return;
}
var b = $('<span id="paloAltoLibrary">Palo Alto Library</span>');
b.css({
color: '#ffffff',
fontSize: '12px',
padding: '4px 30px 3px 28px',
borderRadius: '3px',
border: 'solid #d91c71 1px',
width: '100%',
background: '-webkit-gradient(linear, 0 0, 0 100%, from(#D03865), to(#BF335C))',
marginBottom: '8px',
position: 'static',
opacity: '1.0'
})
.addClass('a-button')
.addClass('a-button-input');
b.click(function(ev) {
window.open(libraryUrl, "_blank");
ev.preventDefault();
ev.stopPropagation();
});
// figure out where to insert the button
var addToCart = $('.a-button-stack'); // Page with 'AddToCart' button
if (addToCart.length > 0)
addToCart.first().before(b);
else { //
var bbox = $('.cBox.buyTopBox');
b.css('marginLeft', 30);
bbox.first().prepend(b);
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment