前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Typecho 博客文章评论添加显示 UserAgent(UA)的功能

Typecho 博客文章评论添加显示 UserAgent(UA)的功能

作者头像
季春二九
发布2023-03-22 14:13:54
1.1K0
发布2023-03-22 14:13:54
举报
文章被收录于专栏:季春二九季春二九
  • 本篇文章实现了为 Typecho 博客文章评论添加显示 UserAgent(UA)的功能
  • 本功能可替代 UserAgent 插件,更美观、简洁且好看

效果显示

  • 大概就是这样了,实际效果请看我的评论!
TIM截图20200226131837.png
TIM截图20200226131837.png
  • 目前可以识别的操作系统以及浏览器
3404854218.png
3404854218.png

食用方法

  • 这里以 Mirages为例,其他主题操作方法类似(VOID配置看评论)。

首先 将下面这段 css 全部加入到 Mirages/css/7.10.0/Mirages.min.css 末尾。

代码语言:javascript
复制
.ua-icon{display:inline-block;width:1pc;height:1pc;background-size:cover;background-repeat:no-repeat;vertical-align:text-top}.icon-360{background-image:url(https://img.jichun29.cn/img/20200226125429.png)}.icon-android{background-image:url(https://img.jichun29.cn/img/20200226125423.png);height:19px}.icon-apple{background-image:url(https://img.jichun29.cn/img/20200226125422.png)}.icon-baidu{background-image:url(https://img.jichun29.cn/img/20200226125424.png)}.icon-chrome{background-image:url(https://img.jichun29.cn/img/20200226125427.png)}.icon-edge{background-image:url(https://img.jichun29.cn/img/20200226125425.png)}.icon-firefox{background-image:url(https://img.jichun29.cn/img/20200226125426.png)}.icon-google{background-image:url(https://img.jichun29.cn/img/20200226125428.png)}.icon-ie{background-image:url(https://img.jichun29.cn/img/20200226125431.png)}.icon-liebao{background-image:url(https://img.jichun29.cn/img/20200226125430.png)}.icon-linux{background-image:url(https://img.jichun29.cn/img/20200226125433.png)}.icon-mac{background-image:url(https://img.jichun29.cn/img/20200226125432.png)}.icon-opera{background-image:url(https://img.jichun29.cn/img/20200226125434.png)}.icon-qq{background-image:url(https://img.jichun29.cn/img/20200226125435.png)}.icon-quark{background-image:url(https://img.jichun29.cn/img/20200226125437.png)}.icon-safari{background-image:url(https://img.jichun29.cn/img/20200226125438.png)}.icon-ubuntu{background-image:url(https://img.jichun29.cn/img/20200226125436.png)}.icon-uc{background-image:url(https://img.jichun29.cn/img/20200226125439.png)}.icon-win1{background-image:url(https://img.jichun29.cn/img/20200226125440.png)}.icon-win2{background-image:url(https://img.jichun29.cn/img/20200226125421.png)}
  • 也可后台加入自定义 css 或是直接在 header.php 中引入

然后 找到 Mirages/functions.php,将下面代码完整复制,加到 functions.php 文件的最末尾

代码语言:javascript
复制
    // 获取浏览器信息
    function getBrowser($agent)
    {
        if (preg_match('/MSIE\s([^\s|;]+)/i', $agent, $regs)) {
            $outputer = '  Internet Explore';
        } else if (preg_match('/FireFox\/([^\s]+)/i', $agent, $regs)) {
          $str1 = explode('Firefox/', $regs[0]);
    $FireFox_vern = explode('.', $str1[1]);
            $outputer = '  FireFox';
        } else if (preg_match('/Maxthon([\d]*)\/([^\s]+)/i', $agent, $regs)) {
          $str1 = explode('Maxthon/', $agent);
    $Maxthon_vern = explode('.', $str1[1]);
            $outputer = '  MicroSoft Edge';
        } else if (preg_match('#360([a-zA-Z0-9.]+)#i', $agent, $regs)) {
    $outputer = '  360极速浏览器';
        } else if (preg_match('/Edge([\d]*)\/([^\s]+)/i', $agent, $regs)) {
            $str1 = explode('Edge/', $regs[0]);
    $Edge_vern = explode('.', $str1[1]);
            $outputer = '  MicroSoft Edge';
        } else if (preg_match('/UC/i', $agent)) {
                  $str1 = explode('rowser/',  $agent);
    $UCBrowser_vern = explode('.', $str1[1]);
            $outputer = '  UC浏览器';
        }  else if (preg_match('/QQ/i', $agent, $regs)||preg_match('/QQBrowser\/([^\s]+)/i', $agent, $regs)) {
                      $str1 = explode('rowser/',  $agent);
    $QQ_vern = explode('.', $str1[1]);
            $outputer = '  QQ浏览器';
        } else if (preg_match('/UBrowser/i', $agent, $regs)) {
                  $str1 = explode('rowser/',  $agent);
    $UCBrowser_vern = explode('.', $str1[1]);
            $outputer = '  UC浏览器';
        }  else if (preg_match('/Opera[\s|\/]([^\s]+)/i', $agent, $regs)) {
            $outputer = '  Opera';
        } else if (preg_match('/Chrome([\d]*)\/([^\s]+)/i', $agent, $regs)) {
    $str1 = explode('Chrome/', $agent);
    $chrome_vern = explode('.', $str1[1]);
            $outputer = '  Google Chrome';
        } else if (preg_match('/safari\/([^\s]+)/i', $agent, $regs)) {
             $str1 = explode('Version/',  $agent);
    $safari_vern = explode('.', $str1[1]);
            $outputer = '  Safari';
        } else{
            $outputer = '  Google Chrome';
        }
        echo $outputer;
    }
    // 获取操作系统信息
    function getOs($agent)
    {
        $os = false;
     
        if (preg_match('/win/i', $agent)) {
            if (preg_match('/nt 6.0/i', $agent)) {
                $os = '    Windows Vista / ';
            } else if (preg_match('/nt 6.1/i', $agent)) {
                $os = '    Windows 7 / ';
            } else if (preg_match('/nt 6.2/i', $agent)) {
                $os = '    Windows 8 / ';
            } else if(preg_match('/nt 6.3/i', $agent)) {
                $os = '    Windows 8.1 / ';
            } else if(preg_match('/nt 5.1/i', $agent)) {
                $os = '    Windows XP / ';
            } else if (preg_match('/nt 10.0/i', $agent)) {
                $os = '    Windows 10 / ';
            } else{
                $os = '    Windows X64 / ';
            }
        } else if (preg_match('/android/i', $agent)) {
        if (preg_match('/android 9/i', $agent)) {
                $os = '    Android Pie / ';
            }
        else if (preg_match('/android 8/i', $agent)) {
                $os = '    Android Oreo / ';
            }
        else{
                $os = '    Android / ';
        }
        }
        else if (preg_match('/ubuntu/i', $agent)) {
            $os = '    Ubuntu / ';
        } else if (preg_match('/linux/i', $agent)) {
            $os = '    Linux / ';
        } else if (preg_match('/iPhone/i', $agent)) {
            $os = '    iPhone / ';
        } else if (preg_match('/mac/i', $agent)) {
            $os = '    MacOS / ';
        }else if (preg_match('/fusion/i', $agent)) {
            $os = '    Android / ';
        } else {
            $os = '    Linux / ';
        }
        echo $os;
    }

最后

在 Mirages/lib/comments.php 中找到合适位置添加以下代码:

代码语言:javascript
复制
    agent); ?>
    agent); ?>
TIM截图20200226131539.png
TIM截图20200226131539.png

如果修改完都显示 Linux 的话,需要将上面的 comments 替换成 this 即可,注意代码缩进修改完后刷新浏览器缓存,现在你的评论 UA 已经变得很漂亮啦!

本文转自左岸大佬,做了一些修改。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 效果显示
  • 食用方法
  • 最后
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档