<script> function readyNotif() { // check user credentials let cred = localStorage.getItem('ptc_credentials'); if (cred) { let data = JSON.parse(cred); getNotif(successNotif, 'ptc_notifications', data.username); delete data; } else { document.querySelector('#notif-count').innerHTML = '⚠️'; document.querySelector('.notif-info').innerHTML += '<br><br><span>Please Login First.</span><br><a href="https://storehaccounts.blogspot.com/p/login_20.html" class="button ln">Login</a> <a href="https://storehaccounts.blogspot.com/p/create-account.html" class="button ln">Register</a><br><a href="https://storehaccounts.blogspot.com/p/battle-cats-gamer-community.html" class="button ln">Visit Community</a>'; //getNotif(successFeeds, 'ptc_feeds_notification', ''); } } function onclickOfNotif(username, notif_id) { let data = { [username]: { [notif_id]: { "read": "true" } } } patch('https://jsonbin.org/ptcreborn/ptc_notifications', data, null); let count = document.querySelector('#notif-count').innerText; document.querySelector('#notif-count').innerText = parseInt(count) - 1; document.getElementById(notif_id).style.opacity = '0.7'; document.getElementById(notif_id).querySelector('.unread-notif').remove(); document.getElementById(notif_id).querySelector('a').removeAttribute('onclick'); } function onclickFeed(username, notif_id) { if (username.length > 0) { let data = { [notif_id]: { "views": { [username]: "viewed" } } } patch('https://jsonbin.org/ptcreborn/ptc_feeds_notification', data, null); } let count = document.querySelector('#notif-count').innerText; document.querySelector('#notif-count').innerText = parseInt(count) - 1; } function patch(url, data, callback) { let req = new XMLHttpRequest(); req.onload = () => { if (req.readyState == 4) if (req.status == 200) if (callback != null) callback(req.response); } req.onerror = (err) => { window.alert('Error encountered! ' + err); } req.open('PATCH', url, true); req.setRequestHeader('Content-Type', 'application/json'); req.setRequestHeader('Authorization', 'token 44489997-ed40-4f52-92a9-dfe6fe8ad5db'); req.send(JSON.stringify(data)); } /*async function showNotif(title, comment, thumbnail, url, username, keys) { // create and show the notification const showNotification = () => { // create a new notification const notification = new Notification(title, { body: comment, icon: thumbnail }); // close the notification after 10 seconds setTimeout(() => { notification.close(); }, 10 * 1000); // navigate to a URL when clicked notification.addEventListener('click', () => { onclickOfNotif(username, keys); window.open(url, '_blank'); }); } // show an error message const showError = () => { const error = document.querySelector('.error'); error.style.display = 'block'; error.textContent = 'You blocked the notifications'; } // check notification permission let granted = false; if (Notification.permission === 'granted') { granted = true; } else if (Notification.permission !== 'denied') { let permission = await Notification.requestPermission(); granted = permission === 'granted' ? true : false; } // show notification or error granted ? showNotification() : showError(); };*/ function successFeeds(data, username) { let parsed_data = JSON.parse(data); let keys = Object.keys(parsed_data); let numOfNotif = 0; for (let i = 0; i < keys.length; i++) { let divv = document.createElement('div'); let temp_username = parsed_data[keys[i]].username; let post_url = 'https://storehaccounts.blogspot.com/p/ptc-post-official.html?' + keys[i]; let msg = parsed_data[keys[i]].title; let viewers = JSON.stringify(parsed_data[keys[i]].views); let isViewed = viewers.includes(username); if (username.length == 0) isViewed = false; divv.setAttribute('class', 'notif-block'); divv.id = keys[i]; //showNotif(temp_username, msg, 'https://i.imgur.com/E2TRPZy.jpg', post_url, temp_username, keys[i]); let timeToday = new Date().getTime(); let day_elapsed = parseInt(keys[i]) - timeToday; day_elapsed = day_elapsed / (1000 * 60 * 60 * 24); if (!isViewed) { // means the post is not yet viewed... if (day_elapsed == 2) { divv.innerHTML = "<span class='unread-notif' style='background: blue !important;'>POST YESTERDAY unread</span><span><u>" + temp_username + "</u></span> posted <a onclick='onclickFeed(\"" + username + "\", \"" + keys[i] + "\")' href='" + post_url + "'> " + (msg ? msg : 'uploaded ') + " </a> <small> " + new Date(parseInt(keys[i])) + "</small>"; numOfNotif++; } else if (day_elapsed <= 1) { numOfNotif++; divv.innerHTML = "<span class='unread-notif' style='background: orange !important;'>POST TODAY unread</span><span><u>" + temp_username + "</u></span> posted <a onclick='onclickFeed(\"" + username + "\", \"" + keys[i] + "\")' href='" + post_url + "'> " + (msg ? msg : 'uploaded ') + " </a> <small> " + new Date(parseInt(keys[i])) + "</small>"; } else { numOfNotif++; divv.innerHTML = "<span class='unread-notif' style='background: orange !important;'>POST DAYS AGO.. unread</span><span><u>" + temp_username + "</u></span> posted <a onclick='onclickFeed(\"" + username + "\", \"" + keys[i] + "\")' href='" + post_url + "'> " + (msg ? msg : 'uploaded ') + " </a> <small> " + new Date(parseInt(keys[i])) + "</small>"; } } else { // the post is already viewed divv.style.opacity = '0.7'; if (day_elapsed == 2) { divv.innerHTML = "<span class='unread-notif' style='background: orange !important;'>POST </span><span><u>" + temp_username + "</u></span> posted <a onclick='onclickFeed(\"" + username + "\", \"" + keys[i] + "\")' href='" + post_url + "'> " + (msg ? msg : 'uploaded ') + " </a> <small> " + new Date(parseInt(keys[i])) + "</small>"; } else if (day_elapsed <= 1) { divv.innerHTML = "<span class='unread-notif' style='background: orange !important;'>POST TODAY</span><span><u>" + temp_username + "</u></span> posted <a onclick='onclickFeed(\"" + username + "\", \"" + keys[i] + "\")' href='" + post_url + "'> " + (msg ? msg : 'uploaded ') + " </a> <small> " + new Date(parseInt(keys[i])) + "</small>"; } else { divv.innerHTML = "<span class='unread-notif' style='background: orange !important;'>POST DAYS AGO..</span><span><u>" + temp_username + "</u></span> posted <a onclick='onclickFeed(\"" + username + "\", \"" + keys[i] + "\")' href='" + post_url + "'> " + (msg ? msg : 'uploaded ') + " </a> <small> " + new Date(parseInt(keys[i])) + "</small>"; } } document.querySelector('.white_content').insertBefore(divv, document.querySelector('.notif-block')); } let prevCount = parseInt(document.querySelector('#notif-count').innerText); if(prevCount !== prevCount) prevCount = 0; numOfNotif += prevCount; document.querySelector('#notif-count').innerText = prevCount + numOfNotif; if (numOfNotif == 0) document.querySelector('#notif-count').style.display = 'none'; else document.querySelector('#notif-count').style.display = 'block'; } function successNotif(data, username) { let notif = JSON.parse(data); if (notif[username]) { let keys = Object.keys(notif[username]); let numOfNotif = 0; for (let i = keys.length - 1; i > -1; i--) { let divv = document.createElement('div'); let temp_username = notif[username][keys[i]].username; let post_url = 'https://storehaccounts.blogspot.com/p/ptc-post-official.html?' + notif[username][keys[i]].post_url; let msg = notif[username][keys[i]].action; divv.setAttribute('class', 'notif-block'); divv.id = keys[i]; if (notif[username][keys[i]].read) { // this means the notification is already read divv.style.opacity = '0.7'; divv.innerHTML = "<span class='unread-notif'>COMMENT</span><span><u>" + temp_username + "</u></span> <a href='" + post_url + "'> " + (msg ? msg : 'comment on your post.') + " </a> <small> " + new Date(parseInt(keys[i])) + "</small>"; } else { //showNotif(temp_username, msg, 'https://i.imgur.com/E2TRPZy.jpg', post_url, username, keys[i]); divv.innerHTML = "<span class='unread-notif'>COMMENT unread</span><span><u>" + temp_username + "</u></span> <a onclick='onclickOfNotif(\"" + username + "\", \"" + keys[i] + "\")' href='" + post_url + "'> " + (msg ? msg : 'comment on your post.') + " </a> <small> " + new Date(parseInt(keys[i])) + "</small>"; numOfNotif++; } document.querySelector('.white_content').appendChild(divv); } if (document.querySelector('#notif-count')) document.querySelector('#notif-count').innerText = parseInt(document.querySelector('#notif-count')) + numOfNotif; document.querySelector('#notif-count').innerText = numOfNotif; if (numOfNotif == 0) document.querySelector('#notif-count').style.display = 'none'; } else { document.querySelector('.notif-info').innerHTML += '<br><span style="opacity: 0.7;">Nothing for now.</span><br><br><a href="https://storehaccounts.blogspot.com/p/battle-cats-gamer-community.html" class="button ln">Visit Community</a>'; document.querySelector('#notif-count').style.display = 'none'; } //getNotif(successFeeds, 'ptc_feeds_notification', username); } function getNotif(callback, record, username) { let req = new XMLHttpRequest(); req.onload = () => { if (req.readyState == 4) if (req.status == 200) if (callback) callback(req.response, username); } req.onerror = (err) => { window.alert('Error encountered! ' + err); } req.open('GET', 'https://jsonbin.org/ptcreborn/' + record, true); req.setRequestHeader('Content-Type', 'application/json'); req.setRequestHeader('Authorization', 'token 44489997-ed40-4f52-92a9-dfe6fe8ad5db'); req.send(); } </script>