|
|
|
For Users For Developers |
The Google Gadgets API provides a way to put third-party content onto the Google homepage, other Google properties, and with the introduction of open syndication, any web page. A gadget is a mini-application, such as a photo album or an interactive game. The API is designed to be simple enough so that anyone can turn his or her web page or application into a gadget. For advanced programming tasks, see Part 2 of the Google Gadgets API Developer Guide. Table of ContentsIntroductionAudienceThis document is intended for people who want to use the Google Gadgets API to write gadgets. Gadgets are so easy to create that they are a good starting point if you are just learning about web programming. The Google Gadgets API consists of a few simple building blocks: XML, HTML, and JavaScript. To get started, all you need is a basic understanding of HTML. We will teach you all you need to know about XML to write gadgets. Later, as you write more sophisticated gadgets, you will probably want to learn some JavaScript if you're not familiar with it already. XML is a general purpose markup language. It describes structured data in a way that both humans and computers can read and write. XML is the language you use to write gadget specifications. A gadget is simply an XML file, placed somewhere on the internet where Google can find it. The XML file that specifies a gadget contains instructions on how to process and render the gadget. The XML file can contain all of the data and code for the gadget, or it can have references (URLs) for where to find the rest of the elements. HTML is the markup language used to format pages on the internet. The static content of a gadget is typically written in HTML. JavaScript is a scripting language you can use to add dynamic behavior to your gadgets. Note: Gadgets were formerly called modules, which is why the word "Module" appears throughout the API. "Hello, World"The easiest way to start learning about this API is to see an example. The following gadget simply displays the message "Hello, world!": <?xml version="1.0" encoding="UTF-8" ?> You can see the actual gadget specification here. Note the following about this example:
To run this example:
The text "hello.xml" is shorthand for the URL http://www.google.com/ig/modules/hello.xml. You can put the URL for any gadget specification in the Add by URL text box, including ones that you or your friends write (as long as they are posted on a public web server). Writing Your Own GadgetsGetting StartedTo get started writing your own gadgets, we recommend that you copy one of the examples and make a very simple change to it. For example:
Adding the Developer ModuleThe developer module acts a "command center" for all of the gadgets on your homepage. In addition to listing all the gadgets that you're running, it lets you add, view, and manage gadgets. The developer module gives you features that you will need if you're doing gadget development. For example, it lets you add gadgets that are "broken," which is useful when you are actively changing a gadget. To add the developer module, click here:
In the developer module you can click on individual gadget links to view their XML specifications. This is a good way to see how other gadgets are implemented. You don't need to worry about advanced developer module features for now. They are discussed in more detail in Using the Developer Module. But one feature you may want to take advantage of right away is the Cached checkbox. By default, gadget specifications are cached. You should uncheck Cached for gadgets while you are working on them. This lets you see your edits instead of the cached gadget. Alternatives for Hosting GadgetsIf you don't have access to a public web server, you can host your gadgets using Google Pages. To host gadgets using Google Pages:
For example, suppose you have a gadget called "myphonebook.xml," and you upload it to Google Pages. You could then add it to your personalized homepage with a URL such as the following: http://<Google-username>.googlepages.com/myphonebook.xml
Remember: Gadgets are PublicRemember that there is no such thing as a private gadget. Once you publish your gadget on a public website, people can find it and view it. Be sure not to include personal information, such as your telephone number or personal email address. What if you don't want your gadget to be public? Google encourages gadget authors to share their specifications. However, if you want to minimize your gadget's public visibility before you are ready to release it, here are some tips:
Creating a Gadget SpecOnce you understand how to edit and publish gadgets, you're ready to include more advanced features in your gadget specifications. The XML gadget specification consists of 3 major parts:
When writing a gadget, you should start with the <Content> section. Defining ContentThe <Content> section represents the "brains" of a gadget. The <Content> section defines the type of content, and either holds the content itself or has a link to external content. The <Content> section is where the gadget attributes and user preferences are combined with programming logic and formatting information to become a running gadget. The following table lists the <Content> attributes:
For guidelines on which content type is right for your gadget, see Which Content Type Should I Use? HTML ContentA gadget can have the type "html". This means that it contains HTML, possibly with embedded JavaScript, Flash, ActiveX, or other browser objects. The rules for an HTML gadget are as follows:
<Content type="html">
<![CDATA[ HTML here... ]]>
Here is an example of a gadget that just uses HTML. This gadget displays a clickable photograph that opens a photo album in a new HTML page:
<?xml version="1.0" encoding="UTF-8" ?>
<Module>
<ModulePrefs title="Go to Photo Album" height="250" scaling="false" />
<Content type="html">
<![CDATA[
<div style="text-align:center">
This is an example of the running gadget:
A gadget with an HTML content type can also reference an external JavaScript file: <Module>
<ModulePrefs ... />
<Content type="html"><![CDATA[
<script src="http://www.example.com/gadgets/clock/clock.js" type="text/javascript"></script>
]]></Content>
</Module>
URL ContentA gadget can have a URL content type. In this case the href= attribute provides a URL, and any other content in the gadget spec is ignored. With a URL content type, the assumption is that all information relating to the gadget's user interface and programmatic logic resides in the HTML file referenced by the URL. You do not put any HTML markup or JavaScript within the gadget itself. For example: <Module> <ModulePrefs ... /> <Content type="url" href="http://www/cgi-bin/example/gadgets/mystats.cgi" /> </Module> For a description of how to pass parameters between a url-type gadget and its target web page, see Getting User Prefs from URL-Type Gadgets. By default, gadgets are displayed in iframes. Iframes include a transparent margin if you don't put in code to specify otherwise. Gadgets with the content type html are automatically generated with the appropriate code to remove margins. However, when your content type is URL, you are responsible for managing how your gadget is displayed. To remove margins, include the following text in the HTML file referenced by your gadget: <style type="text/css"> body {margin: 0px;} </style>
You must properly escape the following characters in a type="url" gadget:
For example:
Which Content Type Should I Use?The most flexible, versatile content type is html. Almost all of the examples in this developer guide use the html content type, because it lets you take advantage of the Google Gadgets JavaScript API and other specialized gadget features. The url content type imposes more restrictions on you. The basic differences between the html and url types are as follows:
Defining User PreferencesSome gadgets need to give users a way of supplying user-specific information. For example, a game gadget might allow users to enter a preferred level of difficulty. The user preferences (<UserPref>) section in the XML file describes the user input fields that are turned into user interface controls when the gadget runs. User preferences are stored persistently. For example, this gadget displays a personal greeting based on the time of day. It lets users specify the following:
This is an example of the running gadget:
This is what the gadget looks like when the user clicks edit to modify the user preferences:
The user preferences that get turned into user interface controls in the running gadget are defined in the XML specification as follows: <?xml version="1.0" encoding="UTF-8" ?> <Module> <ModulePrefs title="Preferences for __UP_myname__" height="250" /> <UserPref name="myname" display_name="Name" required="true"/> <UserPref name="myphoto" display_name="Photo"/> <UserPref name="mychoice" display_name="Show Photo?" datatype="bool"/> <UserPref name="mycolor" display_name="Color" default_value="Yellow" datatype="enum" > <EnumValue value="Red" /> <EnumValue value="Aqua" /> <EnumValue value="Lime" /> <EnumValue value="Yellow" /> <EnumValue value="Pink" /> <EnumValue value="Orange" /> <EnumValue value="White" /> </UserPref> Note the following:
Here is the JavaScript that displays the greeting for the gadget. For a discussion of the use of __MODULE_ID__ at the end of the sample code, see the section Writing Robust Gadgets: <Content type="html"><![CDATA[ For a list of all the <UserPref> attributes, see the Reference. User preferences can be accessed from your gadget using the user preferences JavaScript API, for example: <script type="text/javascript">
// Must be constructed using the __MODULE_ID__ token. It gets replaced
// at runtime with the actual ID of your gadget.
var prefs = new _IG_Prefs(__MODULE_ID__);
var someStringPref = prefs.getString("StringPrefName");
var someIntPref = prefs.getInt("IntPrefName");
var someBoolPref = prefs.getBool("BoolPrefName");
</script>
For a list of all of the JavaScript functions, see the Reference. User Preference Substitution VariablesYou can use a substitution variable of the format __UP_userpref__ in the <ModulePrefs> or <UserPref> sections, where userpref matches the name attribute of a user preference. When the gadget runs, the string value of the corresponding user preference is substituted for the variable, unescaped. For example, in this excerpt, the value the user supplies at run time for the projects user preference is substituted for __UP_projects__ in the title_url string: <Module>
<ModulePrefs title="Build Monitor"
title_url="http://www.example.com/build/status.php?__UP_projects__"/>
<UserPref name="projects" display_name="project(s)"/>
<Content ... />
</Module>
You can see another example of this in the user preferences sample. Here are the general guidelines for using user preference substitution variables:
Using the "location" Data TypeGadgets based on Google Maps can use the location data type. The following example gadget illustrates how to use the location data type. For gadgets, the value supplied for a location data type must be a major city or postal code in the United States, Canada, or the United Kingdom. You might find that using postal codes yields a better result. When you use the location data type, you can use getString() to retrieve the longitude and latitude of the user-specified location. <Module> When reading a location preference x:
Earlier versions of the Google Gadgets API did not allow you to specify a default_value for a location type. This is no longer the case. Defining Gadget PreferencesThe <ModulePrefs> section in the XML file specifies characteristics of the gadget, such as title, author, preferred sizing, and so on. For example: <Module> The users of your gadget cannot change these attributes. For a full list of the <ModulePrefs> attributes, see the Reference. Adding Homepage Gadgets to Other Google PropertiesThe personalized homepage is not the only Google product that supports gadgets. You can also add homepage gadgets to Google Desktop and Google Pages (still experimental). CompatibilityYou can add most homepage gadgets to Google Desktop and Google Pages. However, homepage gadgets are not completely interchangeable with Desktop and Pages gadgets. Here are some of the limitations:
Adding Homepage Gadgets to Google DesktopTo add a homepage gadget the Desktop via the Desktop Sidebar:
Adding Homepage Gadgets to Google PagesNote: Gadgets are still considered an experimental feature in Google Pages. To enable them, choose Site settings (upper right corner) in the Page Manager. On the Settings page, click Enable experimental features. Then click Yes. To add a homepage gadget to Google Pages:
Where to Go From HereFor a description of advanced programming tasks, see Part 2 of the Developer Guide.
|
||||||||||||||||||||
![]() |