2022-09-12 06:49:52 -05:00
|
|
|
if (typeof auth !== 'undefined') {
|
|
|
|
var viewsCollection = db.collection('views');
|
2022-09-28 17:43:50 -05:00
|
|
|
var likesCollection = db.collection('likes');
|
2022-09-12 06:49:52 -05:00
|
|
|
|
|
|
|
function numberWithCommas(x) {
|
|
|
|
//return x.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ",");
|
|
|
|
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
var update_views = function (oid, id) {
|
|
|
|
viewsCollection.doc(id).onSnapshot(doc => {
|
|
|
|
var data = doc.data();
|
|
|
|
if (data) {
|
|
|
|
var label = document.querySelectorAll("span[id='" + oid + "']")[0].innerText.split(' ')[1]
|
2022-10-17 04:44:20 -05:00
|
|
|
document.querySelectorAll("span[id='" + oid + "']")[0].innerText = numberWithCommas(data.views)
|
2022-09-12 06:49:52 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-28 17:43:50 -05:00
|
|
|
var update_likes = function (oid, id) {
|
|
|
|
likesCollection.doc(id).onSnapshot(doc => {
|
|
|
|
var data = doc.data();
|
|
|
|
if (data) {
|
|
|
|
var label = document.querySelectorAll("span[id='" + oid + "']")[0].innerText.split(' ')[1]
|
2022-10-17 04:44:20 -05:00
|
|
|
document.querySelectorAll("span[id='" + oid + "']")[0].innerText = numberWithCommas(data.likes)
|
2022-09-28 17:43:50 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-09-12 06:49:52 -05:00
|
|
|
|
|
|
|
auth.signInAnonymously()
|
|
|
|
.then(() => {
|
|
|
|
var views_nodes = document.querySelectorAll("span[id^='views_']")
|
|
|
|
|
|
|
|
for (var i in views_nodes) {
|
|
|
|
var oid = views_nodes[i].id
|
|
|
|
var id = oid ? oid.replaceAll("/", "-") : oid
|
|
|
|
if (id) {
|
|
|
|
update_views(oid, id)
|
|
|
|
}
|
|
|
|
}
|
2022-09-28 17:43:50 -05:00
|
|
|
|
|
|
|
var likes_nodes = document.querySelectorAll("span[id^='likes_']")
|
|
|
|
|
|
|
|
for (var i in likes_nodes) {
|
|
|
|
var oid = likes_nodes[i].id
|
|
|
|
var id = oid ? oid.replaceAll("/", "-") : oid
|
|
|
|
if (id) {
|
|
|
|
update_likes(oid, id)
|
|
|
|
}
|
|
|
|
}
|
2022-09-12 06:49:52 -05:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
var errorCode = error.code;
|
|
|
|
var errorMessage = error.message;
|
|
|
|
console.error(errorCode, errorMessage)
|
|
|
|
});
|
|
|
|
}
|