/
mograblog-counter.js
110 lines (103 loc) · 3.41 KB
/
mograblog-counter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
$(function () {
function showCount (count) {
var $pageviews = $('.pageviews')
$pageviews.attr('title', count + ' people saw this post')
$pageviews.find('.pageviews-content').text(count)
if (count > 20) {
$pageviews.addClass('initialized')
}
}
if (window && window.location && window.location.hostname === 'localhost') {
showCount(200);
return // don't trigger counter on localhost
}
$.getScript('https://www.gstatic.com/firebasejs/3.2.1/firebase.js', function () {
/**
* initialize
* @type {{apiKey: string, authDomain: string, databaseURL: string, storageBucket: string}}
*/
// Initialize Firebase
var config = {
apiKey: 'AIzaSyBPkaNJa7n7yb670VB07t-6SO5nGSJ_3R0',
authDomain: 'mograblog.firebaseapp.com',
databaseURL: 'https://mograblog.firebaseio.com',
storageBucket: ''
}
firebase.initializeApp(config)
var rootRef = firebase.database().ref()
var pageViewsRef = rootRef.child('pageviews')
if (window && window.location && window.location.pathname) {
/****
*
* update page views
*
****/
var pathkey = slug(window.location.pathname)
var pageRef = pageViewsRef.child(pathkey)
var counted = false
pageRef.on('value', function (pageviews) {
var pageviewsCount = pageviews.val()
console.log('this is value', pageviewsCount)
showCount(pageviewsCount);
if (!counted) {
counted = true
pageRef.transaction(function (views) {
return views + 1
})
}
})
}
/******
*
* update currently reading
*
*******/
var currentReadingRef = rootRef.child('currentlyReading')
var currentlyReading = false
currentReadingRef.on('value', function (currentlyReadingValue) {
/*****
*
* construct the twitter-feed like display
*
*****/
var $currentlyReading = $('.currently-reading')
$currentlyReading.empty()
currentlyReadingValue.val().forEach(function (value) {
$currentlyReading.append(
$('<a>', {text: value.title, href: value.url, 'class': 'currently-reading-link mograblog-link'})
)
var agoText = 'earlier..'
if (value.timestamp) {
var hoursAgo = Math.floor((Date.now() - value.timestamp) / (1000 * 60 * 60))
agoText = hoursAgo < 2 ? 'right now..' : hoursAgo + ' hours ago'
}
$currentlyReading.append(
$('<div>', {text: agoText, 'class': 'currently-reading-ago'})
)
})
$('.currently-reading-wrapper').addClass('initialized')
/**
*
* update the value
*
*/
if (!currentlyReading) { // modify only once
currentlyReading = true
currentReadingRef.transaction(function (currentlyReadingArray) {
var postTitle = $('.post-title').text()
try {
if (currentlyReadingArray && currentlyReadingArray.length > 0 && currentlyReadingArray[0].title === postTitle) { // don't modify if already registered this post
return
}
} catch (e) {
}
return [{ // add this post and append with all the rest
title: postTitle,
url: document.location.pathname,
timestamp: Date.now()
}].concat(currentlyReadingArray.slice(0, currentlyReadingArray.length - 1))
})
}
})
})
})