Revisions

gist: 154528 Download_button fork
public
Public Clone URL: git://gist.github.com/154528.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
65
66
67
68
69
70
71
72
73
function cleanUpSite(site) {
 
  // Fix up the title by trimming subtitles, trailing splitters
  let title = site.fullTitle.split(
    /[-.:<!>;,\|\/\u00ab\u00b7\u00bb\u2013\u2014\u2022]+( |$)/)[0];
 
  if (!title)
    title = site.uri
 
  // Shorten it if it's still too long
  let maxLen = 45;
  if (title.length > maxLen)
    title = title.slice(0, maxLen) + "\u2026";
 
  site.title = title
 
  return site;
}
 
function find(input, onComplete){
      let awesome = Cc["@mozilla.org/autocomplete/search;1?name=history"].
        getService(Ci.nsIAutoCompleteSearch);
 
 
      awesome.startSearch(input, "", null, ({
        onSearchResult: function(search, result) {
          switch (result.searchResult) {
            case result.RESULT_NOMATCH:
              onComplete([]);
 
            case result.RESULT_SUCCESS_ONGOING:
            case result.RESULT_SUCCESS:
              awesome.stopSearch();
 
              var results = [];
              
              for(let i = 0; i < result.matchCount; i++){
                  results.push(cleanUpSite({
                      fullTitle: result.getCommentAt(i),
                      favicon: result.getImageAt(i),
                      uri: result.getValueAt(i),
                  }));
              }
 
              onComplete(results);
          }
        }
      }));
}
 
var noun_url = {
  label: "url",
  suggest: function(text, html, callback, selectedIndices) {
    find(text, function(results){
      callback([CmdUtils.makeSugg(r.uri, r.title, r.favicon, 0.8) for each (r in results)]);
    });
    return a;
  }
};
 
CmdUtils.CreateCommand({
  names: ["go to"],
  arguments: [{role: 'object', nountype: noun_url, label:"search term"}],
  preview: function preview(pblock, args) {
    pblock.innerHTML = "Opens <b><a href='" + args.object.text + "'>" + args.object.html + "</a></b> on a new tab";
  },
  execute: function execute(args) {
    if(args.object.text){
       Utils.openUrlInBrowser(args.object.text);
    }
  }
});