Labs/Ubiquity/Writing A Search Command: Difference between revisions

No edit summary
 
(5 intermediate revisions by the same user not shown)
Line 135: Line 135:
Each selector can alternatively be a function that receives its context and returns the expected result for each mode.
Each selector can alternatively be a function that receives its context and returns the expected result for each mode.
* jQuery mode: jQuery object
* jQuery mode: jQuery object
* JSON mode: string if [#contianer] is specified, object/array otherwise.
* JSON mode: String if [[#container]] is specified. Object/Array otherwise.


==== type ====
==== type ====
Specifies the type of remote data to retrieve. (same as <code>jQuery.ajax()</code>'s <code>dataType</code> option). If <code>"json"</code> is passed, the parser switches to JSON mode.
Specifies the type of remote data to retrieve. (same as <code>[http://api.jquery.com/jQuery.ajax/ jQuery.ajax()]</code>'s <code>dataType</code> option). If <code>"json"</code> is passed, the parser switches to JSON mode.


==== url ====
==== url / postData ====
Alternative url used in case the preview uses a different service or API than the execute function. i.e.: "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={QUERY}"
Alternative URL and/or POST Data used in case the preview uses a different service or API than the execute function. i.e.: "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={QUERY}"


==== container ====
==== container ====
Line 171: Line 171:
==== baseUrl ====
==== baseUrl ====
String used as base URL if [[#href]] and/or [[#thumbnail]]'s src is found to be relative. If not provided, [[#url]] will be used instead.
String used as base URL if [[#href]] and/or [[#thumbnail]]'s src is found to be relative. If not provided, [[#url]] will be used instead.
==== plain ====
Array of strings naming selector properties (<code>"title", "body", "href", "thumbnail"</code>) that should be treated as plain text.
==== log ====
Function to which the command logs the reponse data and parsed results.


=== Other ===
=== Other ===
Line 180: Line 186:
<pre>CmdUtils.makeSearchCommand({url:"http://google.com/search?q=%s"})</pre>
<pre>CmdUtils.makeSearchCommand({url:"http://google.com/search?q=%s"})</pre>


=== Adding bodies ===
=== Adding preview ===
<pre>
<pre>
CmdUtils.makeSearchCommand({
CmdUtils.makeSearchCommand({
Line 211: Line 217:
</pre>
</pre>


=== Using functions in the parser ===
=== Parsing XML / Using functions in the parser ===
<pre>
<pre>
// ToDo: Not a good example any more
CmdUtils.makeSearchCommand({
CmdUtils.makeSearchCommand({
  name: "MovieZoo",
   url: "http://wunderground.com/cgi-bin/findweather/getForecast?query=%s",
   url: "http://www.moviezoo.dk/alle-produkter/soeg/{QUERY}",
   parser: {
   parser: {
     container: "table.nyhedsbrev_tabel2",
    type: "xml",
     title: "tr:nth-child(2) tr tr:first-child b",
    url: "http://api.wunderground.com/auto/wui/geo/ForecastXML?query=%s",
     href: function () $("a.nyhedsbrev", this),
     container: "simpleforecast > forecastday",
    body: function () $("table table tr:not(:first)", this),
     title: "pretty",
     thumbnail: "img.lister_img",
     body: function() $("<div>").append(
     maxResults: 3,
      $("conditions", this), "<br/>",
      $("<b>", {
        text: $("low > celsius", this).text() + "\u2103",
        style: "color:#66f",
      }),
      " \uFF5E ",
      $("<b>", {
        text: $("high > celsius", this).text() + "\u2103",
        style: "color:#f66",
      })),
     thumbnail: function() $("<img>", {
      src: "http://icons-pe.wxug.com/i/c/a/" + $("icon", this).text() + ".gif",
     }),
   },
   },
});
});
73

edits