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

2024-03-23 12:52

本文主要是介绍Typecho 博客文章评论添加显示 UserAgent(UA)的功能,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  • 本篇文章实现了为 Typecho 博客文章评论添加显示 UserAgent(UA)的功能
  • 本功能可替代 UserAgent 插件,更美观、简洁且好看

效果显示

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

食用方法

  • 这里以 Mirages主题为例,其他主题操作方法类似。

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

.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 文件的最末尾

    // 获取浏览器信息function getBrowser($agent){if (preg_match('/MSIE\s([^\s|;]+)/i', $agent, $regs)) {$outputer = '<i class="ua-icon icon-ie"></i>&nbsp;&nbsp;Internet Explore';} else if (preg_match('/FireFox\/([^\s]+)/i', $agent, $regs)) {$str1 = explode('Firefox/', $regs[0]);$FireFox_vern = explode('.', $str1[1]);$outputer = '<i class="ua-icon icon-firefox"></i>&nbsp;&nbsp;FireFox';} else if (preg_match('/Maxthon([\d]*)\/([^\s]+)/i', $agent, $regs)) {$str1 = explode('Maxthon/', $agent);$Maxthon_vern = explode('.', $str1[1]);$outputer = '<i class="ua-icon icon-edge"></i>&nbsp;&nbsp;MicroSoft Edge';} else if (preg_match('#360([a-zA-Z0-9.]+)#i', $agent, $regs)) {$outputer = '<i class="ua-icon icon-360"></i>&nbsp;&nbsp;360极速浏览器';} else if (preg_match('/Edge([\d]*)\/([^\s]+)/i', $agent, $regs)) {$str1 = explode('Edge/', $regs[0]);$Edge_vern = explode('.', $str1[1]);$outputer = '<i class="ua-icon icon-edge"></i>&nbsp;&nbsp;MicroSoft Edge';} else if (preg_match('/UC/i', $agent)) {$str1 = explode('rowser/',  $agent);$UCBrowser_vern = explode('.', $str1[1]);$outputer = '<i class="ua-icon icon-uc"></i>&nbsp;&nbsp;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 = '<i class= "ua-icon icon-qq"></i>&nbsp;&nbsp;QQ浏览器';} else if (preg_match('/UBrowser/i', $agent, $regs)) {$str1 = explode('rowser/',  $agent);$UCBrowser_vern = explode('.', $str1[1]);$outputer = '<i class="ua-icon icon-uc"></i>&nbsp;&nbsp;UC浏览器';}  else if (preg_match('/Opera[\s|\/]([^\s]+)/i', $agent, $regs)) {$outputer = '<i class= "ua-icon icon-opera"></i>&nbsp;&nbsp;Opera';} else if (preg_match('/Chrome([\d]*)\/([^\s]+)/i', $agent, $regs)) {$str1 = explode('Chrome/', $agent);$chrome_vern = explode('.', $str1[1]);$outputer = '<i class="ua-icon icon-chrome""></i>&nbsp;&nbsp;Google Chrome';} else if (preg_match('/safari\/([^\s]+)/i', $agent, $regs)) {$str1 = explode('Version/',  $agent);$safari_vern = explode('.', $str1[1]);$outputer = '<i class="ua-icon icon-safari"></i>&nbsp;&nbsp;Safari';} else{$outputer = '<i class="ua-icon icon-chrome"></i>&nbsp;&nbsp;Google Chrome';}echo $outputer;}// 获取操作系统信息function getOs($agent){$os = false;if (preg_match('/win/i', $agent)) {if (preg_match('/nt 6.0/i', $agent)) {$os = '&nbsp;&nbsp;<i class= "ua-icon icon-win1"></i>&nbsp;&nbsp;Windows Vista&nbsp;/&nbsp;';} else if (preg_match('/nt 6.1/i', $agent)) {$os = '&nbsp;&nbsp;<i class= "ua-icon icon-win1"></i>&nbsp;&nbsp;Windows 7&nbsp;/&nbsp;';} else if (preg_match('/nt 6.2/i', $agent)) {$os = '&nbsp;&nbsp;<i class="ua-icon icon-win2"></i>&nbsp;&nbsp;Windows 8&nbsp;/&nbsp;';} else if(preg_match('/nt 6.3/i', $agent)) {$os = '&nbsp;&nbsp;<i class= "ua-icon icon-win2"></i>&nbsp;&nbsp;Windows 8.1&nbsp;/&nbsp;';} else if(preg_match('/nt 5.1/i', $agent)) {$os = '&nbsp;&nbsp;<i class="ua-icon icon-win1"></i>&nbsp;&nbsp;Windows XP&nbsp;/&nbsp;';} else if (preg_match('/nt 10.0/i', $agent)) {$os = '&nbsp;&nbsp;<i class="ua-icon icon-win2"></i>&nbsp;&nbsp;Windows 10&nbsp;/&nbsp;';} else{$os = '&nbsp;&nbsp;<i class="ua-icon icon-win2"></i>&nbsp;&nbsp;Windows X64&nbsp;/&nbsp;';}} else if (preg_match('/android/i', $agent)) {if (preg_match('/android 9/i', $agent)) {$os = '&nbsp;&nbsp;<i class="ua-icon icon-android"></i>&nbsp;&nbsp;Android Pie&nbsp;/&nbsp;';}else if (preg_match('/android 8/i', $agent)) {$os = '&nbsp;&nbsp;<i class="ua-icon icon-android"></i>&nbsp;&nbsp;Android Oreo&nbsp;/&nbsp;';}else{$os = '&nbsp;&nbsp;<i class="ua-icon icon-android"></i>&nbsp;&nbsp;Android&nbsp;/&nbsp;';}}else if (preg_match('/ubuntu/i', $agent)) {$os = '&nbsp;&nbsp;<i class="ua-icon icon-ubuntu"></i>&nbsp;&nbsp;Ubuntu&nbsp;/&nbsp;';} else if (preg_match('/linux/i', $agent)) {$os = '&nbsp;&nbsp;<i class= "ua-icon icon-linux"></i>&nbsp;&nbsp;Linux&nbsp;/&nbsp;';} else if (preg_match('/iPhone/i', $agent)) {$os = '&nbsp;&nbsp;<i class="ua-icon icon-apple"></i>&nbsp;&nbsp;iPhone&nbsp;/&nbsp;';} else if (preg_match('/mac/i', $agent)) {$os = '&nbsp;&nbsp;<i class="ua-icon icon-mac"></i>&nbsp;&nbsp;MacOS&nbsp;/&nbsp;';}else if (preg_match('/fusion/i', $agent)) {$os = '&nbsp;&nbsp;<i class="ua-icon icon-android"></i>&nbsp;&nbsp;Android&nbsp;/&nbsp;';} else {$os = '&nbsp;&nbsp;<i class="ua-icon icon-linux"></i>&nbsp;&nbsp;Linux&nbsp;/&nbsp;';}echo $os;}

最后

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

<span class="comment-ua"><?php getOs($comments->agent); ?><?php getBrowser($comments->agent); ?></span>

TIM截图20200226131539.png

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

这篇关于Typecho 博客文章评论添加显示 UserAgent(UA)的功能的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/838371

相关文章

苹果macOS 26 Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色

《苹果macOS26Tahoe主题功能大升级:可定制图标/高亮文本/文件夹颜色》在整体系统设计方面,macOS26采用了全新的玻璃质感视觉风格,应用于Dock栏、应用图标以及桌面小部件等多个界面... 科技媒体 MACRumors 昨日(6 月 13 日)发布博文,报道称在 macOS 26 Tahoe 中

Java使用HttpClient实现图片下载与本地保存功能

《Java使用HttpClient实现图片下载与本地保存功能》在当今数字化时代,网络资源的获取与处理已成为软件开发中的常见需求,其中,图片作为网络上最常见的资源之一,其下载与保存功能在许多应用场景中都... 目录引言一、Apache HttpClient简介二、技术栈与环境准备三、实现图片下载与保存功能1.

MybatisPlus service接口功能介绍

《MybatisPlusservice接口功能介绍》:本文主要介绍MybatisPlusservice接口功能介绍,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友... 目录Service接口基本用法进阶用法总结:Lambda方法Service接口基本用法MyBATisP

Java反射实现多属性去重与分组功能

《Java反射实现多属性去重与分组功能》在Java开发中,​​List是一种非常常用的数据结构,通常我们会遇到这样的问题:如何处理​​List​​​中的相同字段?无论是去重还是分组,合理的操作可以提高... 目录一、开发环境与基础组件准备1.环境配置:2. 代码结构说明:二、基础反射工具:BeanUtils

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

Druid连接池实现自定义数据库密码加解密功能

《Druid连接池实现自定义数据库密码加解密功能》在现代应用开发中,数据安全是至关重要的,本文将介绍如何在​​Druid​​连接池中实现自定义的数据库密码加解密功能,有需要的小伙伴可以参考一下... 目录1. 环境准备2. 密码加密算法的选择3. 自定义 ​​DruidDataSource​​ 的密码解密3

SpringCloud使用Nacos 配置中心实现配置自动刷新功能使用

《SpringCloud使用Nacos配置中心实现配置自动刷新功能使用》SpringCloud项目中使用Nacos作为配置中心可以方便开发及运维人员随时查看配置信息,及配置共享,并且Nacos支持配... 目录前言一、Nacos中集中配置方式?二、使用步骤1.使用$Value 注解2.使用@Configur

idea中project的显示问题及解决

《idea中project的显示问题及解决》:本文主要介绍idea中project的显示问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录idea中project的显示问题清除配置重China编程新生成配置总结idea中project的显示问题新建空的pr

SpringBoot后端实现小程序微信登录功能实现

《SpringBoot后端实现小程序微信登录功能实现》微信小程序登录是开发者通过微信提供的身份验证机制,获取用户唯一标识(openid)和会话密钥(session_key)的过程,这篇文章给大家介绍S... 目录SpringBoot实现微信小程序登录简介SpringBoot后端实现微信登录SpringBoo

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue