Skip to content

Instantly share code, notes, and snippets.

@eversonl
Created April 8, 2010 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eversonl/359976 to your computer and use it in GitHub Desktop.
Save eversonl/359976 to your computer and use it in GitHub Desktop.
Bing Serches
bingcommand("bing");
bingcommand("python", "docs.python.org", "http://www.python.org/favicon.ico");
bingcommand("django", "docs.djangoproject.com", "http://www.djangoproject.com/favicon.ico");
bingcommand("jquery", "api.jquery.com", "http://www.jquery.com/favicon.ico");
bingcommand("php", "www.php.net", "http://www.php.net/favicon.ico");
bingcommand("javascript", "developer.mozilla.org", "https://developer.mozilla.org/favicon.ico");
bingcommand("w3.org", "www.w3.org");
bingcommand("lifehacker", "lifehacker.com", "http://www.lifehacker.com/favicon.ico");
bingcommand("snownews", "0-21.co.uk", "http://www.0-21.co.uk/favicon.ico");
bingcommand("emacs", "emacswiki.org", "http://emacswiki.org/favicon.ico");
bingcommand("flex", "livedocs.adobe.com/flex/3", "http://livedocs.adobe.com/favicon.ico");
function bingcommand(name, base, iconurl) {
CmdUtils.CreateCommand(
{names: [name],
icon: (typeof iconurl == "undefined" ?
"http://www.bing.com/favicon.ico" :
iconurl),
description: "Search "+name,
help: "Search "+name+". Press Alt+n to open result number n. For example, Alt+2 will open the second result.",
author: {name:"Lee Everson"},
arguments: [{role:"object", label:"keywords", nountype: noun_arb_text}],
execute: function(args) {
Utils.openUrlInBrowser("http://www.bing.com/search?q="+
encodeURIComponent(args.object.text));
},
cache: {},
// sometime the ajax queries don't come back in order. This variable
// helps us make sure we display the last result.
lastrefresh: {time:0},
preview: function(out, args) {
var keywords = $.trim(args.object.text),
cache = this.cache;
lastrefresh = this.lastrefresh;
if (keywords in cache) {
out.innerHTML = cache[keywords];
return;
}
bingsearch(keywords, function(buf, data, time) {
if (lastrefresh.time > time) return;
lastrefresh.time = time;
out.innerHTML = buf;
cache[keywords] = buf;
}, base);
}});
}
function bingsearch(keywords, cb, base) {
base = (typeof base != "undefined" && base != null ?
"site:"+base+" " : "");
if (!keywords.length)
return cb("Type your keywords", []);
$.getJSON(
"http://api.search.live.net/json.aspx?JsonCallback=?",
{Query: base + keywords,
"Web.Count":9,
AppId:"C616BF0FB34B57F01CDC2CB2D333D0741BCCF570",
Market:"en-GB",
JsonType:"callback",
Sources:"Web RelatedSearch",
Adult:"Off",
Version:"2.2"},
// format the output
function(data) {
if (!data.SearchResponse.Web.Total)
return cb("No results for this search<br>", [], (new Date()).getTime());
var msg = data.SearchResponse.Web.Results;
var accesskey = 1;
// break the keywords into a list, quoted text count as 1 element
var keys = [];
keys = keys.concat(keywords.replace(/"(.*?)"/gi, function(s, m) {
keys.push(m);
return "";
}).split(" "));
// format each message into the template
var results = $.map(msg, function(x) {
if (!x) return "";
x.Title = (x.Title ? sanitize(x.Title) : "");
x.Description = (x.Description ? sanitize(x.Description) : "");
$.each(keys, function(i, key) {
if (key.length < 3) return;
x.Title = (x.Title ? colorize(key, x.Title) : "");
x.Description = (x.Description ? colorize(key, x.Description) : "");
});
// this avoids conflits with keywords
x.Title = x.Title.replace(/@@s/g, "<span style='color:#FFA500'>").
replace(/@@e/g, "</span>");
x.Description = x.Description.replace(/@@s/g, "<span style='color:#FFA500'>").
replace(/@@e/g, "</span>");
x.Domain = (base ? "":
(" <font color='#00BFFF'>"+
x.Url.match(/.*\/\/([a-z0-9.-]+).*/i)[1]+
"</font>"));
x.accesskey = accesskey++;
return CmdUtils.renderTemplate(
"<p><span style='background-color: #ccc; color: black; padding: 0 3px; margin: 5px;font-weight: bold; border: 1px solid #888;'>${accesskey}</span>"+
"<a accesskey='${accesskey}' style='text-decoration: none' "+
"href='${Url}'><span style='font-weight:bold'>${Title}</span>"+
"${Domain}<br>"+
"${Description}</a></p>", x);
}).join("");
// return the html
return cb(CmdUtils.renderTemplate(
"<p><b>${Total}</b> results. Related searches: ${Related}</p>",
{Total:humanizednumber(data.SearchResponse.Web.Total, ","),
Related:data.SearchResponse.RelatedSearch ?
$.map(data.SearchResponse.RelatedSearch.Results.slice(0, 7),
function(x) { return "<i style='color: #32CD32;'>"+x.Title+"</i>"; }).join(" - "):
"no results."}
) + results, msg, (new Date()).getTime());
});
}
/////////////
// helpers //
/////////////
function trace(x) { CmdUtils.log(x); }
function sanitize(str) {
return str.replace("<", "&lt;").replace(">", "&gt;");
}
function colorize(key, str) {
var s = "+|{}()[].*?\\";
for (var i = 0; i < s.length; i++)
key = key.replace(s.charAt(i), "\\"+s.charAt(i));
key = key.replace(" ", "[ _,-]");
return str.replace(new RegExp(key, "gi"),
function(s) {
return "@@s"+s+"@@e"; })
}
function humanizednumber(n, sep) {
function reverse(s) {
return s.split("").reverse().join("");
}
return reverse(reverse(String(n)).match(/(\d{1,3})/gi).join(sep));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment