Create a gist now

Instantly share code, notes, and snippets.

@dhou /x
Created Mar 5, 2009

What would you like to do?
//Twitter search + Google search
//Migrated to Ubiquity from Greasemonkey script: http://userscripts.org/scripts/show/43451
//houyr@twitter
//with ubiq cmd hack by satyr http://gist.github.com/74368
//function cmd_
function pageLoad_twitterSearchPlusGoogle(doc) {
var href = doc.location.href;
var q = (/^https?:\/\/(?:www\.)?google\..*\/.*[&?]q=([^&]*)(?:&|$)/(href)||0)[1];
if(!q) return;
var lang = (href.match(/[&?]hl=([^&]*)(?:&|$)/)) ? href.match(/[&?]hl=([^&]*)(?:&|$)/)[1] : 'en';
jQuery.getJSON("http://search.twitter.com/search.json", {
q: q,
lang: lang
},
function(r) {
if (r.results && r.results.length > 0)
{
var doc = CmdUtils.getDocument();
var results = doc.getElementById("res");
var ds = doc.createElement("ol");
results.insertBefore(ds, results.firstChild);
var il,h;
var query = unescape(q).replace(/\+/g, ' ');
h = ds.appendChild(doc.createElement("li"));
h.className = "g";
var h3 = h.appendChild(doc.createElement("h3"));
h3.className = "r";
h3.innerHTML = "<a href='http://search.twitter.com/search?q=" + q + "'>Twitter results for <em>" + query + "</em></a>";
var t = h.appendChild(doc.createElement("table"));
t.className = "ts";
var tb = t.appendChild(doc.createElement("tbody"));
var row = tb.appendChild(doc.createElement("tr"));
row.innerHTML = '<td style="padding-top: 5px; padding-right: 10px; font-size: 78%; line-height: normal; width: 43px; text-align: center;" valign="top"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACsAAAAqCAIAAAClYzUyAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA+NJREFUeNrEWE2IFEcU/qZrup3enp1slnFwHCVZNWBgFYOK5BBvIbcENQSCBC+5evGci5d48JYguQSCkGsOogcPgZyMCCohBCUkMeIqu/6QnZ2dmZ7umled1zW6rtNtb9ea3hSPhu4Z6n313ldfvVelKIrwv45SKgIf+DPEEuE1gRmBqthABATcDHGhjX9CsF8JTDv4aB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FkMf1vCE0kdSZQpFZ6Fd6bdjxvILtXNEBQ0/hVgANbdbXZbeZWHAAAAAElFTkSuQmCC" alt="" height="42" width="43"></td><td style="padding-top: 3px;" valign="top">';
for (var i = 0; i < 5; i++)
{
il = "<div class='s'>" +
"<a href='http://twitter.com/" + r.results[i].from_user + "' class='l'>" +
r.results[i].from_user + "</a>: " +
autolink(r.results[i].text) + ' <span class="f">' + tt(new Date(r.results[i].created_at)) + '</span></div>';
row.innerHTML += il;
}
row.innerHTML += '</td>';
}
});
}
function tt(dt)
{
var nw = new Date(),
df = nw - dt,
dm = Math.floor(df / 60000),
dh = Math.floor(dm / 60),
at = new Date(dt);
if (dm <= 0) {
return "a few seconds ago";
}
if (dm < 60) {
return (dm == 1) ? "1 minute ago": dm + " minutes ago";
}
if (dh <= 1) {
return "about 1 hour ago";
}
if (dh < 24) {
return "about " + dh + " hours ago";
}
if ((nw.getDate() - dt.getDate()) == 1) {
return "yesterday";
}
at.setDate(at.getDate() + 1);
nw.setDate(nw.getDate() + 1);
if ((nw.getDate() - at.getDate()) == 1) {
return "yesterday";
}
var minutes = dt.getMinutes();
if (minutes < 10) minutes = "0" + minutes;
if (dt.getHours() == 0) nw = "12:" + minutes + " AM ";
else if (dt.getHours() < 12) nw = dt.getHours() + ":" + minutes + " AM ";
else nw = (dt.getHours() - 12) + ":" + minutes + " PM ";
return nw + ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"][dt.getMonth()] + " " + dt.getDate();
}
function autolink(s)
{
var hlink = /\s(ht|f)tp:\/\/([^ \,\;\:\!\)\(\"\'\<\>\f\n\r\t\v])+/g;
return (s.replace(hlink,
function($0, $1, $2) {
s = $0.substring(1, $0.length);
// remove trailing dots, if any
while (s.length > 0 && s.charAt(s.length - 1) == '.')
s = s.substring(0, s.length - 1);
// add hlink
return " " + s.link(s);
}
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment