互联网技术 · 2024年2月14日

Typecho评论显示UserAgent、操作系统和浏览器标识

最近更新友情链接,突然羡慕别人有的浏览器标识,能看到更多的评论者信息,我觉得能够增加访客在页面停留的时间吧,所以决定把自己的typecho也实现这个功能,通过对比添加函数和使用插件两种方法,有了心得。

由于想给博客加一个文章评论显示 UserAgent 功能,在网上搜寻并尝试了 UserAgent 插件,但是并不如意。一是因为他太过臃肿,虽然几乎可以识别市面上所有的 OS 和浏览器,但是我们常用的也就几个而已,大多数都用不到,二是图标太老旧了,而且清晰度很低,在博客上显得突兀且不美观。—-来自左岸博客的引用(传送门)

代码实现评论显示UserAgent

林三通过试用,觉得左岸同学的方法比较适合自己,因为手动添加函数意味着更自由的位置和样式显示。

1、首先找到主题根目录,打开 functions.php 文件,在函数区域(不是最顶端)粘贴下面的代码:

// 获取浏览器信息

function getBrowser($agent)

{

if (preg_match(/MSIEs([^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 Fast Browser;

} 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 Browser;

} else if (preg_match(/QQ/i, $agent, $regs)||preg_match(/QQ Browser/([^s]+)/i, $agent, $regs)) {

$str1 = explode(rowser/, $agent);

$QQ_vern = explode(., $str1[1]);

$outputer = QQ Browser;

} else if (preg_match(/UBrowser/i, $agent, $regs)) {

$str1 = explode(rowser/, $agent);

$UCBrowser_vern = explode(., $str1[1]);

$outputer = UC Browser;

} 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;

}

2、comments.php 中找到合适位置(比如评论作者的后面)添加以下代码:

agent); ?>agent); ?>

然后刷新页面就可以看到UA信息显示出来了。林三这里去掉了图标显示,如果想要图标的,请参考左岸博客的原文来操作,还需要复制相应的css代码到你的样式表中(左岸同学使用的图标在cnd上面,速度还是不错的)。

插件实现评论显示UserAgent

UserAgent是一个较好的评论增强插件,可以分别设置图标、文字或图标+文字形式,来显示评论的UA部分。

1、下载插件:https://github.com/ennnnny/typecho 或者使用网盘下载:传送门,提取码 aqe5

2、然后解压上传UserAgent文件夹至主题插件目录并启用,选择你想要的显示效果保存

3、引用,在你想显示的位置上加上这段代码:agent); ?>

请根据自己的模板来判断是使用$this或$comments(如果不清楚,可以都试下),林三试了下,也是可以显示的。

文章来源:林三在线

OpenMagic API

Need more than content? Move into the product flow.

If you are here for model access, pricing, developer docs, or the future API console, the dedicated product path now lives on api.openmagic.ai.

登录免费注册