漫谈 Windows 11 的 UserAgent

最近一直在折腾跟 wp 相关的内容,主要是把拉跨速度的插件以及代码全部优化了一遍。作为一个强迫症已经基本治愈的患者,看到一些错误还是不能忍受。尤其是开启查询插件之后再控制台报错的那些内容,本着除恶务尽的作风,继续对报错的插件进行了修复。

将弃用的函数全部进行了替换,之所以要做这个是原有的归档插件频繁报错,后来自己尝试写了一个页面,但是查询效率并不高。没有达到自己的要求,所以就对原插件进行了修复。这几天修复的插件有两个:Clean Archives  以及Simple microblogging 【增强版】。至此所有控制台的错误就都处理掉了。

另外一个就是 WP-UserAgent  无法识别 windows11 的问题,这个其实搜索一下,网上也没有什么解决方案,甚至没看到神马系统的文章。之所以识别不出来是因为 win11 之后 ua 不会更新 os 版本的信息了:

User-Agent字符串不会更新为区分Windows 11和Windows 10,也不会区分 CPU 体系结构。 建议不要使用User-Agent字符串来检索用户代理数据。 不支持User-Agent客户端提示的浏览器将无法区分Windows 11和Windows 10,也无法区分 CPU 体系结构。

上面是微软的文档:https://learn.microsoft.com/zh-cn/microsoft-edge/web-platform/how-to-detect-win11

在文档里也给出了如何判断操作系统,不过是 js 代码:

navigator.userAgentData.getHighEntropyValues(["platformVersion"])
 .then(ua => {
   if (navigator.userAgentData.platform === "Windows") {
     const majorPlatformVersion = parseInt(ua.platformVersion.split('.')[0]);
     if (majorPlatformVersion >= 13) {
       console.log("Windows 11 or later");
      }
      else if (majorPlatformVersion > 0) {
        console.log("Windows 10");
      }
      else {
        console.log("Before Windows 10");
      }
   }
   else {
     console.log("Not running on Windows");
   }
 });

不过有了这个代码,要做系统识别貌似也就有了办法了:

1.给评论增加一个 隐藏的input,用于上传是否是 windows11

// 在comment_form_logged_in钩子中添加自定义字段
add_action('comment_form_logged_in', 'add_custom_field_to_comment_form');
function add_custom_field_to_comment_form() {
    echo '<p><input type="text" name="comment_windows_version" id="comment_windows_version" /></p>';
}
 
// 在comment_form_before_fields钩子中添加自定义字段
add_action('comment_form_before_fields', 'add_custom_field_to_comment_form');

2.加载 js 代码自动填充这个字段的 value

try{navigator.userAgentData.getHighEntropyValues(["platformVersion"])
.then(ua => {
  if (navigator.userAgentData.platform === "Windows") {
    const majorPlatformVersion = parseInt(ua.platformVersion.split('.')[0]);
    if (majorPlatformVersion >= 13) {
     document.getElementById("comment_windows_version").value = "win11";

      console.log("Windows 11 or later");
     }
     else if (majorPlatformVersion > 0) {
       console.log("Windows 10");
     }
     else {
       console.log("Before Windows 10");
     }
  }
  else {
    console.log("Not running on Windows");
    document.getElementById("comment_windows_version").value = "Not Windows";

  }
});
}catch (e) {
  console.log("Windows veresion fix:Not Supported:");
}

将 js 代码添加到 wp 中,自己调整下面代码的 js 地址:

function add_win_fix_js_js_to_head() {
    echo "<script src=\"//h4ck.org.cn/win11.js\" type=\"module\"></script>";
}
add_action( 'wp_footer', 'add_win_fix_js_js_to_head' );

3.提交评论时进行预处理,如果是 windows11 将 ua 中的 os 进行替换

// 钩子函数,在评论提交前调用
add_filter('pre_comment_user_agent', 'fix_win11_user_agent');
 
function fix_win11_user_agent($user_agent) {
    if (isset($_POST['comment_windows_version'])) {
        $win_version = $_POST['comment_windows_version'];
        // $user_agent ='test';
        if ($user_agent && $win_version=='win11'){
            $user_agent = str_replace('Windows 10','Windows 11', $user_agent);
        }
    }

    // 否则,返回原始的用户代理字符串
    return $user_agent;
}

4.修改插件,兼容 windows 11显示,编辑wp-useragent-detect-os.php文件,在495 行之后 添加:

if (preg_match('/Windows NT 11.0/i', $useragent)
            || preg_match('/Windows NT 11/i', $useragent))
        {
            $version = '11';
            $code = 'win-6';
        }
        elseif (preg_match('/Windows NT 10.0/i', $useragent)
            || preg_match('/Windows NT 6.4/i', $useragent))
        {
            $version = '10';
            $code = 'win-6';
        }

实际效果:

已知问题,直接从管理后台发布的评论无法修正,不过嘛,现在看来也基本够用了。

至于夜间模式,又去掉了,自己写代码写不好,插件兼容性有问题,暂时就酱紫吧。

修复插件下载:

