Skip to content

Instantly share code, notes, and snippets.

@anandology
Created June 27, 2013 22:50
Show Gist options
  • Select an option

  • Save anandology/5881099 to your computer and use it in GitHub Desktop.

Select an option

Save anandology/5881099 to your computer and use it in GitHub Desktop.
My dotjs extension for amazon.com
//
// My dotjs extension for amazon.com
// - Adds link to OL when an entry is available in OL
//
function find_isbn() {
// URL is of the form .../dp/$isbn/ref=..
var parts = window.location.pathname.split("/");
var patterns = [
"^/[^/]*/dp/([0-9Xx]{10})(?:/.*)?$",
"^/gp/product/([0-9Xx]{10})$"
]
for (var i in patterns) {
var m = window.location.pathname.match(patterns[i]);
console.log([window.location.pathname, patterns[i], m]);
if (m != null) {
return m[1];
}
}
return null;
}
function load_ol(isbn) {
console.log("load_ol: " + isbn);
$.getJSON("http://openlibrary.org/api/books?format=json&jscmd=data&bibkeys=ISBN:" + isbn, function(data) {
$(".parseasinTitle").before("<div class='ol' id='ISBN-XX'></div>".replace("XX", isbn));
var d = data['ISBN:' + isbn];
if (!d) {
return;
}
var $e = $("#ISBN-" + isbn);
var badges = {
view: "http://openlibrary.org/static/images/open-library-view-large-button.png",
read: "http://openlibrary.org/static/images/open-library-read-large-button.png",
borrow: "http://openlibrary.org/static/images/open-library-borrow-large-button.png",
checkedout: "http://openlibrary.org/static/images/open-library-checkedout-large-button.png"
};
var img, url;
if (d.ebooks && d.ebooks[0]) {
var ebook = d.ebooks[0];
if (ebook.availability == "read") {
img = badges.read;
url = ebook.read_url;
}
else if (ebook.availability == "borrow") {
img = badges.borrow;
url = ebook.borrow_url;
}
else {
img = badges.view;
url = d.url;
}
}
else {
img = badges.view;
url = d.url;
}
if (d) {
$("<a class='openlibrary' style='float: right'></a>")
.attr('href', url)
.appendTo($e)
.append("<img />").find("img").attr("src", img);
}
});
console.log("END load_ol: " + isbn);
}
load_ol(find_isbn());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment