PageSourceSearch

https://sn1ppet.github.io/js/app.js

js sn1ppet.github.io collected 2026-10-03 09:58:04 UTC 2,880 bytes, 106 lines download raw bytes

1$(document).ready(function() {
2
3  // Text selector
4  var selectText = function(element) {
5    if (document.selection) {
6      var range = document.body.createTextRange();
7      range.moveToElementText(element);
8      range.select();
9    }
10    else if (window.getSelection) {
11      var range = document.createRange();
12      range.selectNode(element);
13      window.getSelection().removeAllRanges();
14      window.getSelection().addRange(range);
15    }
16  };
17
18  // Highlight button
19  $('.highlight').each(function() {
20    var $this = $(this);
21
22    $this.wrap('<div class="highlight-wrapper"></div>');
23    $('<button><span class="octicon octicon-clippy"></span</button>').appendTo($this.parent()).on('click', function() {
24      selectText($this[0]);
25    });
26
27  });
28
29  // Typeahead
30  var snippets = new Bloodhound({
31    datumTokenizer: Bloodhound.tokenizers.obj.whitespace('title'),
32    queryTokenizer: Bloodhound.tokenizers.whitespace,
33    prefetch: basePath + '/api/snippets.json'
34  });
35
36  snippets.initialize();
37
38  $('#search')
39    .typeahead({
40      autoselect: true,
41      hint: true,
42      highlight: true,
43      minLength: 1
44    }, {
45      name: 'snippets',
46      displayKey: 'title',
47      source: snippets.ttAdapter(),
48      templates: {
49        empty: [
50          '<div class="empty-message">',
51          'Feel free to <a href="https://github.com/sn1ppet/sn1ppet.github.io/new/master/_posts">submit</a> a snippet.',
52          '</div>'
53        ].join('\n'),
54        suggestion: function(data){
55          return '<p><strong>' + data.title + '</strong> - ' + data.category+ '</p>';
56        }
57      }
58    })
59    .on('typeahead:selected', function(obj, datum, name) {
60      if (typeof datum.url !== 'undefined') {
61        location.href = basePath + datum.url;
62      }
63    }).focus();
64
65  // Global key watcher
66  $(document).keyup(function(e) {
67    if ($('body').hasClass('front')) {
68      return;
69    }
70
71    var keyCode = e.which ? e.which : e.keyCode;
72    var $searchForm = $('#search-form');
73
74    // @todo
75    if ($searchForm.hasClass('show') && keyCode === 27) {
76      $searchForm.removeClass('show');
77    }
78    else if (['INPUT', 'TEXTAREA'].indexOf(e.target.nodeName) === -1) {
79      var character = String.fromCharCode(e.keyCode);
80
81      if (character.match(/[a-z0-9]/i)) {
82        $searchForm.addClass('show').find('.tt-input').val(character).trigger('input').focus();
83      }
84
85      e.preventDefault();
86    }
87  });
88
89  // Ace editor
90  $('.ace-editor').each(function () {
91    var editor = ace.edit(this);
92    editor.getSession().setMode('ace/mode/' + $(this).attr('data-mode'));
93    editor.setTheme('ace/theme/monokai');
94    editor.setOptions({
95      minLines: 3,
96      maxLines: 20,
97      tabSize: 2
98    });
99  });
100  
101  // Select first code snippet automatically.
102  var $codeSnippet = $('.highlight:first');
103  if ($codeSnippet.length) {
104    selectText($codeSnippet[0]);
105  }
106});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.