温馨提示: 此处隐藏内容需要发表评论,并且审核通过后才能查看。
(发表评论请勾选 在此浏览器中保存我的显示名称、邮箱地址和网站地址,以便下次评论时使用。
(请仔细检查自己的昵称和评论内容,以免被识别为垃圾评论而导致无法正常审核。)

☆版权☆

* 网站名称:obaby@mars
* 网址:https://h4ck.org.cn/
* 个性:https://oba.by/
* 本文标题: 《漫谈 Windows 11 的 UserAgent》
* 本文链接:https://image.h4ck.org.cn/2024/03/16044
* 短链接:https://oba.by/?p=16044
* 转载文章请标明文章来源,原文标题以及原文链接。请遵从 《署名-非商业性使用-相同方式共享 2.5 中国大陆 (CC BY-NC-SA 2.5 CN) 》许可协议。


You may also like

49 comments

  1. Level 3
    Firefox 123 Firefox 123 Windows 10 Windows 10 cn上海市闵行区 电信

    方法都差不多,我是用那段js判断是否是win11,然后放入cookie。通过cookie判断是否是11,然后将ua里面的Windows 10替换为Windows 11,在插件里面新增一个对11的判断。

  2.  Level 5
    Google Chrome 122 Google Chrome 122 Windows 10 Windows 10 cn北京市 电信

    优秀如你,这个功能我还没有加上去 可以请教一下了 哈哈
    另外 评论用户名、邮箱和网站信息 每次都要重新填写

  3.  Level 4
    Firefox 123 Firefox 123 Windows 10 Windows 10 cn云南省德宏州 移动

    有技术就是好呀,这地方我也发现了但是不改了哈直接给隐藏了

  4. Level 4
    Google Chrome 122 Google Chrome 122 Mac OS X 10.15 Mac OS X 10.15 cn广东省清远市 电信

    ok就很棒,细节做得太到位了,如果是我的话,应该会选择直接无视,哈哈哈

  5. Level 4
    Microsoft Edge 110 Microsoft Edge 110 Windows 10 Windows 10 us美国加利福利亚州洛杉矶

    专门mark下,看看我的ua

    1. Level 4
      Internet Explorer 6 Internet Explorer 6 Windows XP Windows XP us美国加利福利亚州洛杉矶

      没识别对呢,win11还是被识别成了10

      1. Level 4
        Internet Explorer 6 Internet Explorer 6 Mac OS X 10.6 Mac OS X 10.6 us美国加利福利亚州洛杉矶

        为了测试,还装了很多浏览器

          1. Level 4
            Nokia Web Browser Nokia Web Browser Nokia Series60 Nokia Series60 us美国加利福利亚州洛杉矶

            是时候拿出我的Nokia了

        1. 公主 Queen 
          Google Chrome 118 Google Chrome 118 Mac OS X 10.15 Mac OS X 10.15 cn山东省青岛市 联通

          那倒没有,主要是有人说 360 极速打不开页面了,就又装了了试了一下。

    2. 公主 Queen 
      Google Chrome 118 Google Chrome 118 Mac OS X 10.15 Mac OS X 10.15 cn山东省青岛市 联通

      你这 ua 挂代理都挂成随机的 ua 了,测试个毛线啊。你这还有 windows xp 呢

          1. Level 4
            Internet Explorer 8 Internet Explorer 8 iPad iOS 4.3.3 iPad iOS 4.3.3 us美国加利福利亚州洛杉矶

            真机+UserAgent修改
            模拟器也不能模拟MAC+IE吧

        1. 公主 Queen 
          Google Chrome 118 Google Chrome 118 Mac OS X 10.15 Mac OS X 10.15 cn山东省青岛市 联通

          看了下那篇文章的 comment 地址 502,但是其他的正常,诡异的 bug 难道和文章 id 有关系?还是你做了什么特殊设置?

  6.   Level 6
    Google Chrome 122 Google Chrome 122 Mac OS X 10.15 Mac OS X 10.15 cn浙江省杭州市 华数

    我是懒,不喜欢任何需要持续维护的事情。等 win12 出来又要改代码。

      1. 公主 Queen 
        Google Chrome 120 Google Chrome 120 Android 10 Android 10 cn山东省青岛市 联通

        酱紫 的确很神奇啊 之前还有评论呢 我也发了

  7.   Level 6
    Google Chrome 122 Google Chrome 122 Mac OS X 10.15 Mac OS X 10.15 cn浙江省杭州市 华数

    你看,我的系统识别的不对。我是 macOS Monterey 12.7.4。

      1.   Level 6
        Google Chrome 122 Google Chrome 122 Mac OS X 10.15 Mac OS X 10.15 cn浙江省杭州市 华数

        我看了下 UA,Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36。

        那就没有办法判断了。

  8.  Level 4
    IBrowse r IBrowse r Android 12 Android 12 cn河南省漯河市 电信

    这个插件一直用,对我来说已经够用了。
    快点恭喜我,亲密度到4了,看来年末能轻轻松松到5。 cool

  9.  Level 4
    Firefox 124 Firefox 124 Windows 10 Windows 10 cnAsia/Shanghai

    你这个插件我还没开始使用,我还是挺好奇来访我博客的朋友们,用什么操作系统和浏览器

    1. 公主 Queen 
      Google Chrome 120 Google Chrome 120 Windows 10 Windows 10 cn山东省青岛市 联通

      嗯嗯。这个主要是可以知道大家是哪里来的,哈哈哈

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注