Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tobynet/586518 to your computer and use it in GitHub Desktop.
Save tobynet/586518 to your computer and use it in GitHub Desktop.
witter search(ja) result on Google right side space
// ==UserScript==
// @name Twitter search(ja) result on Google
// @namespace http://efcl.info/
// @include http://www.google.*/search?*
// ==/UserScript==
//
// original Twitter Search Results on Google for Greasemonkey [ http://userscripts.org/scripts/show/43451 ]
//
// Released under the MIT license
// modified by
// http://twitter.com/cherenkov
// http://twitter.com/toby_net
// see also
// http://help-me-hackers.com/tasks/134
(function(doc){
// *config* display number
var SEARCHINTERVAL = 5;
// 操作メソッド - ナビ子記法
var twit = {
getWord : urlParse,
getJson : getJson,
templete : makeTemplete,
}
// twit.getWord - URLから検索単語を返す
function urlParse(){
var URL = doc.location.href;
var m = URL.match(/[&?]q=([^&]*)(?:&|$)/);
m = m && m[1]; // match word
return m;
}
// twit.getJson - JSON取得後、twit.templeteを呼ぶ
function getJson(word){
if( word ){
GM_xmlhttpRequest({
method:"GET",
url:"http://pcod.no-ip.org/yats/search?query="+word+"&json",
headers:{
"User-Agent":"Mozilla/5.0",
"Accept":"text/json"
},
onload:function(res){
twit.templete(res.responseText , word);
},
onerror:function(res){
console.log(res);
}
});
}
}
// twit.templete - テンプレートに従って挿入
function makeTemplete(jsonObj ,word){
try {
var obj = JSON.parse(jsonObj);
} catch (err) {
console.log(err); // Error parsing JSON.
}
// make templete - 結果が存在するなら
if( obj && obj.length > 0 ){
var results = document.getElementById("res");
var tw_div = document.createElement("div");
tw_div.id = "GM_Twitter_search_on_Google_div"
var ds = document.createElement("div");
ds.id = "GM_Twitter_search_on_Google"
ds.style.fontSize = "80%";
ds.lineHeight = "1.6";
// Search word link
var h3 = document.createElement("h3");
h3.className = "r";
h3.innerHTML = "<a href='http://pcod.no-ip.org/yats/search?query="+ word +"'>Twitter results for <em>"+ htmlescape(decodeURI(word)) +"</em></a>";
tw_div.appendChild(h3);
tw_div.appendChild(ds);
GM_addStyle('#GM_Twitter_search_on_Google { font-size: 90% !important;}');
var rhs = document.getElementById('rhs');
if(rhs) {
rhs.insertBefore(tw_div, rhs.firstChild);
tw_div.style.marginBottom = "30px";
} else {
document.getElementById('center_col').style.minWidth = "0px";
var str = '<div style="display: block; border-left: 1px solid rgb(211, 225, 249); position: absolute; right: 0px; top: 0pt; width: 264px;" id="rhs">';
var df = document.createRange().createContextualFragment(str);
var leftnav = document.getElementById('leftnav');
leftnav.parentNode.insertBefore(df, leftnav);
document.getElementById('rhs').appendChild(tw_div);
}
var tmp = addReslut(obj, 0); // 0番目から
var addButton = document.createElement("button");
addButton.addEventListener("click" , function(){
tmp = addReslut(obj,tmp);
}, false);
addButton.textContent = "追加";
var t = doc.getElementById("GM_Twitter_search_on_Google_div");
t.appendChild(addButton);
}
}
function addReslut(obj ,startNum){
var t = [];
var objLength = obj.length;
var results = document.getElementById("GM_Twitter_search_on_Google");
if(objLength == startNum){
results.innerHTML += "これ以上追加できません";
return null;
}
var to = startNum + SEARCHINTERVAL;
if(objLength < SEARCHINTERVAL){
to = objLength;
}else if(objLength < to){
to = objLength;
}
for(var i=startNum;i<to;i++){
t.push(listTag(obj[i].user, obj[i].content, obj[i].url,obj[i].time));
}
results.innerHTML += t.join("\n");
return to;
}
// テンプレート
function listTag(user, content , url ,time){
var userHome = "http://twitter.com/"+ user;
var cHtml = <>
<div class="s">
<a href={userHome} class="l">{user}</a>:
{content}
<span class="f"><a href ={url} style="color:#999999;">{time}</a></span>
</div>
<hr />
</>;
return cHtml.toString();
}
// HTMLエスケープ
function htmlescape(str){
var tmp = document.createElement("div");
tmp.appendChild(document.createTextNode(str));
return tmp.innerHTML;
};
// 二重表示を防止
if(document.getElementById("GM_Twitter_search_on_Google_div")) return;
/* init */
var searchWord = twit.getWord();
twit.getJson(searchWord);
})(document);
@tobynet
Copy link
Author

tobynet commented Sep 19, 2010

MIT Licenseでいいとのことでライセンスなど追記
refs http://help-me-hackers.com/tasks/134

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment