Add initial OpenSearch search engine support
[conkeror.git] / modules / webjump.js
blob16128ea2d78632faa3ffae89b5062e9164924868
2 //// web jump stuff
4 var webjumps = {};
5 function add_webjump(key, loc)
7     webjumps[key] = loc;
10 function add_delicious_webjumps (username)
12     add_webjump("delicious", " http://del.icio.us/" + username);
13     add_webjump("adelicious", "javascript:location.href='http://del.icio.us/" + username + "?v=2&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title);");
14     add_webjump("sdelicious", " http://del.icio.us/search/?search=%s");
15     add_webjump("sadelicious", " http://del.icio.us/search/all?search=%s");
18 function webjumps_clear()
20     webjumps = {};
23 // Some built in web jumps
24 function webjumps_add_defaults()
26     add_webjump("conkerorwiki",
27                 "http://conkeror.org/?action=fullsearch&context=60&value=%s&fullsearch=Text");
28     add_webjump("google",     "http://www.google.com/search?q=%s");
29     add_webjump("lucky",      "http://www.google.com/search?q=%s&btnI=I'm Feeling Lucky");
30     add_webjump("maps",       "http://maps.google.com/?q=%s");
31     add_webjump("scholar",    "http://scholar.google.com/scholar?q=%s");
32     add_webjump("clusty",     "http://www.clusty.com/search?query=%s");
33     add_webjump("wikipedia",  "http://en.wikipedia.org/wiki/Special:Search?search=%s");
34     add_webjump("slang",      "http://www.urbandictionary.com/define.php?term=%s");
35     add_webjump("dictionary", "http://dictionary.reference.com/search?q=%s");
36     add_webjump("xulplanet",  "http://www.google.com/custom?q=%s&cof=S%3A"+
37                 "http%3A%2F%2Fwww.xulplanet.com%3BAH%3Aleft%3BLH%3A65%3BLC"+
38                 "%3A4682B4%3BL%3Ahttp%3A%2F%2Fwww.xulplanet.com%2Fimages%2F"+
39                 "xulplanet.png%3BALC%3Ablue%3BLW%3A215%3BAWFID%3A0979f384d5"+
40                 "181409%3B&domains=xulplanet.com&sitesearch=xulplanet.com&sa=Go");
41     add_webjump("image",      "http://images.google.com/images?q=%s");
42     add_webjump("bugzilla",   "https://bugzilla.mozilla.org/show_bug.cgi?id=%s");
43     add_webjump("clhs",       "http://www.xach.com/clhs?q=%s");
44     add_webjump("emacswiki",  "http://www.emacswiki.org/cgi-bin/wiki?search=%s");
45     add_webjump("cliki",      "http://www.cliki.net/admin/search?words=%s");
46     add_webjump("ratpoisonwiki", "http://ratpoison.elektrubadur.se/?search=%s");
47     add_webjump("stumpwmwiki", "http://stumpwm.elektrubadur.se/?search=%s");
48     add_webjump("savannah", "http://savannah.gnu.org/search/?words=%s&type_of_search=soft&Search=Search&exact=1");
49     add_webjump("sourceforge", "http://sourceforge.net/search/?words=%s");
50     add_webjump("freshmeat", "http://freshmeat.net/search/?q=%s");
51     add_webjump("slashdot", "http://slashdot.org/search.pl?query=%s");
52     add_webjump("kuro5hin", "http://www.kuro5hin.org/?op=search&string=%s");
53     add_webjump("sheldonbrown",     "http://www.google.com/search?q=site:sheldonbrown.com %s");
56 function webjump_build_url(template, subs)
58     var b = template.indexOf('%s');
59     var a = b + 2;
60     // Just return the same string if it doesn't contain a %s
61     if (b == -1)
62         return template;
63     else
64         return template.substr(0,b) + encodeURIComponent(subs) + template.substring(a);
67 function get_partial_match(hash, part)
69     var matches = [];
70     for (x in hash) {
71         if (part == x.substr(0, part.length))
72             matches.push(x);
73     }
74     if (matches.length == 1)
75         return matches[0];
76     else
77         return null;
80 function getWebJump(value)
82     try {
83     var start = value.indexOf(' ');
84     var jump;
85     if (start == -1)
86         jump = webjumps[value];
87     else
88         jump = webjumps[value.substr(0,start)];
89     // Try to find a web jump match
90     if (!jump) {
91         var match = get_partial_match(webjumps, value.substr(0,start));
92         if (match)
93             jump = webjumps[match];
94         else
95             return null;
96     }
97     return webjump_build_url(jump, value.substring(start + 1));
98     } catch(e) {/* FIXME: figure out why we need this */ dump_error(e); return null;}
101 function get_url_or_webjump(input)
103     var url = getWebJump(input);
105     if (url) {
106         return url;
107     } else {
108         return input;
109     }
112 webjumps_add_defaults ();