diff --git a/public/client.js b/public/client.js index 2cda6a53..8941fbe3 100644 --- a/public/client.js +++ b/public/client.js @@ -1,85 +1,114 @@ -$( document ).ready(function() { - let items = []; - let itemsRaw = []; - - $.getJSON('/api/books', function(data) { +// Regular expression to pick up HTML/XML tags, in case of any cross-site scripting attempts. +const tagRegex = /(?:\<\/?.+\>)/g; + +$(document).ready(function () { + let items = []; + let itemsRaw = []; + + $.getJSON("/api/books", function (data) { //let items = []; itemsRaw = data; - $.each(data, function(i, val) { - items.push('
...and '+ (data.length - 15)+' more!
'); + items.push("...and " + (data.length - 15) + " more!
"); } - $(''+data+'
Refresh the page
'); - } + $("#detailComments").html( + '' + data + "
Refresh the page
" + ); + }, }); - }); - - $('#bookDetail').on('click','button.addComment',function() { - let newComment = $('#commentToAdd').val(); + }); + + $("#bookDetail").on("click", "button.addComment", function () { + let newComment = $("#commentToAdd").val(); + newComment = newComment.replace(tagRegex, ""); // Sanitize new comment before adding to the HTML below. $.ajax({ - url: '/api/books/'+this.id, - type: 'post', - dataType: 'json', - data: $('#newCommentForm').serialize(), - success: function(data) { + url: "/api/books/" + this.id, + type: "post", + dataType: "json", + data: $("#newCommentForm").serialize(), + success: function (data) { comments.unshift(newComment); //adds new comment to top of list - $('#detailComments').html(comments.join('')); - } + $("#detailComments").html(comments.join("")); + }, }); }); - - $('#newBook').click(function() { + + $("#newBook").click(function () { $.ajax({ - url: '/api/books', - type: 'post', - dataType: 'json', - data: $('#newBookForm').serialize(), - success: function(data) { + url: "/api/books", + type: "post", + dataType: "json", + data: $("#newBookForm").serialize(), + success: function (data) { //update list - } + }, }); }); - - $('#deleteAllBooks').click(function() { + + $("#deleteAllBooks").click(function () { $.ajax({ - url: '/api/books', - type: 'delete', - dataType: 'json', - data: $('#newBookForm').serialize(), - success: function(data) { + url: "/api/books", + type: "delete", + dataType: "json", + data: $("#newBookForm").serialize(), + success: function (data) { //update list - } + }, }); - }); - -}); \ No newline at end of file + }); +});