2013-06-26

Searching websites and evaluating JavaScript via Chrome’s omnibox

This blog post explains how to search several websites (not just a single search engine!) from the omnibox (address bar) in Google Chrome.

Managing search engines

The default is simple: If you enter something in the omnibox and hit return, Chrome uses your input as a search query for Google. Via “Settings → Search”, you have the option of choosing a different search engine for this operation (my Chrome has the defaults Google, Yahoo, Bing).

Next to the drop-down list with the defaults, there is the button “Manage search engines...”, which is where things get interesting. First there is a list with “default search engines” (the ones in the drop-down list), then there is a list with “other search engines”. You can add new search engines to the latter and then move them to the former by clicking the “Make default” button that appears when you hover over an appropriate item.

A search engine entry has three fields: A name, a keyword and a URL. The keyword is an abbreviation for the URL, the URL can contain the placeholder string “%s” where Chrome fills in what you have typed in order to perform a query. Lets try this out and add the following search engine to “other search engines”:

  • Name: Vimeo
  • Keyword: vi
  • URL: http://vimeo.com/search?q=%s
Now we can trigger a search on Vimeo, by typing the keyword “vi”:

Then Chrome tells us to hit Tab, after which we can enter a query:

Chrome also recognizes when you are typing the beginning of a keyword, so you can choose a longer one, if you want to (caveat: not always reliable when several keywords start with the same characters). Google’s documentation says that normal URLs work, too, but that wasn’t the case for my Chrome.

Note that Firefox has had this feature for a long time [1]. You can turn any bookmark into a search engine by assigning it a keyword. This has the added advantage that you can organize your keyword bookmarks in folders.

Automatically adding a web site to Chrome’s search engines

If you associate the right meta-data with your web site, Chrome automatically adds an entry to its “other search engines” when someone visits it. The meta-data has to conform to the OpenSearch standard. You associate it by putting the following tag into the <head> section of a web page.
    <link rel="search" type="application/opensearchdescription+xml"
                       href="data/opensearch.xml">
The file data/opensearch.xml contains the actual OpenSearch meta-data, for example:
    <?xml version="1.0" encoding="UTF-8"?>
    <OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
        <ShortName>Exemplary</ShortName>
        <Description>Use Example.com to search the Web.</Description>
        <Url type="application/rss+xml" 
             template="http://example.com/?q={searchTerms}"/>
        <Url type="application/x-suggestions+json"
             template="http://example.com/suggestions?q={searchTerms}"/>
    </OpenSearchDescription>
The first Url tag tells Chrome how to search, the second one tells it how to retrieve suggestions. As far as I can tell, Chrome automatically assigns the domain name of the URL as a keyword.

You can go to vimeo.com to see a real-world example. After you have visited that web site, there is an entry for it in your “other search engines”.

Evaluate JavaScript via a keyword

Keywords become even more powerful if you combine them with JavaScript and bookmarklets [2]. For example, you can add the following “search engine”:
  • Name: Evaluate
  • Keyword: eval
  • URL: javascript:alert(%s)
Now there is less to type if you want to evaluate a JavaScript expression:
    eval [tab] 3 * 7        → 21
    eval [tab] 'abc'.length → 3

References

  1. Browser keywords: using the address bar as a command line
  2. Tip: use JavaScript as a calculator in Firefox and Chrome

No comments: