73
edits
(→title) |
|||
| (9 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
{{draft}} | {{draft}} | ||
Author: Christian Sonne | Author: Christian Sonne, satyr | ||
== Documentation of CmdUtils.makeSearchCommand == | == Documentation of CmdUtils.makeSearchCommand == | ||
| Line 122: | Line 122: | ||
=== postData === | === postData === | ||
Supplying postData will make Ubiquity use POST instead of GET when retrieving the results. It can be either a string | Supplying postData will make Ubiquity use POST instead of GET when retrieving the results. It can be either a string or an object. i.e.: | ||
<code>{ | * <code>"q={QUERY}&hl=en"</code> | ||
* <code>{q: "{QUERY}", hl: "en"}</code> | |||
=== parser === | === parser === | ||
| Line 130: | Line 131: | ||
Via a number of options, you can tell mSC how to parse the data returned by the search provider. | Via a number of options, you can tell mSC how to parse the data returned by the search provider. | ||
All selectors used in the following sections are either paths into the JSON data in dot-notation, or [http://docs.jquery.com/Selectors jQuery selectors]. Note that the latter are '''''NOT''''' CSS selectors albeit very similar. Some features you might be used to from CSS are missing, whilst others still are added. Which you should use depend on your choice of [[#type]]. | |||
Each selector can alternatively be a function that receives its context and returns the expected result for each mode. | |||
* jQuery mode: jQuery object | |||
* JSON mode: String if [[#container]] is specified. Object/Array otherwise. | |||
==== type ==== | ==== type ==== | ||
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 | 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 ==== | ||
If using the | If using the jQuery parser, this must be a selector that matches each result, thus returning a list of result containers. When using the json parser it must match the parent of all the results, ie: "responseData.results". | ||
It is important to note that in the case of the | It is important to note that in the case of the jQuery parser, all details for each individual result must be contained in each container. | ||
'''NB:''' Whilst this is not strictly speaking a required option for the | '''NB:''' Whilst this is not strictly speaking a required option for the jQuery parser, it is highly recommended that you use it. If not present, mSC will attempt to consolidate the lists of titles, previews and thumbnails into individual results as best it can, but it is a much more fragile method of parsing. | ||
==== title ==== | ==== title ==== | ||
Selector for the title of the result. If using the | Selector for the title of the result. If using the jQuery parser, it can alternatively be a function that receives a single argument (the jQuery result object) and must return a string that will be used as title. | ||
'''NB:''' This options is required if you use the parser | '''NB:''' This options is required if you use the parser. | ||
==== | ==== body ==== | ||
Selector for the | Selector for the content of the result. | ||
==== href ==== | ==== href ==== | ||
Selector for the | Selector for the URL of the result. | ||
'''NB:''' This option can usually be left out. If not present, mSC will attempt to find an anchor in the result matched by the [[#title]] option. | '''NB:''' This option can usually be left out. If not present, mSC will attempt to find an anchor in the result matched by the [[#title]] option. | ||
==== thumbnail ==== | ==== thumbnail ==== | ||
Selector for the | Selector for the thumbnail image of the result. If this does not match an image element, mSC will look for a child element that is an image, and use the first found as thumbnail. | ||
==== maxResults ==== | ==== maxResults ==== | ||
Integer specifying the maximum number of results displayed in the Ubiquity preview. If not specified, it will default to 4. | Integer specifying the maximum number of results displayed in the Ubiquity preview. If not specified, it will default to 4. | ||
==== | ==== baseUrl ==== | ||
String used as base | 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 172: | Line 183: | ||
== Examples == | == Examples == | ||
=== The bare | === The bare essential === | ||
<pre>CmdUtils.makeSearchCommand({url:"http://google.com/search?q=%s"})</pre> | |||
=== Adding | === Adding preview === | ||
<pre> | |||
CmdUtils.makeSearchCommand({ | |||
name: "YouTube", | |||
url: "http://www.youtube.com/results?search_query={QUERY}", | |||
parser: { | |||
container: "div.video-entry", | |||
title: "div.video-long-title a", | |||
body: "div.video-description", | |||
thumbnail: "img.vimg120", | |||
}, | |||
}); | |||
</pre> | |||
=== Using the JSON parser === | === Using the JSON parser === | ||
<pre> | |||
CmdUtils.makeSearchCommand({ | |||
name: "Google", | |||
url: "http://www.google.com/search?q={QUERY}", | |||
parser: { | |||
type: "json", | |||
url: "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q={QUERY}", | |||
container: "responseData.results", | |||
title: "titleNoFormatting", | |||
body: "content", | |||
href: "url", | |||
html: ["body"], | |||
}, | |||
}); | |||
</pre> | |||
=== Using functions in the parser === | === Parsing XML / Using functions in the parser === | ||
<pre> | |||
CmdUtils.makeSearchCommand({ | |||
url: "http://wunderground.com/cgi-bin/findweather/getForecast?query=%s", | |||
parser: { | |||
type: "xml", | |||
url: "http://api.wunderground.com/auto/wui/geo/ForecastXML?query=%s", | |||
container: "simpleforecast > forecastday", | |||
title: "pretty", | |||
body: function() $("<div>").append( | |||
$("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", | |||
}), | |||
}, | |||
}); | |||
</pre> | |||
=== Searching with a different charset === | === Searching with a different charset === | ||
<pre> | |||
CmdUtils.makeSearchCommand({ | |||
names: ["video.baidu", "百度视频"], | |||
url: "http://video.baidu.com/v?word={QUERY}", | |||
charset: "gb2312", | |||
parser: { | |||
container: "#result td", | |||
title: ".r a", | |||
thumbnail: "img", | |||
maxResults: 20, | |||
}, | |||
}); | |||
</pre> | |||
edits