Skip to content

Instantly share code, notes, and snippets.

@asweintraub
Created July 21, 2009 00:43
Show Gist options
  • Save asweintraub/151014 to your computer and use it in GitHub Desktop.
Save asweintraub/151014 to your computer and use it in GitHub Desktop.
var fb_searchList;
var fb_findURL = "http://www.facebook.com/ajax/typeahead_search.php";
var fb_profileURL = "http://www.facebook.com/profile.php?id=";
var fb_searchURL = "http://www.facebook.com/s.php?q=";
var fb_lastQuery = "";
var fb_lastMatches;
function fb_filter(text)
{
var matches = new Array();
if (fb_searchList == null)
{
return matches;
}
if (fb_lastQuery == "" || text.indexOf(fb_lastQuery) != 0)
{
fb_lastMatches = fb_searchList;
}
else if (fb_lastQuery == text)
{
return fb_lastMatches;
}
fb_lastQuery = text;
var count = 0;
var strings = text.toLowerCase().split(' ');
var strings2 = new Array();
for (i = 0; i < strings.length; i++)
{
if (strings[i].length == 0)
{
strings.splice(i, 1);
i--;
}
else
{
strings2[i] = " " + strings[i];
}
}
for (var i = 0; i < fb_lastMatches.length; i++)
{
var name = fb_lastMatches[i].t.toLowerCase();
var match = true;
for (var j = 0; j < strings.length; j++)
{
if ((name.indexOf(strings[j]) != 0) && (name.indexOf(strings2[j]) == -1))
{
match = false;
break;
}
}
if (match)
{
count++;
matches.push(fb_lastMatches[i]);
}
}
fb_lastMatches = matches;
return matches;
}
function fb_sort(object)
{
if (object.ty == 'u') {
return 1 + object.t;
} else if (object.ty == 'g') {
return 2 + object.t;
} else if (object.ty == 'a') {
return 3 + object.t;
} else {
return 9 + object.t;
}
}
function fb_load(callback, preview, pblock)
{
var data = {
url: fb_findURL,
success: function(response) {
var json = response.replace("for (;;);", "");
var results = Utils.decodeJson(json);
fb_searchList = results.payload.entries;
Utils.sortBy(fb_searchList, fb_sort);
if (callback != null)
{
callback();
}
},
error: function(xhr) {
displayMessage('Error! Status = ' + xhr.status);
}
};
if (preview)
{
if (pblock != null)
{
pblock.innerHTML = "Searching...";
}
CmdUtils.previewAjax(pblock, data);
}
else
{
$.ajax(data);
}
}
function fb_update(text, pblock)
{
if (fb_searchList == null)
{
return;
}
var matches = fb_filter(text);
if (matches.length > 0)
{
var listHTML = "<style>ol {margin-left: 0px; padding-left: 0px; list-style-position: inside;} li {height: 50px; margin-bottom: 10px; padding:2px 5px; } li:hover {outline: 1px solid; -moz-outline-radius: 8px} .network {padding-left: 21px;}</style><ol>";
var template = "<li><img src=\"${pic}\" style=\"float:right; clear:both;\"/><span style=\"font-size: 20px;\"><a href=\"${url}${id}\" accessKey=\"${index}\">${name}</a></span>{if network != null}<br/><span class=\"network\">${network}</span>{/if}</li>";
for (var i = 0; i < matches.length && i < CmdUtils.maxSuggestions; i++)
{
listHTML += CmdUtils.renderTemplate( template, {url: fb_profileURL, id: matches[i].i, name: matches[i].t, network: matches[i].n, pic: matches[i].it, index: (i+1)} );
}
listHTML += "</ol>"
listHTML += CmdUtils.renderTemplate("<div style=\"margin-left: 5px\">0. Search Facebook for <a href=\"${url}${query}\" accessKey=\"0\">${query}</a></div>", {url: fb_searchURL, query: text});
pblock.innerHTML = listHTML;
var tip = CmdUtils.getDocument().createElement('div');
tip.setAttribute("style", "font-size: 12px; margin-left: 5px; margin-top: 15px;");
tip.innerHTML = "Tip: You can go to any result in this preview by pressing control, alt, and the result number at the same time.";
pblock.appendChild(tip);
}
else
{
var searchbox = CmdUtils.getDocument().createElement('div');
searchbox.setAttribute("style", "font-size: 20px");
searchbox.innerHTML = "Press enter to search Facebook for <a href=\"" + fb_searchURL + text + "\" style=\"font-weight: bold;\">" + text + "</a>.";
pblock.innerHTML = "";
pblock.appendChild(searchbox);
if (fb_searchList.length == 0)
{
var note = CmdUtils.getDocument().createElement('div');
note.setAttribute("style", "font-size: 12px");
note.innerHTML = "Note: It appears you're not <a href=\"http://www.facebook.com/login.php\" style=\"text-decoration: underline; font-weight: bold;\">logged in</a> to Facebook. If you still receive this message after <a href=\"http://www.facebook.com/login.php\" style=\"text-decoration: underline; font-weight: bold;\">logging in</a>, make sure that you either enable third-party cookies or add www.facebook.com the cookie whitelist.";
pblock.appendChild(note);
}
}
}
function fb_navigate(text)
{
if (fb_searchList == null)
{
Utils.focusUrlInBrowser("http://www.facebook.com/");
}
var matches = fb_filter(text);
if (matches.length > 0)
{
Utils.focusUrlInBrowser(fb_profileURL + (matches[0].i));
}
else
{
Utils.focusUrlInBrowser(fb_searchURL + text);
}
}
CmdUtils.CreateCommand({
names: ["facebook", "fb"],
icon: "http://www.facebook.com/favicon.ico",
description: "Search for friends on Facebook",
help: "Type facebook followed by a name to search for a friend. Pressing return will take you to the first matching profile. If there aren't any matches, pressing return will take you to the Facebook search results. You need to be logged in to see matching friends.",
author: {name: "Andrew Weintraub"},
license: "GPL",
arguments: [{role: 'object', nountype: noun_arb_text, label: "friend's name"}],
previewDelay: 50,
_search: function(text, pblock) {
if (fb_searchList == null || fb_searchList.length == 0)
{
fb_load(function() {
fb_update(text, pblock);
}, true, pblock);
return;
}
pblock.innerHTML = "Searching...";
fb_update(text, pblock);
},
_goto: function(text) {
if (fb_searchList == null || fb_searchList.length == 0)
{
fb_load(function() {
fb_navigate(text);
}, false, null);
return;
}
fb_navigate(text);
},
preview: function preview(pblock, args) {
if (args.object.text.length == 0)
{
fb_searchList = null;
fb_load(function(){}, true, pblock);
pblock.innerHTML = "Go to Facebook.";
return;
}
this._search(args.object.text, pblock);
},
execute: function execute(args) {
if (args.object.text.length == 0)
{
Utils.focusUrlInBrowser("http://www.facebook.com");
return;
}
this._goto(args.object.text);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment