博客园 统计某日到现在新增的阅读数 - 简陋
-- 2024-07-15 更新,获取读取量的方法需要调整一下
document.getElementById('stats-total-view-count').childNodes[1].title.replace("总阅读数: ","")
-----2022-05-18 更新一下,不用F12看了,直接页面上显示
window.onload = function(){ console.log("初始化统计 开始日期2022-05-17 09:10:00") var ele = document.getElementById('stats-total-view-count'); if (ele == null) { $(document).find('body').append('<div style="position: fixed; bottom: 5px; width: 130px; height: 60px; right: 10px; border: 2px solid #6FA833; padding: 10px; "></br>统计访问数量不兼容,请使用Chrome浏览器访问</br></br></div>'); return ; } var curentCount = ele.childNodes[1].innerHTML.replace("\n", ""); var now = parseInt(curentCount) - 12541; var currentMs = (new Date() - new Date("2022-05-17 09:10:00")); var mins = (currentMs / 60000); console.log("当前新增阅读数:" + now); var timeTips = ""; if (mins > 59 ) { var hours = Math.floor(mins/60); var min = (mins % 60).toFixed(2); if (hours < 24) { timeTips = "" + hours + " 小时 " + min + " 分钟"; console.log("" + hours + " 小时 " + min + " 分钟"); } else { var days = Math.floor(hours/24); var hoursm = (hours % 24).toFixed(2); timeTips = days + "天" + hoursm + " 小时 " + min + " 分钟"; console.log(days + "天" + hoursm + " 小时 " + min + " 分钟"); } } else { timeTips = "" + (currentMs / 60000).toFixed(2) + " 分钟 "; console.log("" + (currentMs / 60000) + " 分钟 "); } $(document).find('body').append('<div style="position: fixed; bottom: 5px; width: 130px; height: 100px; right: 10px; border: 2px solid #6FA833; padding: 10px; ">' + '</br>距今时间: </br>' + timeTips + '</br></br>' + "当前新增阅读数:" + now + '</br></div>'); }
相关基础知识点:
1. 根据元素id获取元素:document.getElementById('stats-total-view-count')
2. 获取元素的第二个子元素: 元素.childNodes[1]
3. 获取元素内容:.innerHTML
4. 字符串整数转为整数数字:parseInt("123")
5.日期字符串转为Date类型变量: new Date("2022-05-17 09:10:00")
6. Date变量相减得到相差的毫秒数
7.1分钟等于60000毫秒
8. 数字,向下取整数:Math.floor("1.6") 结果为1
9. 数字,保留两位小数: 1.235456.toFixed(2) 结果为1.24,大于5往上进一位
10. / 取整数部分 % 取余数部分
11. 附加元素: 使用append 如: $(document).find('body').append('<div style="position: fixed; bottom: 5px; width: 30px; height: 30px; right: 10px;">Demo</div>');
----2022-05-17
在 管理 -> 页脚HTML代码里复制如下代码,为什么整这个?我感觉每天的阅读量新增有一百了,我实际确认一下,看是不是真的满百了
需要初始年月日时分秒,差不多就行,
需要当前的阅读数
然后等到第二天,在个人博客首页,F12 看一下,就知道有没有破百了
js :
window.onload = function(){ console.log("初始化统计 开始日期2022-05-17 09:10:00") var curentCount = document.getElementById('stats-total-view-count').childNodes[1].innerHTML.replace("\n", ""); var now = parseInt(curentCount) - 12541; var currentMs = (new Date() - new Date("2022-05-17 09:10:00")); var mins = (currentMs / 60000); console.log("当前新增阅读数:" + now); if (mins > 59 ) { var hours = Math.floor(mins/60); var min = mins % 60; console.log("时间 : " + hours + " 小时 " + min + " 分钟"); } else { console.log("时间: " + (currentMs / 60000) + " 分钟 "); } }