tt25 (owner)

Revisions

  • 08d661 tt25 Sat Mar 07 06:52:17 -0800 2009
  • 651b80 tt25 Sat Mar 07 05:50:58 -0800 2009
  • 4aaa05 tt25 Sat Mar 07 05:17:50 -0800 2009
  • 07ba3a tt25 Sat Mar 07 05:11:48 -0800 2009
  • 2c9ea1 tt25 Sat Mar 07 04:58:05 -0800 2009
  • c09a87 tt25 Sat Mar 07 04:41:35 -0800 2009
  • ac42a4 tt25 Sat Mar 07 04:35:59 -0800 2009
  • 7c8aa6 tt25 Sat Mar 07 04:34:37 -0800 2009
gist: 75318 Download_button fork
public
Public Clone URL: git://gist.github.com/75318.git
Embed All Files: show embed
api.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var API={
country2people: function(c){
// c = "日本"
var p=c+"人";
return API.ja2en(p).next(function(p){
return p.replace(/\s*people\s*/g,"");
});
}
 
,en2ja: function(word,pair){
return API.trans(word,"en|ja");
}
 
,ja2en: function(word,pair){
return API.trans(word,"ja|en");
}
 
,trans: function(word,pair){
var url="http://www.google.com/uds/Gtranslate?q="+encodeURIComponent(word)+"&langpair="+encodeURIComponent(pair)+"&v=1.0&callback=%cb";
return JSON.req(url,function(json){
return json.responseData.translatedText;
});
}
 
,suggest: function(q){
var url="http://suggestqueries.google.com/complete/search?hl=en&jsonp=%cb&qu="+encodeURIComponent(q);
return JSON.req(url,function(json){
var items=json.pop();
var ret=[];
for(var i=0,len=items.length; i<len; i++){
var results=items[i][1].match(/^[0-9,]+/)[0].replace(/[^0-9]/g,"");
ret.push({
quote: items[i][0],
results: results
});
}
return ret;
});
}
}
 
index.html #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja">
  <head>
    <title>Google's eye: What is your country thought?</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" href="style.css" type="text/css" charset="utf-8" />
    
<link rel="alternate" type="application/atom+xml" title="Atom 1.0" href="/blog/rss" />
  </head>
  <body>
<h1>Google's eye</h1>
<form action="#">
<p id="sample">
country:<input type="text" name="q" id="q" value="japan" />
<input type="submit" value="view" />
ex: <a href="#japan">japan</a>, <a href="#british">british</a>, <a href="#india">india</a>, <a href="#america">america</a> <span style="display:none" id="country-ja">,<a href="#スイス">スイス</a> ,<a href="#トルコ">トルコ</a></span> ...
</p>
<p id="lang" style="display:none;">
<label>
<input type="checkbox" name="ja" id="trans" />
日本語でおk
</label>
<a href="#" id="permalink"></a>
</p>
</form>
<ol id="history">
</ol>
 
