Skip to content

Instantly share code, notes, and snippets.

@ftherese
Created October 29, 2009 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ftherese/221506 to your computer and use it in GitHub Desktop.
Save ftherese/221506 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>
<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">
// CHECK THIS FUNCTION
function createRequest() {
var result = null;
if (window.XMLHttpRequest) {
result = new XMLHttpRequest();
if (typeof result.overrideMimeType != 'undefined') {
result.overrideMimeType('text/xml'); // Or anything else
}
}
else if (window.ActiveXObject) {
// MSIE
try { result = new ActiveXObject("Microsoft.XMLHTTP");}
catch(err){ try{result = new ActiveXObject("MSXML2.XMLHTTP");}
catch(err){}}
}
else {
// No known mechanism -- consider aborting the application
}
return result;
}
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);
}
}
//CHECK THIS FUNCTION
for ( i = 1; i < entries.openSearch$totalResults.$t; i+=10) {
var req = createRequest(); // defined above
// Create the callback:
req.onreadystatechange = function() {
if (req.readyState != 4) return; // Not there yet
if (req.status != 200) {
// Handle request failure here...
return;
}
// Request successful, read the response
var resp = req.responseText;
// ... and use it as needed by your app.
}
var link = document.createElement('a');
link.href = '#';
link.onclick = function(){req.open("GET", 'http://books.google.com/books/feeds/volumes?q=' + encodeURIComponent(document.getElementById("theQuery").value) + '&min-viewability=partial' + + i + '&alt=json-in-script&callback=handleResults', true); return false};
var textElem = document.createElement('span');
textElem.innerHTML = i + ' ';
link.appendChild(textElem);
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 || [];
entries.openSearch$totalResults = feed.openSearch$totalResults;
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 id="theQuery" type="text" size="30" name="query" value="Bible"/>
<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>
</html>
]]></Content>
</Module>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment