IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    漫谈 Windows 11 的 UserAgent

    obaby发表于 2024-03-19 00:51:57
    love 0

    最近一直在折腾跟 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';
            }

    实际效果:

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

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

    修复插件下载:

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



沪ICP备19023445号-2号
友情链接