73 lines
1.6 KiB
JavaScript
73 lines
1.6 KiB
JavaScript
/* 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];
|
|
});
|
|
};
|
|
var urlRegex = /(https?://[^\s]+)/g;
|
|
url = function(string){
|
|
return ''+string.replace(urlRegex, '<a target="_blank" href="$1">$1</a>')
|
|
}
|
|
var titleRegex = /^(.*\n)/;
|
|
title = function(string){
|
|
var str=''+string.replace(titleRegex, '<b>$1</b>');
|
|
return ''+string.replace(titleRegex, '<b>$1</b>');
|
|
}
|
|
|
|
function updatePage(data){
|
|
|
|
var content = "";
|
|
$.each(data.hits.hits, (k,v)=>{
|
|
var item = v._source;
|
|
|
|
content += `
|
|
|
|
<div class="log row-fluid">
|
|
<div class="span9">
|
|
<p>${escape(item.created_at)} -- ${escape(item.author)} -- ${escape(item.server)}
|
|
<pre> ${title(url(escape(item.content)))}</pre>
|
|
</span>
|
|
</div>
|
|
`;
|
|
});
|
|
$("#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 );
|