Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
// Register a service worker, this one located in serviceworker.js
// A service worker is a piece of code the browser runs behind the scenes.
if ('serviceWorker' in navigator) {
console.log('CLIENT: service worker registration in progress.');
navigator.serviceWorker.register('sw.js').then(function() {
console.log('CLIENT: service worker registration complete.');
}, function() {
console.log('CLIENT: service worker registration failure.');
});
} else {
console.log('CLIENT: service workers are not supported.');
}
let usernameInput = document.querySelector("#usernameInput");
let statusInput = document.querySelector("#statusInput");
let profilePicInput = document.querySelector("#profilePicInput");
let profileSaveInput = document.querySelector("#profileSaveInput");
let profilePicDisplayElement = document.querySelector("#profilePicDisplay")
let profilePic = localStorage.getItem("")
var sampleMessage = {
sentBy: "joel",
dateStamp: "02/20/2020 at 2:49",
text: "Hello, this is a new message."
}
let allMessages = [];
let chatBox = document.querySelector("#chatBox");
allMessages.push(sampleMessage);
console.log(allMessages);
function makeAndDisplayMessage(message) {
let curr_date = new Date(message.dateStamp);
let month = curr_date.getMonth() + 1;
let date_str = month.toString() + "/" + curr_date.getDate().toString() + "/" + curr_date.getFullYear().toString();
let curr_time = new Date(message.dateStamp);
let time_str = curr_time.getHours().toString() + ":" + curr_time.getMinutes().toString();
// console.log(message);
var messageParagraph = document.createElement("p");
var messageBelow = document.createElement("h4");
// var messageUsername = document.createElement("h3");
messageParagraph.innerHTML = message.sentBy + " " + " - " + " " + time_str + " " + date_str;
messageBelow.innerHTML = message.message;
// chatBox.appendChild();
chatBox.appendChild(messageParagraph);
chatBox.appendChild(messageBelow);
messageParagraph.scrollIntoView();
// chatBox.scrollTop = chatBox.scrollHeight;
}
watchFirebaseForChanges(
function(msg){
makeAndDisplayMessage(msg.data())
}
);
function showTheMessage(){
document.querySelector("#send-button").addEventListener('click', function (){
let curr_date = new Date();
let month = curr_date.getMonth() + 1;
let date_str = month.toString() + "/" + curr_date.getDate().toString() + "/" + curr_date.getFullYear().toString();
let src = "sound/tone.mp3"
let audio = new Audio(src);
audio.play();
window.navigator.vibrate(200);
let newMessage = {
sentBy: localStorage.getItem("username"),
dateStamp: Date.now(),
message: document.querySelector("#chat-input").value
}
saveMessageToFirebase(newMessage);
// makeAndDisplayMessage(newMessage);
document.querySelector("#chat-input").value = "";
document.querySelector("#chat-input").focus();
});
}
document.getElementById("usernameInput").innerHTML = localStorage.getItem("username");
document.getElementById("statusInput").innerHTML = localStorage.getItem("userStatus");
document.getElementById("profilePicInput").src = localStorage.getItem("userProfilePic") || "img/profile.jpeg";
// i tried to make the enter button send message but it didnt work :(
document.querySelector("chat-input").addEventListener('keyup', function(e){
if (event.keyCode === 13) {
showTheMessage();
}
})