<div id="results">
<strong id="message"></strong>
<ul id="items"></ul>
</div>
<script type="text/javascript" src="jsdeferred.mini.js"></script>
<script type="text/javascript" src="json.js"></script>
<script type="text/javascript" src="api.js"></script>
<script type="text/javascript" src="main.js"></script>
<script type="text/javascript">
var lang=(navigator.browserLanguage || navigator.language || navigator.userLanguage)
if(lang.indexOf("ja") > -1){
document.getElementById("lang").style.display="block";
document.getElementById("country-ja").style.display="inline";
}
var sample=document.getElementById("sample").getElementsByTagName("a");
for(var i=0,len=sample.length; i<len; i++){
sample[i].addEventListener("click",function(){
restore_search(this.innerHTML);
},false);
}
Deferred.define();
var text=document.getElementById("q");
var form=text.parentNode.parentNode;
var permalink=document.getElementById("permalink");
form.addEventListener("submit",function(e){
e.preventDefault();
search();
},false);
document.getElementById("trans").addEventListener("change",function(){
search();
},false);
if(location.hash){
restore_search(location.hash.replace(/^#/,""));
}
</script>
 
<div id="footer">
<p>
Inspired by <a href="http://raurublock.tumblr.com/post/84056744/id6nhh-jpg">this reblog</a>.
Using Google suggest &amp; translate API.
Source code is on <a href="http://gist.github.com/75318">github.com</a>
</p>
</div>
</body>
</html>
 
jsdeferred.mini.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// JSDeferred 0.2.2 (c) Copyright (c) 2007 cho45 ( www.lowreal.net )
// See http://coderepos.org/share/wiki/JSDeferred
function Deferred(){return(this instanceof Deferred)? this.init():new Deferred()}
Deferred.ok=function(x){return x};Deferred.ng=function(x){throw x};Deferred.prototype={
init:function(){
this._next=null;this.callback={
ok:Deferred.ok,
ng:Deferred.ng
};return this;},
next:function(fun){return this._post("ok",fun)},
error:function(fun){return this._post("ng",fun)},
call:function(val){return this._fire("ok",val)},
fail:function(err){return this._fire("ng",err)},
cancel:function(){
(this.canceller || function(){})();return this.init();},
_post:function(okng,fun){
this._next=new Deferred();this._next.callback[okng]=fun;return this._next;},
_fire:function(okng,value){
var next="ok";try{
value=this.callback[okng].call(this,value);}catch(e){
next="ng";value=e;}
if(value instanceof Deferred){
value._next=this._next;}else{
if(this._next)this._next._fire(next,value);}
return this;}
};Deferred.parallel=function(dl){
var ret=new Deferred(),values={},num=0;for(var i in dl)if(dl.hasOwnProperty(i))(function(d,i){
d.next(function(v){
values[i]=v;if(--num<=0){
if(dl instanceof Array){
values.length=dl.length;values=Array.prototype.slice.call(values,0);}
ret.call(values);}
}).error(function(e){
ret.fail(e);});num++;})(dl[i],i);if(!num)Deferred.next(function(){ret.call()});ret.canceller=function(){
for(var i in dl)if(dl.hasOwnProperty(i)){
dl[i].cancel();}
};return ret;};Deferred.wait=function(n){
var d=new Deferred(),t=new Date();var id=setTimeout(function(){
clearTimeout(id);d.call((new Date).getTime()-t.getTime());},n*1000);d.canceller=function(){try{clearTimeout(id)}catch(e){}};return d;};Deferred.next_default=function(fun){
var d=new Deferred();var id=setTimeout(function(){clearTimeout(id);d.call()},0);d.canceller=function(){try{clearTimeout(id)}catch(e){}};if(fun)d.callback.ok=fun;return d;};Deferred.next_faster_way_Image=((typeof(Image)!="undefined")&& document.addEventListener)&& function(fun){
var d=new Deferred();var img=new Image();var handler=function(){
d.canceller();d.call();};img.addEventListener("load",handler,false);img.addEventListener("error",handler,false);d.canceller=function(){
img.removeEventListener("load",handler,false);img.removeEventListener("error",handler,false);};img.src="data:,/_/X";if(fun)d.callback.ok=fun;return d;};Deferred.next_faster_way_readystatechange=(!window.opera &&/\bMSIE\b/.test(navigator.userAgent))&& function(fun){
var d=new Deferred();var t=new Date().getTime();if(t-arguments.callee._prev_timeout_called<150){
var cancel=false;var script=document.createElement("script");script.type="text/javascript";script.src="javascript:";script.onreadystatechange=function(){
if(!cancel){
d.canceller();d.call();}
};d.canceller=function(){
if(!cancel){
cancel=true;script.onreadystatechange=null;document.body.removeChild(script);}
};document.body.appendChild(script);}else{
arguments.callee._prev_timeout_called=t;var id=setTimeout(function(){clearTimeout(id);d.call()},0);d.canceller=function(){try{clearTimeout(id)}catch(e){}};}
if(fun)d.callback.ok=fun;return d;};Deferred.next=Deferred.next_faster_way_Image ||
Deferred.next_faster_way_readystatechange ||
Deferred.next_default;Deferred.call=function(f,args){
args=Array.prototype.slice.call(arguments);f=args.shift();return Deferred.next(function(){
return f.apply(this,args);});};Deferred.loop=function(n,fun){
var o={
begin:n.begin || 0,
end:(typeof n.end=="number")? n.end:n-1,
step:n.step || 1,
last:false,
prev:null
};var ret,step=o.step;return Deferred.next(function(){
function _loop(i){
if(i<=o.end){
if((i+step)>o.end){
o.last=true;o.step=o.end-i+1;}
o.prev=ret;ret=fun.call(this,i,o);if(ret instanceof Deferred){
return ret.next(function(r){
ret=r;return Deferred.call(_loop,i+step);});}else{
return Deferred.call(_loop,i+step);}
}else{
return ret;}
}
return(o.begin<=o.end)? Deferred.call(_loop,o.begin):null;});};Deferred.register=function(name,fun){
this.prototype[name]=function(){
return this.next(Deferred.wrap(fun).apply(null,arguments));};};Deferred.wrap=function(dfun){
return function(){
var a=arguments;return function(){
return dfun.apply(null,a);};};};Deferred.register("loop",Deferred.loop);Deferred.register("wait",Deferred.wait);Deferred.define=function(obj,list){
if(!list)list=["parallel","wait","next","call","loop"];if(!obj)obj=(function getGlobal(){return this})();for(var i=0;i<list.length;i++){
var n=list[i];obj[n]=Deferred[n];}
return Deferred;};
json.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
var JSON={
fn: {} // callbacks
,req: function(url,callback){
var cb=JSON.temp_fn(callback);
url=url.replace(/%cb/g,cb);
var sc=document.createElement("script");
sc.src=url;
sc.addEventListener("load",function(e){
document.body.removeChild(this);
window[cb]=null;
},false);
document.body.appendChild(sc);
var ret=next(function(){
if(!!window[cb]) return next(arguments.callee);
return ;
}).next(function(){
var ret=JSON.fn[cb].arg;
JSON.fn[cb]=null;
delete JSON.fn[cb];
return ret;
});
return ret;
}
 
,temp_fn: function(fn){
do {
var name="jsonp_"+(""+Math.random()).replace(/[^0-9]/g,"");
} while(typeof window[name] != "undefined");
var args=arguments;
JSON.fn[name]={fn: fn};
window[name]=function(json){
JSON.fn[name]["arg"]=JSON.fn[name].fn(json);
}
return name;
}
}
 
main.js #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
var search_history={},cache_list={};
function search(){
var country=text.value;
// var country="日本";
var token=country;
var elm_msg=document.getElementById("message");
function message(msg){
elm_msg.innerHTML=msg;
}
function check_change_word(){
//if(token != text.value) throw token;
}
document.getElementById("items").innerHTML="";
message("loading "+country);
next(function(){
// 語句を日本語へ変換
check_change_word();
if(country.match(/[^\x00-\x7f]/)){
return country; // たぶん日本語
}else{
return API.en2ja(country);
}
}).next(function(jcountry){
var cache_key=(document.getElementById("trans").checked) ? "1":"0";
cache_key+=jcountry;
if(cache_list[cache_key]){
return cache_list[cache_key];
}
return next(function(){ // if no cache start
// 語句の準備
check_change_word();
return parallel({
country: API.ja2en(jcountry).next(function(c){
return c;
}),
people: API.country2people(jcountry).next(function(p){
return p;
})
});
}).next(function(res){
// 得られた語句からフレーズ生成
check_change_word();
message("fetching country images..");
var queries=[];
var stack=function(q){
queries.push(API.suggest(q));
}
stack(res.people+" should");
stack(res.people+" become");
stack(res.people+" people are");
stack("why are "+res.people);
stack("what "+res.people);
stack(res.country+" should");
stack(res.country+" become");
stack(res.country+" have");
stack(res.country+" is ");
stack("why are "+res.country);
return parallel(queries);
}).next(function(items){
// 日本語訳するならここで。
check_change_word();
if(!document.getElementById("trans").checked) return items;
message("日本語翻訳中...");
var q=[];
function stack(item){
q.push(
next(function(){
return API.en2ja(item.quote);
}).next(function(w){
item.quote=w;
return item;
})
);
}
for(var i=0,len=items.length; i<len; i++){
if(!items[i]){ continue; }
for(var j=0,len2=items[i].length; j<len2; j++){
stack(items[i][j]);
}
}
return parallel(q);
}).next(function(res){
// サジェスト結果を表示
message("displaying results...");
var ret=[];
for(var i=0,len=res.length; i<len; i++){
if(!res[i]){ continue; }
ret=ret.concat(res[i]);
}
ret.sort(function(a,b){
return b.results - a.results;
});
 
var newul=document.getElementById("items").cloneNode(false);
var exists={};
for(var i=0,len=ret.length; i<len; i++){
var quote=ret[i].quote;
if(exists[quote]) continue;
exists[quote]=true;
var cnt=ret[i].results,formatted=[];
var rank=(cnt > Math.pow(10,7)) ? 1
: (cnt > Math.pow(10,4))? 2
: 3
;
while(cnt.length > 3){
cnt=cnt.replace(/[0-9]{3}$/,function(m){
formatted.push(m);
return "";
});
}
formatted.push(cnt);
cnt=formatted.reverse().join(",");
 
var li=document.createElement("li");
li.className="rank"+rank;
li.innerHTML=[
'<a href="http://google.com/search?q=',
encodeURIComponent(ret[i].quote),
'">',ret[i].quote,'</a>',
'<em>',cnt,'</em>'
].join("");
newul.appendChild(li);
}
cache_list[cache_key]=newul;
return newul;
}); // if no cache end
}).next(function(newul){
message("");
var ul=document.getElementById("items");
ul.parentNode.replaceChild(newul.cloneNode(true),ul);
}).next(function(){
// history
var w=text.value;
if(search_history[w]) return;
search_history[w]=true;
link=permalink.cloneNode(false);
link.href="#"+w;
link.innerHTML=w;
link.addEventListener("click",function(){
restore_search(w);
},false);
var his=document.getElementById("history");
var li=document.createElement("li");
li.appendChild(link);
his.appendChild(li);
}).error(function(e){
//console.log(e);
message("");
});
}// def search end
 
function restore_search(w){
text.value=w;
search();
}
 
 
style.css #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
ul#items {
list-style:none;
margin:0;
padding:0;
}
ul#items li {
list-style:none;
margin:0;
padding-bottom:0.4ex;
padding-top:0.4ex;
}
ul#items li em {
font-size:small;
padding-left:1em;
}
 
ul#items li.rank1{
background:#ff9;
}
ul#items li.rank2{
background:#ffc;
}
ul#items li.rank3{
}
 
div#footer {
margin-top:1em;
padding-top:1em;
border-top:1px solid #333;
}
div#footer p{
margin:0;
}