blaenk (owner)

Revisions

gist: 130853 Download_button fork
public
Public Clone URL: git://gist.github.com/130853.git
Embed All Files: show embed
JavaScript #
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
// YOU CAN DO ANYTHING YOU WANT WITH THIS SOURCE AS LONG AS:
// - YOU GIVE ME CREDIT SOMEWHERE IN YOUR SOURCE
// THANKS
 
// simply tell the user that if no argument is passed,
// they will be taken to the site. if an argument is passed,
// then the site will be searched with their query
function thePreview(pblock, query, siteName) {
   if (!query.text || query.text == "go")
      pblock.innerHTML = "Go to " + siteName;
   else
      pblock.innerHTML = "Search " + siteName + " for " + query.text;
}
 
// this actually determines the action and then invokes it
function SearchOrGoTo(queryUrl, query) {
   var search_string = encodeURIComponent(query.text);
   
   if (!search_string || search_string == "go")
      Utils.openUrlInBrowser(queryUrl.substring(0, queryUrl.indexOf('/', 8) + 1));
   else
      Utils.openUrlInBrowser(queryUrl.replace("{QUERY}", search_string));
}
 
// this is a function which automates the creation of the commands,
// to avoid duplicate code :D
function generateSearchCommand(siteName, cmdName, queryUrl) {
   CmdUtils.CreateCommand({
      name: cmdName,
      takes: {query: noun_arb_text},
      description: "Searches or goes to " + siteName,
      author: {name: "Blaenk", homepage: "http://www.blaenkdenum.com"},
      icon: queryUrl.substring(0, queryUrl.indexOf('/', 8) + 1) + "/favicon.ico",
 
      preview: function(pblock, query) {
         thePreview(pblock, query, siteName);
      },
      
      execute: function(query) {
         SearchOrGoTo(queryUrl, query);
      }
   });
}
 
var sites = [
   {
      name : "Google",
      command : "g",
      queryUrl : "http://www.google.com/search?q={QUERY}"
   },
   {
      name : "Wikipedia",
      command : "w",
      queryUrl : "http://en.wikipedia.org/wiki/Special:Search?search={QUERY}"
   },
   {
      name : "IMDB",
      command : "i",
      queryUrl : "http://www.imdb.com/find?q={QUERY}"
   }];
 
for (i = 0; i < sites.length; i++) {
   generateSearchCommand(sites[i].name, sites[i].command, sites[i].queryUrl);
}