Created
January 7, 2010 16:39
-
-
Save fixlr/271352 to your computer and use it in GitHub Desktop.
Add Google Book cover art to a webpage.
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
var DEBUG = false; | |
var MAXBUFFER = 10; | |
function debug_init() { | |
this.debug_log = document.createElement("div"); | |
this.debug_log.id = 'debug'; | |
this.debug_log.setAttribute('style', "min-height: 1em; width: 95%; border: 1px solid #FF6400; margin-left: auto; margin-right: auto;"); | |
h2 = document.createElement('h2'); | |
h2.setAttribute('style', "color: #fff; background-color: #FF6400; margin: 0 0 0.75em 0; height: 1.5em; font-size: 1.2em; padding: 5px 0 3px 0;"); | |
h2.innerHTML = 'DEBUG LOG'; | |
this.debug_log.appendChild(h2); | |
document.body.appendChild(this.debug_log); | |
} | |
function debug_msg(sbj, msg) { | |
if (DEBUG) | |
this.debug_log.innerHTML += "<strong>"+sbj+":</strong> "+msg+"<br />"; | |
} | |
// Define cross-platform CSS handlers | |
function Stylesheet(ss) { | |
if (typeof ss == "number") ss = document.styleSheets[ss]; | |
this.ss = ss; | |
} | |
Stylesheet.prototype.getRules = function() { | |
return this.ss.cssRules?this.ss.cssRules:this.ss.rules; | |
} | |
Stylesheet.prototype.getRule = function(s) { | |
var rules = this.getRules(); | |
if (!rules) return null; | |
if (typeof s == "number") return rules[s]; | |
s = s.toLowerCase(); | |
for(var i = rules.length-1; i >= 0; i--) { | |
if (rules[i].selectorText.toLowerCase() == s) return rules[i]; | |
} | |
return null; | |
}; | |
Stylesheet.prototype.getStyles = function(s) { | |
var rule = this.getRule(s); | |
if (rule && rule.style) return rule.style; | |
else return null; | |
}; | |
Stylesheet.prototype.getStyleText = function(s) { | |
var rule = this.getRule(s); | |
if (rule && rule.style && rule.style.cssText) return rule.style.cssText; | |
else return ""; | |
}; | |
Stylesheet.prototype.insertRule = function(selector, styles, n) { | |
if (n == undefined) { | |
var rules = this.getRules(); | |
n = rules.length; | |
} | |
if (this.ss.insertRule) // Try the W3C API first | |
this.ss.insertRule(selector + "{"+ styles +"}", n); | |
else if (this.ss.addRule) // Otherwise, use the IE API | |
this.ss.addRule(selector, styles, n); | |
} | |
Stylesheet.prototype.deleteRule = function(s) { | |
// If s is undefined, make it the index of the last rule | |
if (s == undefined) { | |
var rules = this.getRules(); | |
s = rules.length-1; | |
} | |
// If s is not a number, look for a matching rule and get its index | |
if (typeof s != "number") { | |
s = s.toLowerCase(); | |
var rules = this.getRules(); | |
for(var i = rules.length-1; i >= 0; i--) { | |
if (rules[i].selectortext.toLowerCase() == s) { | |
s = i; | |
break; | |
} | |
} | |
if (i == -1) return; | |
} | |
if (this.ss.deleteRule) this.ss.deleteRule(s); | |
else if (this.ss.removeRule) this.ss.removeRule(s); | |
} | |
function gbook_coverart(data) { | |
for (i in data) { | |
var book = data[i]; | |
if (book.thumbnail_url != undefined) { | |
updateCover(book.bib_key.replace(':', '_'), book); | |
} | |
} | |
} | |
function gbookScript(bibkeys, callback) { | |
var script = document.createElement("script"); | |
src = "http://books.google.com/books?jscmd=viewapi&bibkeys="+escape(bibkeys.join(','))+"&callback="+callback; | |
debug_msg('SCRIPT', ' <a href="'+src+'">Google Books API call</a>'); | |
script.src = src; | |
document.body.appendChild(script); | |
} | |
function updateCover(bibkey, book) { | |
debug_msg('UPDATE COVER', bibkey+'<a href="'+book.thumbnail_url+'">thumbnail url</a>'); | |
gbook_div = document.getElementById(bibkey); | |
if (gbook_div.className == 'gbookthumbnail' && book.thumbnail_url) { | |
gbook_div.innerHTML = '<img src="'+book.thumbnail_url+'" border="0" alt="" />'; | |
} else if (gbook_div.className == 'gbookpreview' && book.preview_url) { | |
link_text = 'More Info'; | |
if(book.preview == 'partial') { link_text = 'Preview'; } | |
if(book.preview == 'full') { link_text = 'Full Text'; } | |
gbook_div.innerHTML = | |
'<a href="'+book.preview_url+'" class="thumbnail" target="_blank">' | |
+ '<img src="'+book.thumbnail_url+'" border="0" alt="" />' | |
+ '<span style="padding-top: 3px; display: block;">' + link_text + '</span></a>'; | |
} else if (gbook_div.className == 'gbookfull') { | |
} | |
} | |
window.onload = function() { | |
if (DEBUG) { debug_init(); } | |
// Refactor this | |
ss = new Stylesheet(0); | |
ss.insertRule(".gbookpreview", "width: 80px; text-align: center; padding: 0; margin: 0px;"); | |
ss.insertRule(".gbookpreview a", "margin: 0px; padding: 0px; color: darkblue; text-decoration: none; text-transform: uppercase; font-size: 10px; font-weight: bold;"); | |
ss.insertRule(".gbookpreview a:hover", "color: #000;"); | |
ss.insertRule(".gbookpreview img", "margin: 0px; padding: 0px;"); | |
allDivs = document.getElementsByTagName('div'); | |
bibkeys = []; | |
for (i in allDivs) { | |
var div = allDivs[i]; | |
if (div.className == 'gbookthumbnail' || div.className == 'gbookpreview' || div.className == 'gbsthumbnail') { | |
var bibkey = div.getAttribute('bibkey').split(' ')[0]; | |
bibkeys.push(bibkey); | |
div.setAttribute('id', bibkey.replace(':', '_')); | |
div.innerHTML = '<img src="/images/cover.png" height="80" border="0" alt="" />'; | |
debug_msg('GET COVER', bibkey); | |
} | |
if (bibkeys.length >= MAXBUFFER) { | |
gbookScript(bibkeys, 'gbook_coverart'); | |
bibkeys = []; | |
} | |
} | |
if (bibkeys.length > 0) { | |
gbookScript(bibkeys, 'gbook_coverart'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment