Skip to content

Instantly share code, notes, and snippets.

@ftherese
Created October 28, 2009 21:34
Show Gist options
  • Save ftherese/220863 to your computer and use it in GitHub Desktop.
Save ftherese/220863 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title = "Google Book Search" ><Require feature="dynamic-height" /></ModulePrefs>
<Content type="html"><![CDATA[
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Book Search Embedded Viewer API Example</title>
<script src="http://www.google.com/uds/api?file=uds.js&v=1.0"
type="text/javascript"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<link href="http://www.google.com/uds/css/gsearch.css" rel="stylesheet"
type="text/css"/>
<script src="http://www.google.com/uds/solutions/bookbar/gsbookbar.js"
type="text/javascript"></script>
<script src="http://books.google.com/books/previewlib.js"></script>
<link href="http://www.google.com/uds/solutions/bookbar/gsbookbar.css"
rel="stylesheet" type="text/css"/>
<style type="text/css">
body, table, p {
background-color: white;
font-family: Arial, sans-serif;
font-size: 13px;
}
td { vertical-align : top; }
#bookBar {
width : 70%;
margin-right: 5px;
margin-left: 5px;
padding-top : 4px;
padding-right : 10px;
padding-left : 10px;
padding-bottom : 20px;
}
a { padding-right : 10px;}
</style>
<script type="text/javascript">
function addBookToBar(entries) {
var myClosure = function(identifier){return function() {loadBook(identifier)}};
var myOtherClosure = function(entry){return function()
{
document.getElementById("infoBox").innerHTML='Title: ' + entry.dc$title[0].$t + '<br /><br />' + 'Author: ' + entry.dc$creator[0].$t + '<br /><br />' + 'Publisher: ' + entry.dc$publisher[0].$t + ' ' + entry.dc$date[0].$t + '<br /><br />' + 'Description: ' + entry.dc$description[0].$t + '<br /><br />' + 'Pages: ' + entry.dc$format[0].$t +'<br /><br /><a href="' + entry.link[1].href + '" target="_blanc" >' + 'Preview</a>';
}};
document.getElementById("bookBar").innerHTML='';
for (var i = 0; i < entries.length; ++i) {
var entry = entries[i];
var embeddableValue = entry.gbs$embeddability.value;
var isEmbeddable = (embeddableValue ==
'http://schemas.google.com/books/2008#embeddable');
var identifier = entry.dc$identifier[0].$t;
var link = document.createElement('a');
link.onclick=myClosure(identifier);
link.onmouseover=myOtherClosure(entry);
var img = document.createElement('img');
img.setAttribute('src', entry.link[0].href);
link.appendChild(img);
if (isEmbeddable) {
document.getElementById("bookBar").appendChild(link);
}
}
gadgets.window.adjustHeight();
}
function openBookByTitle(title) {
showCanvas(false);
showStatus('Searching for ' + title + '...');
beginSearch(title);
}
function beginSearch(query) {
// Dynamically load the search results in JavaScript,
// using the Data API JSON interface
// http://code.google.com/apis/gdata/json.html
// Once loaded, handleResults is automatically called with
// the result set
var script = document.createElement("script");
script.src = 'http://books.google.com/books/feeds/volumes?q='
+ encodeURIComponent(query) + '&min-viewability=partial'
+ '&alt=json-in-script&callback=handleResults';
script.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(script);
}
function handleResults(root) {
// Find the identifier of the first embeddable match
// If none found, report an error
var feed = root.feed;
var entries = feed.entry || [];
addBookToBar(entries);
for (var i = 0; i < entries.length; ++i) {
var entry = entries[i];
var embeddableValue = entry.gbs$embeddability.value;
var isEmbeddable = (embeddableValue ==
'http://schemas.google.com/books/2008#embeddable');
var identifier = entry.dc$identifier[0].$t;
if (isEmbeddable) {
loadBook(identifier);
return;
}
}
showStatus('Could not find a match');
}
function loadBook(identifier) {
// Load the Book Search Embedded Viewer API, calling
// showBook when it's ready
var callbackFn = function() { showBook(identifier); };
google.load("books", "0", { "callback" : callbackFn });
}
function showBook(identifier) {
// We have the book ID, API is loaded, now just show it
var canvas = document.getElementById('viewerCanvas');
viewer = new google.books.DefaultViewer(canvas);
viewer.load(identifier);
showCanvas(true);
showStatus('');
gadgets.window.adjustHeight();
}
function showCanvas(showing) {
var canvasDiv = document.getElementById('viewerCanvas');
canvasDiv.style.display = (showing) ? 'block' : 'none';
}
function showStatus(string) {
var statusDiv = document.getElementById('viewerStatus');
var showing = !(string == null || string.length == 0);
statusDiv.innerHTML = (showing) ? string : '';
statusDiv.style.display = (showing) ? 'block' : 'none';
}
</script>
</head>
<body>
<p>This page automatically opens a viewer with the first embeddable book
that matches the search term.</p>
<form name="inputForm"
onsubmit="openBookByTitle(this.query.value); return false;"
method="get">
<input type="text" size="30" name="query" value=""/>
<input type="submit" value="Go!"/>
</form>
<div id="infoBox" style="width:30%;float:right;background:#dddddd;height:100%;">Box floating on the right of the page</div><div id="bookBar"></div>
<div id="viewerStatus"
style="padding: 5px; background-color: #eee; display: none"></div>
<div id="viewerCanvas"
style="width: 70%; height: 600px; display: none"></div>
<script>openBookByTitle('Bible');</script>
</body>
]]></Content>
</Module>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment