From 52dd381304bf920eeced8985e8ab1d4cb1bc40a4 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Fri, 25 Oct 2019 01:49:59 +0200 Subject: Added simple commenting system with Firebase Database integration --- src/static/comments.js | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/static/comments.js (limited to 'src/static/comments.js') diff --git a/src/static/comments.js b/src/static/comments.js new file mode 100644 index 0000000..c8234d2 --- /dev/null +++ b/src/static/comments.js @@ -0,0 +1,63 @@ +var firebaseConfig = { + apiKey: "AIzaSyD3E0XtiUJI4-JIxcIPZziNLGVaTdojz20", + authDomain: "mitja-felicijan-blog.firebaseapp.com", + databaseURL: "https://mitja-felicijan-blog.firebaseio.com", + projectId: "mitja-felicijan-blog", + storageBucket: "mitja-felicijan-blog.appspot.com", + messagingSenderId: "41650892882", + appId: "1:41650892882:web:b308f0a9c47198bdf7ef8b" +}; +firebase.initializeApp(firebaseConfig); + +var database = firebase.database(); +var docPath = 'comments' + window.location.pathname.replace('.html', ''); +var submit = document.querySelector('#submit'); +var comments = document.querySelector('.comments ul'); +var textName = document.querySelector('#name'); +var textComment = document.querySelector('#comment'); +var ref = firebase.database().ref(docPath); + +function encodeHTML(s) { + return s.replace(/&/g, '&').replace(/ +
${encodeHTML(item.name)} - ${encodeHTML(item.published)}
+
${encodeHTML(item.comment)}
+ `; + comments.innerHTML += liItem; + }); + +}, function (errorObject) { + console.log("The read failed: " + errorObject.code); +}); + +submit.addEventListener('click', function (evt) { + if (textName.value && textComment.value) { + submit.disabled = true; + firebase.database().ref(docPath + '/' + Date.now()).set({ + name: textName.value, + comment: textComment.value, + published: new Date().toISOString().slice(0, 16).replace('T', ' '), + }, function (error) { + if (error) { + alert('Data could not be saved.' + error); + } else { + textName.value = ''; + textComment.value = ''; + submit.disabled = false; + } + }); + } +}); -- cgit v1.2.3