0021.Hexo Fluid添加文章时效性提示
新建scripts/injector.js
1 2
| hexo.extend.injector.register('body_end', `<script src="/js/outdate.js"></script>`,'post')
|
新建source/js/outdate.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
| window.onload = function() { let warningDay = 200; let errorDay = 400; let times = document.getElementsByTagName('time'); if (times.length === 0) { return; } let posts = document.getElementsByClassName('post-content'); if (posts.length === 0) { return; }
let pubTime = new Date(times[0].dateTime); let now = Date.now() let interval = parseInt(now - pubTime) let days = parseInt(interval / 86400000) if (interval > warningDay*3600*24*1000 && interval < errorDay*3600*24*1000){ posts[0].innerHTML = '<div class="note note-warning">' + '<h5>文章时效性提示</h5><p>这是一篇发布于 ' + days + ' 天前的文章,部分信息可能已发生改变,请注意甄别。</p>' + '</div>' + posts[0].innerHTML; }else if(interval >= errorDay*3600*24*1000){ posts[0].innerHTML = '<div class="note note-danger">' + '<h5>文章时效性提示</h5><p>这是一篇发布于 ' + days + ' 天前的文章,部分信息可能已发生改变,请注意甄别。</p>' + '</div>' + posts[0].innerHTML; } };
|