Jump to content

Widget:Google Books: Difference between revisions

From 313FILES
Created page with "<noinclude>__NOTOC__ This widget allows you to embed '''[https://developers.google.com/books Google Books]''' on your wiki page. Originally created by [https://www.mediawikiwidgets.org/User:Arv Arv] and adapted by [https://www.mediawikiwidgets.org/User:FrancisFranck FrancisFranck]. Further updated to meet current MediaWiki security requirements. == Using this widget == To use this widget, add the following code to your wiki page: <pre> {{#widget:Google_Books |id=BOOK_..."
 
No edit summary
Line 2: Line 2:
This widget allows you to embed '''[https://developers.google.com/books Google Books]''' on your wiki page.
This widget allows you to embed '''[https://developers.google.com/books Google Books]''' on your wiki page.
Originally created by [https://www.mediawikiwidgets.org/User:Arv Arv] and adapted by [https://www.mediawikiwidgets.org/User:FrancisFranck FrancisFranck].
Originally created by [https://www.mediawikiwidgets.org/User:Arv Arv] and adapted by [https://www.mediawikiwidgets.org/User:FrancisFranck FrancisFranck].
Further updated to meet current MediaWiki security requirements.


== Using this widget ==
== Using this widget ==
To use this widget, add the following code to your wiki page:
For information on how to use this widget, see [https://www.mediawikiwidgets.org/Google_Books widget description page on MediaWikiWidgets.org].
 
<pre>
{{#widget:Google_Books
|id=BOOK_ID
|width=600
|height=500
}}
</pre>
 
Parameters:
* '''id''' - The Google Books ID (required)
* '''width''' - Width of the embedded viewer in pixels (optional, default: 600)
* '''height''' - Height of the embedded viewer in pixels (optional, default: 500)


== Copy to your site ==
== Copy to your site ==
Line 24: Line 10:
</noinclude>
</noinclude>
<includeonly>
<includeonly>
<div id="google-books-container-{{$id|escape:'html'}}"></div>
<div id="googlebooks-<!--{$id|escape:'html'}-->"></div>
<script type="text/javascript" src="https://books.google.com/books/previewlib.js"></script>
<script type="text/javascript">
<script type="text/javascript">
(function() {
/* <![CDATA[ */
    // Validate and sanitize inputs
var width = <!--{$width|escape:'html'|default:'600'}-->;
    var bookId = "{{$id|escape:'html'}}";
var height = <!--{$height|escape:'html'|default:'500'}-->;
    if (!bookId) {
var bookId = "<!--{$id|escape:'html'}-->";
        document.getElementById("google-books-container-{{$id|escape:'html'}}").innerHTML =
if (typeof GBS_insertEmbeddedViewer === 'function') {
            '<div style="color:red">Error: Book ID is required</div>';
    GBS_insertEmbeddedViewer(bookId, width, height);
        return;
}
    }
/* ]]> */
   
    var width = parseInt("{{$width|escape:'html'|default:'600'}}") || 600;
    var height = parseInt("{{$height|escape:'html'|default:'500'}}") || 500;
   
    // Keep reasonable limits
    width = Math.min(Math.max(width, 200), 1200);
    height = Math.min(Math.max(height, 200), 1200);
   
    // Load Google Books API script securely
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = 'https://books.google.com/books/previewlib.js';
    script.onload = function() {
        // API loaded successfully, initialize viewer
        if (typeof GBS_insertEmbeddedViewer === 'function') {
            GBS_insertEmbeddedViewer(bookId, width, height);
        } else {
            document.getElementById("google-books-container-{{$id|escape:'html'}}").innerHTML =
                '<div style="color:red">Error: Google Books API failed to load correctly</div>';
        }
    };
    script.onerror = function() {
        document.getElementById("google-books-container-{{$id|escape:'html'}}").innerHTML =
            '<div style="color:red">Error: Failed to load Google Books API</div>';
    };
   
    document.head.appendChild(script);
})();
</script>
</script>
</includeonly>
</includeonly>

Revision as of 15:11, 10 March 2025

This widget allows you to embed Google Books on your wiki page. Originally created by Arv and adapted by FrancisFranck.

Using this widget

For information on how to use this widget, see widget description page on MediaWikiWidgets.org.

Copy to your site

To use this widget on your site, install MediaWiki Widgets extension and copy the complete source code of this page to your wiki as page Widget:Google Books.