/* global initData, authorizationToken */ // List of HTML entities for escaping. var htmlEscapes = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''', '/': '/' }; // Regex containing the keys listed immediately above. var htmlEscaper = /[&<>"'\/]/g; // Escape a string for HTML interpolation. escape = function(string) { return ('' + string).replace(htmlEscaper, function(match) { return htmlEscapes[match]; }); }; function updatePage(data){ var content = ""; $.each(data.hits.hits, (k,v)=>{ var item = v._source; content += `

${escape(item.created_at)} -- ${escape(item.author)} -- ${escape(item.server)}

${escape(item.content)}

`; }); $("#content").html(content); } $("input").on("keyup",function(e){ const el = $(e.target); const val = el.val(); if( val.length < 3 ){ return; } $.ajax("/search",{ beforeSend: function(request) { request.setRequestHeader("authorizationToken", authorizationToken); }, data: { q:val, } }) .done(function(data) { updatePage(data); }) .fail(function() { alert( "error" ); }); }); updatePage( initData );