美文网首页
JS获取移动端系统信息

JS获取移动端系统信息

作者: 人类进化又没带我 | 来源:发表于2023-08-27 11:05 被阅读0次

JS获取移动端系统信息(操作系统、操作系统版本、横竖屏状态、设备类型、网络状态、生成浏览器指纹)

function getOS() { // 获取当前操作系统
    var os;
    if (navigator.userAgent.indexOf('Android') > -1 || navigator.userAgent.indexOf('Linux') > -1) {
        os = 'Android';
    } else if (navigator.userAgent.indexOf('iPhone') > -1) {
        os = 'iOS';
    } else if (navigator.userAgent.indexOf('Windows Phone') > -1) {
        os = 'WP';
    } else {
        os = 'Others';
    }
    return os;
}

function getOSVersion() { // 获取操作系统版本
    var OSVision = '1.0';
    var u = navigator.userAgent;
    var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1; //Android
    var isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    if (isAndroid) {
        OSVision = navigator.userAgent.split(';')[1].match(/\d+\.\d+/g)[0];
    }
    if (isIOS) {
        OSVision = navigator.userAgent.split(';')[1].match(/(\d+)_(\d+)_?(\d+)?/)[0];
    }
    return OSVision;
}

function getOrientationStatu() { // 获取横竖屏状态
    var orientationStatu;
    if (window.screen.orientation.angle == 180 || window.screen.orientation.angle == 0) { // 竖屏
        orientationStatu = '竖屏';
    }
    if (window.screen.orientation.angle == 90 || window.screen.orientation.angle == -90) { // 横屏
        orientationStatu = '横屏';
    }
    return orientationStatu;
}

function getDeviceType() { // 获取设备类型
    var deviceType;
    var sUserAgent = navigator.userAgent.toLowerCase();
    var bIsIpad = sUserAgent.match(/(ipad)/i) == "ipad";
    var bIsIphoneOs = sUserAgent.match(/iphone os/i) == "iphone os";
    var bIsMidp = sUserAgent.match(/midp/i) == "midp";
    var bIsUc7 = sUserAgent.match(/rv:1.2.3.4/i) == "rv:1.2.3.4";
    var bIsUc = sUserAgent.match(/ucweb/i) == "ucweb";
    var bIsAndroid = sUserAgent.match(/android/i) == "android";
    var bIsCE = sUserAgent.match(/windows ce/i) == "windows ce";
    var bIsWM = sUserAgent.match(/windows mobile/i) == "windows mobile";

    if (!(bIsIpad || bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM)) {
        deviceType = 'PC'; //pc
    } else if (bIsIphoneOs || bIsMidp || bIsUc7 || bIsUc || bIsAndroid || bIsCE || bIsWM) {
        deviceType = 'phone'; //phone
    } else if (bIsIpad) {
        deviceType = 'ipad'; //ipad
    } else {
        deviceType = '未知';
    }
    return deviceType;
}

function getNetWork() { // 获取网络状态
    var netWork;
    switch (navigator.connection.effectiveType) {
        case 'wifi':
            netWork = 'wifi'; // wifi
            break;
        case '4g':
            netWork = '4G'; // 4g
            break;
        case '2g':
            netWork = '2G'; // 2g
            break;
        case  '3g':
            netWork = '3G'; // 3g
            break;
        case  'ethernet':
            netWork = '以太网'; // ethernet
            break;
        case  'default':
            netWork = '未知'; // 未知
            break;
    }
    return netWork;
}

function createFingerprint(domain) { // 生成浏览器指纹
    var fingerprint;

    function bin2hex(s) {
        var i, l, n, o = '';
        s += '';
        for (i = 0, l = s.length; i < l; i++) {
            n = s.charCodeAt(i)
                .toString(16);
            o += n.length < 2 ? '0' + n : n;
        }
        return o;
    }

    var canvas = document.createElement('canvas');
    var ctx = canvas.getContext('2d');
    var txt = domain || window.location.host;
    ctx.textBaseline = "top";
    ctx.font = "14px 'Arial'";
    ctx.textBaseline = "tencent";
    ctx.fillStyle = "#f60";
    ctx.fillRect(125, 1, 62, 20);
    ctx.fillStyle = "#069";
    ctx.fillText(txt, 2, 15);
    ctx.fillStyle = "rgba(102, 204, 0, 0.7)";
    ctx.fillText(txt, 4, 17);

    var b64 = canvas.toDataURL().replace("data:image/png;base64,", "");
    var bin = atob(b64);
    var crc = bin2hex(bin.slice(-16, -12));
    fingerprint = crc;
    return fingerprint;

}

相关文章

  • js 获取移动端常用信息

    判断是否是安卓 var isAndroid = /Android/i.test(navigator.userAge...

  • js & jQuery 相关链接

    jquery获取当前页面的URL信息jQuery 教程移动端Web页面适配方案浅谈js运行机制(线程)JavaSc...

  • 移动端userAgent信息获取

    移动端userAgent举例: android:5.0 (Linux; Android 7.0; MIX Buil...

  • 操作系统判断

    1、js判断移动端系统 2、js判断是否PC端 3、js判断是否为微信内置浏览器 4、js判断是否为ie浏览器,并...

  • uniapp 获取元素高度

    获取系统信息: uni.getSystemInfoSync() app端: constquery=uni.crea...

  • H5移动端事件与设备宽度

    移动端获取设备宽度 基本上所有的移动端获取宽度,都是从getBoundingClientRect()对象上获取, ...

  • 综合

    现在pc端和移动端都有适应了,如果要pc和移动端都要的话,可以用js里面的 :js判断是移动端还是pc端,写两套吧...

  • js-插件/框架

    1.swiper.js Swiper常用于移动端网站的内容触摸滑动 功能: 移动端轮播图 swiper.js...

  • 2019-07-05

    [js获取ip地址,操作系统,浏览器版本等信息,可兼容 这次呢,说一下使用js获取用户电脑的ip信息,刚开始只是想...

  • 前端常用插件

    touch.js--百度开发移动端手势库 isscroll--js模拟上拉加载下拉刷新 hammer--移动端手势...

网友评论

      本文标题:JS获取移动端系统信息

      本文链接:https://www.haomeiwen.com/subject/bfcwmdtx.html