-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathastro.lookup.js
More file actions
61 lines (49 loc) · 1.67 KB
/
Copy pathastro.lookup.js
File metadata and controls
61 lines (49 loc) · 1.67 KB
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
/*
* Name resolver based on the LookUP webservice
* Written by Stuart Lowe
*
*/
(function () {
function init(astrojs) {
this.fns = {};
this.results = function(d){
if(typeof d=="undefined" || (d.type && d.type=="error")) return { message: "There was a problem getting the search results. Sorry about that." };
if(d.target && d.target.suggestion) return { message: 'Not found. Did you mean '+d.target.suggestion+'?', suggestion: d.target.suggestion }
if(d.ra) return d;
else{
if(d.message) return d;
else return { message: "Not found. Sorry." };
}
}
this.find = function(objectname,eventData,callback){
if(typeof callback!=="function" && typeof eventData==="function"){
callback = eventData;
eventData = {};
}
if(typeof callback!=="function") return -1;
var str = objectname;
str = encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28'). replace(/\)/g, '%29').replace(/\*/g, '%2A');
str = str.replace(/%0A/g, '\n');
var fname = 'astrojslookup'+Date.now();
var _obj = this;
var _arg = eventData;
var _call = callback;
this.fns[fname] = function(d){
d = _obj.results(d);
_call.call(astrojs,{result:d,data:_arg});
}
var js = document.createElement('script');
js.setAttribute('type', 'text/javascript');
js.setAttribute('src', 'http://www.strudel.org.uk/lookUP/json/?name='+str+'&callback=astrojs.lookup.fns.'+fname);
js.onerror = js.onreadystatechange = this.fns[fname];
document.getElementsByTagName('head')[0].appendChild(js);
return 0;
}
return this;
}
astrojs.registerPackage({
init: init,
name: 'lookup',
version: '0.1'
});
})(astrojs);