niku-client/app/static/js/main.js

184 lines
5.4 KiB
JavaScript

$(document).ready(function() {
$('.connection_row').click(function(e) {
if (!getSelection().toString()) {
var id = $(this).data('id');
location.href = '/connection/' + id.toString();
}
});
$('.search_row').click(function(e) {
if (!getSelection().toString()) {
var id = $(this).data('id');
location.href = '/search/' + id.toString() + '/1';
}
});
});
function launch_stream_viewer(stream_data, hex_width) {
$(document).ready(function() {
var viewer = $('<div id="stream_viewer">')
viewer.css("overflow","hidden");
viewer.css("width","75%");
var button = $('<button style="display:block;margin:0 auto" data-action="raw">View as raw</button>').click(function() {
var todo = button.data('action');
var other = todo == 'raw' ? 'hex' : 'raw';
format_stream(todo);
button.data('action', other);
button.text('View as ' + other);
});
$('#stream_viewer_deploy').append(button).append(viewer);
format_stream('hex');
});
function format_stream(format) {
var target = $('#stream_viewer');
var since_last_newline = 0;
target.html('');
for (var i = 0; i < stream_data.length; i++) {
var packet = stream_data[i];
var formatted_packet;
[formatted_packet, since_last_newline] = format_packet(packet['data'], format, since_last_newline, hex_width);
var packet_blurb = $('<span>');
packet_blurb.html(formatted_packet);
packet_blurb.addClass('streamdata')
packet_blurb.addClass(packet['direction'])
packet_blurb.addClass(i.toString())
packet_blurb.prop('title', packet['timediff'].toString());
packet_blurb.css("float","left");
var buttonCopy = $('<button><img width="25px" src="/static/images/copy.png"/></button>').click(function() {
classButtonCopy=$(this).attr('class')
aCBC=classButtonCopy.split(" ");
direction= aCBC[1]
number=parseInt(aCBC[2])
aDirection=document.querySelectorAll("button."+direction)
aCopy=document.querySelectorAll("button.copy")
aNumber= {}
aDirectionMax=parseInt($(aDirection[aDirection.length-1]).attr('class').split(" ")[2])
for (i=0;i<=aDirection.length-1;i++)
aNumber[$(aDirection[i]).attr('class').split(" ")[2]]='1'
for (i=number;i>=0;i--){
if (i in aNumber)
min=i
else
break
}
for (i=number;i<=aDirectionMax;i++){
if (i in aNumber)
max=i
else
break
}
finalValue=""
for (i=min;i<=max;i++)
finalValue+=aCopy[i].value
var clipboardCopy = $('<textarea></textarea>')
target.append(clipboardCopy);
$('textarea').val(finalValue)
$('textarea').select()
document.execCommand('copy');
$('textarea').remove();
labelObject=$(document.querySelector("#labelCopy"+number.toString()))
labelObject.css("visibility","visible")
setTimeout(function(){ labelObject.css("visibility","hidden")},1000);
});
buttonCopy.css("float","left");
[formatted_packetCopy, since_last_newlineCopy] = format_packet(packet['data'], format, since_last_newline, hex_width,1);
buttonCopy.prop("value",formatted_packetCopy.slice(0, -1))
buttonCopy.addClass("copy")
buttonCopy.addClass(packet['direction'])
buttonCopy.addClass(i.toString())
buttonCopy.css("margin-left","15px");
var labelCopy = $('<label>Copied!</label>')
labelCopy.css("float","left");
labelCopy.css("color","green")
labelCopy.css("margin-left","15px");
labelCopy.css("margin-top","7px");
labelCopy.css("visibility","hidden")
$(labelCopy).attr("id","labelCopy"+i.toString())
if ($(packet_blurb).text())
target.append(packet_blurb);
else
i--
if (buttonCopy.prop("value")){
target.append(buttonCopy)
target.append(labelCopy)
}
}
}
}
function translate_single_char(c) {
if (c == 0xa) {
return '&#8629;';
} else if (c == 0x3c) {
return '&lt;';
} else if (c == 0x3e) {
return '&gt;';
} else if (c == 0x26) {
return '&amp;';
} else if (c < 0x20 || c > 0x7e) {
return '&#164;';
} else {
return String.fromCharCode(c);
}
}
function format_packet (input_chars, format, since_last_newline, hex_width, rendering=0) {
out = '';
if (rendering==0){
delimiter=" "
delimiter2=" "
}
else{
delimiter=","
delimiter2=""
}
if (format == 'raw') {
for (var i = 0; i < input_chars.length; i++) {
var c = input_chars[i];
since_last_newline++;
out += translate_single_char(c);
if (c == 0xa || since_last_newline >= 80) {
if (rendering==0)
out += '<br>';
since_last_newline = 0;
}
}
return [out, since_last_newline]
} else if (format == 'hex') {
for (var i = 0; i < input_chars.length; i += 16) {
hexline = '';
textline = '';
for (var j = 0; j < 16 && i + j < input_chars.length; j++) {
var c = input_chars[i+j];
textline += translate_single_char(c);
if (j % 4 == 0) hexline += delimiter2;
hex = c.toString(16);
while (hex.length < hex_width) hex = '0' + hex;
hexline += hex + delimiter;
}
while ((hexline.length < hex_width*16 + 16 + 6) && rendering==0) hexline += ' ';
if (rendering==0)
out += hexline + '| ' + textline + "<br>"
else
out += hexline
}
return [out, 0];
}
};