Created
May 13, 2009 14:13
-
-
Save vjt/111038 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Twitter Search Results on Google | |
// @namespace markcarey | |
// @description Shows results from Twitter on Google search pages | |
// @include http://www.google.*/search?*q=* | |
// @include http://www.google.*/* | |
// @date 2009-05-13 | |
// @version 1.1 | |
// @GM_version 0.8.20080609.0 | |
// ==/UserScript== | |
// CHANGELOG: | |
// | |
// - Made it work with GreaseKit (http://8-p.info/greasekit/) and placed | |
// twitter results alongside google's ones. References included. | |
// | |
// [email protected], 2009-05-13 | |
// | |
// - Added a `count` parameter to choose how many tweets display | |
// - Reworked HTML generation, adding an <ul> and <li>s with "g w0" class, | |
// to show some space between tweets and make them more readable. | |
// | |
// [email protected], 2009-05-16 | |
// | |
// - Added bare (and buggy) search keywords highlighting in twitter results | |
// | |
// [email protected], 2009-06-06 | |
// | |
GM_TUR = { | |
un : "", | |
re : [], | |
lang : "en", | |
count: 20, | |
init : function() | |
{ | |
var href = document.location.href; | |
GM_TUR.un = href.match(/[&?]q=([^&]*)(?:&|$)/)[1]; | |
GM_TUR.lang = (href.match(/[&?]hl=([^&]*)(?:&|$)/)) ? href.match(/[&?]hl=([^&]*)(?:&|$)/)[1] : 'en'; | |
if( GM_TUR.un != "" ) | |
{ | |
// http://mt-hacks.com/20090302-realtime-twitter-search-results-on-google.html#comment-324507 | |
var tag = document.createElement('script'); | |
tag.src = 'http://search.twitter.com/search.json?q='+GM_TUR.un+'&lang='+GM_TUR.lang+'&callback=GM_TUR.handle&rpp='+GM_TUR.count+'&page=1'; | |
document.getElementsByTagName('head').item(0).appendChild(tag); | |
var re = GM_TUR.un.split(/[ \+]/); | |
for (i in re) { | |
GM_TUR.re.push(RegExp(re[i] + "[\\s,\\.;$]", 'ig')); | |
} | |
} | |
}, | |
handle : function(r) | |
{ | |
// Remove "sponsored links" ;) | |
if (ads = document.getElementById('mbEnd')) { | |
ads.setAttribute('style', 'display:none'); | |
} | |
if (tads = document.getElementById('tads')) { | |
tads.setAttribute('style', 'display:none'); | |
} | |
// Proceed. | |
if( r.results && r.results.length > 0 ) | |
{ | |
//var results = document.getElementById("res"); | |
var results = document.getElementById('res'); | |
var ds = document.createElement("ol"); | |
// http://mt-hacks.com/20090302-realtime-twitter-search-results-on-google.html#comment-324511 | |
results.setAttribute("style", "float: left; max-width: 50%") | |
ds.setAttribute("style", "float: left; max-width: 45%") | |
results.parentNode.insertBefore(ds, results.nextSibling); | |
var il, h; | |
var query = unescape(GM_TUR.un).replace(/\+/g, ' '); | |
h = ds.appendChild(document.createElement("li")); | |
h.className = "g"; | |
var h3 = h.appendChild(document.createElement("h3")); | |
h3.className = "r"; | |
h3.innerHTML = "<a href='http://search.twitter.com/search?q="+ GM_TUR.un +"'>Twitter results for <em>"+ query +"</em></a>"; | |
var t = h.appendChild(document.createElement("table")); | |
t.className = "ts"; | |
var tb = t.appendChild(document.createElement("tbody")); | |
var row = tb.appendChild(document.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+DPEEuE1gRmBqthABATcDHGhjX9CsF8JTDv4aBKH3A1BwO6v+/j2MeoWtlfgAF3g/gCLEic243C1eATs7/Q8LUtMWzEaBVj6+7KK0ZxpCbeAdFirX37x8WiACWCgIBVIP9lsBgHc8AuJQX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top"><ul>'; | |
for( var i=0; i < GM_TUR.count; i++ ) | |
{ | |
il = "<li class='g w0'><div class='s'>"+ | |
"<a href='http://twitter.com/"+r.results[i].from_user+"' class='l'>"+ | |
r.results[i].from_user+"</a>: "+ | |
autolink(autobold(r.results[i].text))+' <span class="f">'+GM_TUR.tt(new Date(r.results[i].created_at))+'</span></div>'; | |
row.innerHTML += il; | |
} | |
row.innerHTML += '</li></td>'; | |
} | |
}, | |
tt : function(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(); | |
} | |
}; | |
GM_TUR.init(); | |
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); | |
})); | |
} | |
function autobold(s) | |
{ | |
for (i in GM_TUR.re) { | |
s = s.replace(GM_TUR.re[i], function ($0, $1, $2) { | |
return "<strong>" + $0 + "</strong>" | |
}); | |
} | |
return s; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment