Finding Images on a Specific Site

March 8, 2010

One feature of the AJAX Image Search API that you might find useful is the ability to retrieve only the images which are visible on a specific website. For example, you could add a search box that allows people to search through just the images on your own site or you could create a slideshow which shows images from your favorite site.

To specify a site, use the setSiteRestriction method on an ImageSearch object. Here is a simple example:

http://code.google.com/apis/ajax/playground/#site_restrict

We can do more than just provide a site-specific image search box, we could also use the search results in a unique way. For example, we could create a slideshow which shows images which match our desired keyword and appear on a specific site. For this example, let's create a simple slideshow that displays images from nasa.gov.

var imageIndex = 0;
var images;

function nextImage() {
imageIndex++;
if (imageIndex >= images.length) {
imageIndex = 0;
}

var imageContainer = document.getElementById(
'image-container');
imageContainer.src = images[imageIndex].tbUrl;
}

function searchComplete(searcher) {
if (searcher.results && searcher.results.length > 0) {
var contentDiv = document.getElementById(
'content-slideshow');
contentDiv.innerHTML = '';

var imageTag = document.createElement('img');
imageTag['id'] = 'image-container';
imageTag['src'] = searcher.results[imageIndex].tbUrl;
images = searcher.results;

contentDiv.appendChild(imageTag);

// Switch to the next image every 5 seconds.
setInterval("nextImage();", 5000);
}
}

function slideshowOnLoad() {
var imageSearch = new google.search.ImageSearch();
imageSearch.setSiteRestriction('nasa.gov');
imageSearch.setSearchCompleteCallback(
this, searchComplete, [imageSearch]);
imageSearch.execute('supernova');
}

google.setOnLoadCallback(slideshowOnLoad);

In the above samples, there are three lines I'd like to call your attention to. The first line to note is the imageSearch.execute at the bottom, here we've entered the keywords that our slideshow images should be related to. Next we restrict the site to nasa.gov using imageSearch.setSiteRestriction. Lastly, we call setInterval once we receive the results of our search for images. The setInterval call tells the browser to run our nextImage function every five seconds.

Here are the two samples we've talked about in action:

The site restriction can also include a path within a website. For example you could do setSiteRestriction(
'http://www.flickr.com/photos/<username>')
to search the photos that have been posted by a particular user on flickr.

To learn more about some other neat features of the AJAX Image Search API take a look at our code playground samples and documentation. For questions on this and other topics, drop us a line in the discussion group.

New Parameter for Server Side API Calls

January 19, 2010

Over the last several years, you've helped make Google's AJAX APIs incredibly successful. Not surprisingly, however, there are some people who try to take advantage of these free APIs by using them in ways that they were not designed for, abuse which is prohibited by the Terms of Use. Specifically, some servers are making countless requests - requests not made on the behalf of an end-user - in an attempt to scrape data from the APIs.

To help us discourage this behavior without affecting legitimate developers, we're adding a new parameter to the RESTful interface, userip. With this parameter, developers have the option of supplying the IP address of the end-user on whose behalf they are making the API request. Doing so will help us distinguish this legitimate server-side traffic from the more abusive scraping in which there are no end-users.

Use of this new parameter is *not* required. However, if it is not included with server-side requests, those requests are more likely to be interpreted and automatically blocked as abuse, especially in situations where a server is sending a high volume of traffic to the API. Additional safeguards you can take include setting setting a valid HTTP referer (as required by our Terms of Use) and using an API key. These additional measures will help us contact you in case there are problems with your website or application (sometimes a programming error results in massive traffic, forcing us to block your access if we are unable to contact you). In choosing to utilize this parameter, please be sure that you're in compliance with any local laws, including any laws relating to disclosure of personal information being sent.

Check the documentation for usage of the new parameter. We'd love to hear any comments, questions or problems you're having in the support forum.

Web Search in Your Country

October 12, 2009

I am happy to announce the addition of the ability to scope your searches to a specific country in the AJAX Web Search API. Now, if you have a lot of visitors in Madagascar, you can make sure that the search results displayed on your site are tailored to them. All it takes is a small change to your code.

There are three possible ways to implement, depending on how you're using the API:

  1. If you use the loader, you can simply load jsapi on the domain you're interested in (example), such as:
    <script src="http://www.google.es/jsapi"></script>

  2. Alternately, you can set this with the web search object's .setRestriction method (example):
    var ws = new google.search.WebSearch();
    ws.setRestriction(google.search.Search.RESTRICT_EXTENDED_ARGS,
    {'gl' : 'es'});
  3. Finally, if you're using the RESTful interface, all you have to do is append a "gl" URL parameter to your request:
    http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=flowers&gl=fr

Most valid country codes will work, as long as Google has a home page on that country's top level domain (e.g. google.es). If you use an invalid or unsupported country code, you'll get an error message letting you know.

We're excited to bring you this addition to the API, and look forward to seeing the innovative ways in which you use this new feature to improve your users' experience. Please drop us a line with your thoughts (or questions) on our discussion group.

Google Chrome Frame Ajax Detection

September 28, 2009

In partnership with the Google Chrome Frame team, we are making available a library to allow your web application to detect the presence of Google Chrome Frame. We on the Ajax team are excited about the possibilities of this add-on improving JavaScript performance and enabling some of the new features available in HTML5. If you have a web application which makes use of these new features, you can use this library to prompt the user to install Google Chrome Frame, or recognize when a user has just installed it. The library provides granular controls so that you can create the user experience which best suits your site.

As a short example, I've created the following simple demo which just detects whether Google Chrome Frame is installed or not with an alternate message if you are in a browser for which this plugin is not available.

Do you have Google Chrome Frame installed?
We're checking on that now.


Ben Lisbakken has also added detection for Google Chrome Frame to the Ajax Playground. If you view source on the page you can see another example of a customized CFInstall.check implementation which is designed to fit the page.

For more details on the Google Chrome Frame Ajax API, see the documentation and for questions, please visit the discussion group.

More Languages, More Keyboards

September 15, 2009

The language APIs keep right on trucking, released recently are a handful of new translation languages, pairs, and keyboard layouts.

We've added the ability to use machine translation to or from the following languages:

  • Afrikaans
  • Belarusian
  • Icelandic
  • Irish
  • Macedonian
  • Malay
  • Persian
  • Swahili
  • Welsh
  • Yiddish

With the addition of the above the total count for language pair combination comes to a mind boggling 2550 pairs. In addition, we find the above additions exciting because, for the first time, African languages are available through the API and we now support all 23 Official European Union languages.

A few months ago we announce our virtual keyboard API and this month we've added nine new keyboard layouts:

  • Bulgarian
  • Czech
  • Greek
  • Hebrew
  • Hungarian - 101 layout
  • Slovak
  • Slovenian
  • Turkish - Q layout
  • Ukrainian - 101 layout

Here's a simple example of using the Slovak keyboard layout.

Custom Search with Custom Style: Peanut Butter and Jelly

August 10, 2009

Creating a custom look and feel for your website can have significant benefits in everything from improving usability to setting a professional or playful tone for your website. In many cases, letting users search the content of your site and related sites gets them the information they need faster. After all, a speedy user experience is a happy user experience. Here are some examples of how Custom Search and custom styles are as easy (and delicious) as peanut butter and jelly.

We start with a Custom Search Element, which uses the CustomSearchControl to add a Custom Search Engine to my web page. If you've never used a Custom Search engine before, I think you'll find a lot to love: it uses Google's search technology to include a specific group of websites for indexing, and you can share in ad revenue.

One of the many benefits of using the AJAX Search APIs to dynamically add search capabilities to your web pages is that you can also control the look and feel of the search input and results by using open web standards like cascading style sheets (CSS). When you combine this styling with Custom Search, you can create a seamless search experience for your users.

You can begin by changing the search input box (dynamically added to your page by default) to use an input box that you've placed on the page wherever you like.

// Set drawing options to use my text box
// as input instead of having the library create one.
var drawOptions = new google.search.DrawOptions();
drawOptions.setInput(
document.getElementById('query_input'));

// Draw the control in content div
customSearchControl.draw(
'results', drawOptions);

With the above changes we get a page that looks like this:

Now that we're able to use a Custom Search box (look ma, no button push required!) we can add CSS rules to change fonts, colors, and more in the search results.

For example, see: http://ajax-apis.appspot.com/cse-style which has CSS rules that effect the styling of the search results and compare it to our first step which uses the default styles.

Take a look at the CSS rules to get an idea for how this works, and how you can do custom styling to fit your own website.

We can change the font and add a border around each search result:

#results .gsc-results {
/* Sets font for titles, snippets, and URLs. */
font-family: arial, helvetica, sans-serif;
}

#results .gsc-result {
position: relative;
border: 1px solid #eee;
border-left: 10px solid #eee;
padding: 8px 8px 8px 20px;
border-radius: 8px;
-webkit-border-radius: 8px;
-moz-border-radius: 8px;
}

We can also change the style of a single result when the user moves the mouse cursor over it:

#results .gsc-result:hover {
border: 1px solid #888;
border-left: 10px solid #888;
}

These are just a couple of examples. Since the CSS styling is handled by the browser, you can use any supported CSS rules to select portions of the search results. For more information on the result HTML structure and the CSS classes you may want to select in your own customizations, see the documentation on styling AJAX Search results.

Behind the scenes with two AJAX API Developers

July 20, 2009

We enjoy featuring real-world applications that show how versatile the AJAX APIs can be. Below we're highlighting two very different apps and the developers who built them:

AroundMe
AroundMe is an iPhone application where the AJAX APIs are central to the user experience. Specifically, the app utilizes the Local Search API to enable users to find information about their surroundings. In the videos below, Marco Pifferi (the developer behind AroundMe) gives a demo of his app, his thoughts on using the AJAX APIs, and tips for integrating them in mobile apps.



Mibbit
Mibbit is a web-based chat application that uses the AJAX APIs to enhance the chat experience. The language APIs help users to translate their messages into a number of different languages. Mibbit also uses the Maps API and YouTube API to display embedded maps or YouTube videos if a user includes a Maps or YouTube URL in their chat. Jimmy Moore, creator of Mibbit, walks through Mibbit in his video below.



Do you have a great app that uses the AJAX APIs? Submit a video about it and we may feature it on this blog. Questions? Stop by our support forum or IRC channel.

Introducing the Virtual Keyboard API

June 22, 2009

It is often difficult for Internet users to input text in many non-Latin script-based languages for a variety of reasons. The correct keyboard layout may not be installed on the computer they're using - sometimes such a layout may not be well developed or widely available. This poses a challenging problem for web developers because there is no way they can ensure that their users have access to this very basic input technology. Our Transliteration API can help, but requires that the user know multiple languages.

Right on the heels of introducing support for translating Persian (Farsi), we've added a new Virtual Keyboard API into the Google AJAX Language API to further assist with text input. With this, developers can help their users input text without relying on the right software being installed on the computer they happen to be using.

It couldn't be easier to get this on your page. Simply load the right package:

google.load("elements", "1", {
packages: "keyboard"
});

Then create a keyboard, specifying the keyboard layout and text field to bind to:
var kbd = new google.elements.keyboard.Keyboard(
[google.elements.keyboard.LayoutCode.Arabic],
['myTextArea']);

And here's what it looks like:

This gives you the control to provide a better user experience, even for multilingual websites. By creating multiple keyboards with different layouts, each text field can be bound to the appropriate keyboard - and the user will see only the keyboard attached to whichever text field has the focus.

But don't take my word for it - check out a sample for yourself. Notice that in addition to allowing users to click on the virtual keyboard, it also temporarily transforms the key assignments on their physical keyboard, allowing rapid typing for those users accustomed to a given layout.

With this initial release, we are launching 5 language layouts. They are:

  • Arabic (العربية)
  • Hindi (हिन्दी)
  • Polish (Polski)
  • Russian (Русский)
  • Thai (ไทย)


We plan to roll out support for more keyboard layouts in the future. But in the meantime, read through the class reference and see the rest of the Code Playground samples.

After you've had time to experiment, let us know what you think and which other layouts you'd like to see. Feedback is always welcome in our support forum and IRC channel.

Ext-core ready to go

June 11, 2009

Recently, the guys over at Ext JS released ext-core under an MIT license, which was a big win for open source! Today, they released a stable (non-beta) version of this library and we are proud to be part of that announcement by hosting the new version. You can now pull ext-core from Google servers:

// directly access it
http://ajax.googleapis.com/ajax/libs/ext-core/3.0.0/ext-core.js
// alias the newest 3.0.x version
http://ajax.googleapis.com/ajax/libs/ext-core/3.0/ext-core.js
// alias the newest 3.x.x version
http://ajax.googleapis.com/ajax/libs/ext-core/3/ext-core.js
// directly access the uncompressed code
http://ajax.googleapis.com/ajax/libs/ext-core/3.0.0/ext-core-debug.js
You can also use it from the loader:
google.load('ext-core', '3.0');
google.load('ext-core', '3', {uncompressed : true});
Thanks to all for the requests to add ext-core to our Libraries API, and big thanks to Ext JS for providing their library so openly! For more information, head over to their blog post.

Arabic Transliteration added to the AJAX Language API

June 8, 2009

Last year we launched the Transliteration API as an addition to the AJAX Language API. This gave users the ability to transliterate (the process of phonetically converting words from one script into another) text into several Indic languages using a normal English keyboard.

We're happy to announce that we've now added Arabic to the list of supported languages. Now, you can allow your users to easily input Arabic-language text into any text field or text area on your web page without switching to a non-Latin alphabet keyboard - just like on the Labs page. For example, if the user types 'mar7aban bekom', the API will transliterate each word, with the result 'مرحبا بكم' (Arabic for welcome). The API will even automatically adjust the direction of the text area to support this right-to-left language.

Take a look at the documentation and then head over to the Code Playground to give it a try for yourself. If you have any questions, stop by the Google AJAX API developer forum or IRC channel.