<?xml version="1.0" encoding="UTF-8"?>
<Module>
<ModulePrefs title="OneBox Results for __UP_search__"
             height="150"
             author="Adam S."
             author_email="adam.feedback+onebox@gmail.com" 
             author_affiliation="Google, Inc."
             description="Find maps, weather, movie listings, news, stock quotes, local buisnesses and more without leaving your homepage.  Hit edit and enter your query.  Stays up to date."/>
<Userpref name="search" datatype="string" required="true"/>
<Content type="html">
<![CDATA[
  <script>
  // doesn't yet work reliably on...
  //  - calculator, e.g. "10 - 5" works but "45 * 21" fails (end string fails to match)
  function onebox__MODULE_ID__ (search, myfunc) {
     var url = "http://www.google.com/search?q=" + encodeURIComponent(search);
     _IG_FetchContent(url, function(response) {
       if (response == "") {
         myfunc(search,"error: no response from " + url);
         return;
       }

       var idx1 = -1;   // start of HTML
       if (idx1 == -1) {
         // froogle: <p class=e><table...> <tr...> <td...> <a href...>
         idx1 = response.search(/<table[^<]+<tr[^<]+<td[^<]+<a[^<]+Product search results/);
       }
       if (idx1 == -1) {
         // news: <p class=e><table...> <tr...> <td...> <a href...>
         idx1 = response.search(/<table[^<]+<tr[^<]+<td[^<]+<a[^<]+News results for/);
       }
       if (idx1 == -1) {
         // local: <p class=e><table...> <tr...> <td...> <a href...>
         idx1 = response.search(/<table[^<]+<tr[^<]+<td[^<]+<a[^<]+ Local results for/);
       }
       if (idx1 == -1) {
         // weather
         idx1 = response.search(/<b>Weather<.b> for <b>/);
       }
       if (idx1 == -1) {
         // maps: <p class=e><a href...>Map of...
         idx1 = response.search(/<a[^<]+Map of/);
       }
       if (idx1 == -1) {
         // phoneboox: <p class=e><table...<tr...><td...><a href...>Phonebook results for
         idx1 = response.search(/<table[^<]+<tr[^<]+<td[^<]+<a[^<]+Phonebook results/);
       }
       if (idx1 == -1) {
         // flights: <p class=e><script>function
         idx1 = response.search(/<script>function.+script><a[^>]+>Flights from/);
       }
       if (idx1 == -1) {
         // try generic
         idx1 = response.indexOf("</td></tr></table><p>")
       }
       if (idx1 == -1) {
         myfunc(search,"error: no onebox results found-- please try another query.<br>");
         return;
       }

       var idx2 = response.indexOf("<div><p class=g>");
       if (idx2 == -1) {
         myfunc(search,"error: couldn't find end of onebox content.<br>");
         return;
       }

       // success!
       var s = response.substring(idx1, idx2);
       // onebox images are hosted on www.google.com
       s = s.replace(/src=([\"])?\/images/g, "src=$1http://www.google.com/images");
       s = s.replace(/src=([\"])?\/pfetch/g, "src=$1http://www.google.com/pfetch");
       myfunc(search,s);
     });
  }
  function run__MODULE_ID__ () {
     var prefs = new _IG_Prefs(__MODULE_ID__);
     var search = prefs.getString("search");
     var divobj = _gel('remote_' + __MODULE_ID__);
     // self test
     if (search == "_test") {
       var ar = new Array;
       ar[0] = "SFO JFK";
       ar[1] = "100 main st santa cruz ca";
       ar[2] = "831-426-1244";
       ar[3] = "canon eos rebel";
       ar[4] = "weather santa cruz ca";
       ar[5] = "george bush";
       ar[6] = "goerge bush";
       for (var i=0; i < ar.length; i++) {
          onebox__MODULE_ID__(ar[i], function(search,resp) {
            if (resp.indexOf("error:") == -1) {
              divobj.innerHTML += "PASS: "+search+"<br>";
            } else {
              divobj.innerHTML += "FAIL: "+resp;
            }
          });           
       }
     } else {
       onebox__MODULE_ID__(search, function(search,s) { divobj.innerHTML = s; });
     }
  }
  run__MODULE_ID__();
  </script>
]]>
</Content>
</Module>
