如何从谷歌获取图书详细信息

时间:2016-12-03 09:58:45

标签: javascript php json google-books

如何通过google-jackets-js从谷歌获取图片,并在div内创建的img标签的帮助下显示id" gbs-thumbnail"通过javascript本身。建议也欢迎,只需要一点支持。提前谢谢

index.ctp

    <html>
    <head>
<?= $this->Html->script('google-jackets.js') ?>
        <script type="text/javascript"> 
            $(document).ready(function(){ 
                    GetCoverFromIsbn(); 
            }); 
        </script> 
    </head>
    <body>
               <li>
             <?php $r=0; $check=0; foreach($data as $rrrr)
                        {?>

                        <div class="col-md-3 col-sm-3">
                            <div class="best-seller-pro">
                                <figure> 

                <div id="bookcover"> 
                            <div id="gbs-thumbnail" class="9383692103" title="28496801">
                            </div></div>
                                </figure>      
                                    <?php echo $this->Form->end(); ?>
                                </div>
                            </div>
                        </div>
                        <?php $r++;$check=0;} ?>
    </body>
    </html>

谷歌jackets.js

    // if (typeof temp == "undefined" || !temp) {
    var temp = {};
/**
 * A namespace for Google related functions.
 */
temp.Google = {


    /**
     * Search all:
     *    <div title="biblionumber" id="isbn" class="gbs-thumbnail"></div>
     * or
     *    <div title="biblionumber" id="isbn" class="gbs-thumbnail-preview"></div>
     * and run a search with all collected isbns to Google Book Search.
     * The result is asynchronously returned by Google and catched by
     * gbsCallBack().
     */
    GetCoverFromIsbn: function(newWindow) {
        var bibkeys = [];
        $("[id^=gbs-thumbnail]").each(function(i) {
            bibkeys.push($(this).attr("class")); // id=isbn
        });
        bibkeys = bibkeys.join(',');
        var scriptElement = document.createElement("script");
        this.openInNewWindow=newWindow;
        scriptElement.setAttribute("id", "jsonScript");
        scriptElement.setAttribute("src",
            "https://books.google.com/books?bibkeys=" + escape(bibkeys) +
            "&jscmd=viewapi&callback=temp.Google.gbsCallBack");
        scriptElement.setAttribute("type", "text/javascript");
        document.documentElement.firstChild.appendChild(scriptElement);

    },

    /**
     * Add cover pages <div
     * and link to preview if div id is gbs-thumbnail-preview
     */
    gbsCallBack: function(booksInfo) {
         var target = '';
         if (this.openInNewWindow) {
            target = 'target="_blank" rel="noreferrer" ';
         }
         for (id in booksInfo) {
             var book = booksInfo[id];
             $("[id^=gbs-thumbnail]."+book.bib_key).each(function() {
                 var a = document.createElement("a");
                 a.href = book.info_url;
                 if (typeof(book.thumbnail_url) != "undefined") {
                     var img = document.createElement("img");
                     img.src = book.thumbnail_url;
                     $(this).empty().append(img);
                     var re = /^gbs-thumbnail-preview/;
                     if ( re.exec($(this).attr("id")) ) {
                         $(this).append(
                             '<div class="google-books-preview">' +
                             '<a '+target+'href="' +
                             book.info_url +
                             '"><img src="' +
                             'https://books.google.com/intl/en/googlebooks/images/gbs_preview_sticker1.gif' +
                             '"></a></div>'
                             );
                     }
                 } else {
                     var message = document.createElement("span");
                     $(message).attr("class","no-image");
                     $(message).html(NO_GOOGLE_JACKET);
                     $(this).empty().append(message);
                 }
             });
         }

     }
};

0 个答案:

没有答案