summaryrefslogtreecommitdiff
path: root/template/search_files.st
blob: ebe4d39c01da805da05e41409e34f4f456cdc079 (plain)
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
<html>
  <head>
    <title>dump.fm image search</title>
    $head()$
  </head>
  <body>
  
  <input type="text" name="search-query" id="search-query" />
  <div id="message"></div>
  <div id="content"></div>
  
  </body>
  
  <script>
    var term = ""
    var maxImages = 20;
  
    function addScript(term){
      jQuery("#search-script").remove()
      jQuery("head").append("<scr"+"ipt src='/cmd/ghettosearch/"+term+"' id='search-script'></sc"+"ript>")
    }
    
    function setContent(x){
      jQuery("#content").html(x)
    }
    
    function setMessage(x){
      jQuery("#message").html(x)
    }
    
    function searchError(error){
      setContent("")
      setMessage(error)
    }
    
    function searchResult(results){
      var images = []
      var alreadyGot = {}
      if(results === null || results.length == 0) {
        setContent("")
        setMessage("no results found")        
      } else {
      
        for(var r = 0; r<results.length; r++){
          if (images.length == maxImages) break;
          var content = results[r]['content']
          if (content.substring(0,6) == "<safe>") continue; // skip html posts
          var imageUrls = getImagesAsArray(content);
          for (var i=0; i<imageUrls; i++){
            var imageUrl = imageUrls[i];
            if (imageUrl in alreadyGot) continue;
            alreadyGot[imageUrl] = true
            if (imageUrl.indexOf(term) > -1)
              images.push(imageUrl)
          }
        }
        var contentString = ''
        for(var i = 0; i < images.length; i++){
          contentString += '<a href="'+images[i]+'"><img src="'+images[i]+'"></a>'
        }
        setContent(contentString)
      }
    }
    
    function keyHandler(){
      term = jQuery("#search-query").val().trim()
      if (term.length < 3) {
        setMessage("search query too small")
      } else {
        setMessage("searching for '"+term+"'")
        addScript(term)
      }
    }
    
    jQuery("#search-query").keyup(keyHandler)
    
    
  </script>
  
</html>