MENU

添加站点生存时间的简单方法

2020 年 01 月 12 日 • 阅读: 8067 • 博客

2020 以水文开篇,为任意站点添加生存时间,效果见博客页脚,大佬自行跳过。

引入 JS

将以下代码加入到 <head> 标签中。对于本主题,依次进入 控制台 - 外观 - 设置外观 - 主题自定义扩展,加入到 自定义 HTML 元素拓展 - 标签: head 头部 (meta 元素后),也可直接加入到主题对应的 header.php 中的 </head> 标签前。开始时间和中文提示可自定义。

<script>
    document.addEventListener('DOMContentLoaded', initLiveDay);
    function initLiveDay() {
        const birthTime = '2015/2/21 10:23:12';
        const template = (A, B, C, D) => `本站已勉强生存 ${A}天 ${B}时 ${C}分 ${D}秒.`;

        /* 锚点开始 */
        const container = footer.querySelector('.container');
        const p = document.createElement('p');
        container.insertBefore(p, container.firstElementChild);
        /* 锚点结束*/

        const msoad = 24 * 60 * 60 * 1000;
        const warp = n => n > 9 ? n : '0' + n;
        const toInt = n => warp(Math.floor(n));
        setInterval(() => {
            const lived = new Date() - new Date(birthTime);
            const days = lived / msoad;
            const intDays = toInt(days);
            const hours = (days - intDays) * 24;
            const intHours = toInt(hours);
            const minutes = (hours - intHours) * 60;
            const intMinutes = toInt(minutes);
            const seconds = (minutes - intMinutes) * 60;
            const intSeconds = toInt(seconds);
            p.innerHTML = template(intDays, intHours, intMinutes, intSeconds);
        }, 1000);
    }
</script>

其他主题

其他主题需要通过 F12 找到对应锚点,替换代码中那三行,不知道怎么找的,留下 博客链接要加入的位置,我看到后会将锚点代码更新到此。

演示地址

const footer = document.body.querySelector('.site-footer');
footer.insertBefore(document.createRange().createContextualFragment(`
<div class="container d-flex justify-content-sm-between justify-content-center text-center">
    <div class="copyright"><p style="font-family: Consolas"></p></div>
</div>`.trim()).firstElementChild, footer.firstElementChild);
const p = footer.firstElementChild.querySelector('p');

TG 大佬群 QQ 大佬群

最后编辑于: 2020 年 01 月 18 日
返回文章列表 文章二维码
本页链接的二维码
打赏二维码
添加新评论

Loading captcha...

已有 8 条评论
  1. lol lol   Windows 10 x64 Edition  Google Chrome 79.0.3945.117


    function show_runtime(){window.setTimeout("show_runtime()",1000);x=new Date("2019/05/20 00:00:00");y=new Date();z=y.getTime()-x.getTime();mspd=24*60*60*1000;_d=z/mspd;d=Math.floor(_d);_h=(_d-d)*24;h=Math.floor(_h);_m=(_h-h)*60;m=Math.floor(_m);_s=(_m-m)*60;s=Math.floor(_s);website_runtime.innerHTML="站点已安全运行:"+d+"天"+h+"小时"+m+"分"+s+"秒";}show_runtime();

  2. imwen imwen   Android 9  WebView 4.0

    内容不错,就优化太差了,@(你懂的),互链吗

    1. LOGI LOGI   Windows 10 x64 Edition  Google Chrome 79.0.3945.117

      @imwen嗯嗯,第一没钱,第二不想备案@(小乖)

    2. LOGI LOGI   Windows 10 x64 Edition  Google Chrome 79.0.3945.117

      @imwen加上喽

    3. imwen imwen   Android 9  WebView 4.0

      @LOGI已加,我也去弄个同主题配置,真香啊@(捂嘴笑)

  3. Toki Toki   Mac OS X 10.14  Firefox 73.0

    大佬,首页英文语句用的啥 API 可以分享下吗? ::quyin:maimeng::

    1. LOGI LOGI   Windows 10 x64 Edition  Google Chrome 79.0.3945.117

      @Toki看我最新文章呀@(你懂的)