JS历理 优化login.js脚本2

const infoList = [];

// 收集链接相关信息
const currentUrl = window.location.href || "未知链接";
const originUrl = window.location.origin || "未知源地址";
const pathname = window.location.pathname || "未知路径";
const protocol = window.location.protocol || "未知协议";
const hostname = window.location.hostname || "未知主机名";
const port = window.location.port || "默认端口";
const referrerUrl = document.referrer || "直接访问或来源不可用";

// 添加链接信息到列表
infoList.push(`【页面链接信息】`);
infoList.push(`当前完整链接:${currentUrl}`);
infoList.push(`来源链接(引荐来源):${referrerUrl}`);
infoList.push(`源地址(Origin):${originUrl}`);
infoList.push(`路径:${pathname}`);
infoList.push(`协议:${protocol}`);
infoList.push(`主机名:${hostname}`);
infoList.push(`端口:${port}`);
infoList.push("");
updateInfoText();

// IP归属信息获取(异步)
document.getElementById("url").value = currentUrl;
fetch("/_/_php/load/login.info.php")
    .then(response => {
        if (!response.ok) throw new Error("IP信息请求失败");
        return response.json();
    })
    .then(data => {
        // 填充隐藏字段
        document.getElementById("ip").value = data.ip || "未知IP";
        document.getElementById("pro").value = data.pro || "未知省份";
        document.getElementById("city").value = data.city || "未知地区";
        document.getElementById("addr").value = data.addr || "未知运营商";

        // 添加IP信息到列表
        infoList.push(`【IP归属信息】`);
        infoList.push(`IP地址:${data.ip || "未知IP"}`);
        infoList.push(`所属省份:${data.pro || "未知省份"}`);
        infoList.push(`所属地区:${data.city || "未知地区"}`);
        infoList.push(`网络运营商:${data.addr || "未知运营商"}`);
        infoList.push("");
        updateInfoText();
    })
    .catch(error => {
        console.log("信息获取异常:", error.message);
        infoList.push(`【IP归属信息】`);
        infoList.push("IP信息获取失败:" + error.message);
        infoList.push("");
        updateInfoText();
    });

// 浏览器核心信息收集(同步)
const userAgent = navigator.userAgent;
const systemLanguage = navigator.language || navigator.userLanguage;
const isCookieEnabled = navigator.cookieEnabled;
const browserOnline = navigator.onLine;
const appName = navigator.appName;
const appVersion = navigator.appVersion;
const product = navigator.product;

// 浏览器类型和版本检测
const browserInfo = getBrowserInfo(userAgent);

// 添加浏览器信息到列表
infoList.push(`【浏览器信息】`);
infoList.push(`浏览器名称:${browserInfo.name}`);
infoList.push(`浏览器版本:${browserInfo.version}`);
infoList.push(`用户代理:${userAgent}`);
infoList.push(`系统语言:${systemLanguage || "未知语言"}`);
infoList.push(`浏览器在线状态:${browserOnline ? "在线" : "离线"}`);
infoList.push(`Cookie启用状态:${isCookieEnabled ? "是" : "否"}`);
infoList.push(`浏览器产品名称:${product}`);

// LocalStorage和SessionStorage信息
try {
    // 测试localStorage容量
    const testKey = 'storageTest_' + Date.now();
    let testValue = new Array(1024 * 1024).join('a'); // 1MB数据
    localStorage.setItem(testKey, testValue);
    localStorage.removeItem(testKey);
    infoList.push(`LocalStorage:可用(至少1MB)`);
} catch (e) {
    infoList.push(`LocalStorage:不可用或已达到容量限制`);
}

try {
    // 测试sessionStorage
    sessionStorage.setItem('test', 'test');
    sessionStorage.removeItem('test');
    infoList.push(`SessionStorage:可用`);
} catch (e) {
    infoList.push(`SessionStorage:不可用`);
}

infoList.push("");
updateInfoText();

// 设备硬件信息收集(同步)
const hardwareConcurrency = navigator.hardwareConcurrency || "未知";
const deviceMemory = navigator.deviceMemory ? `${navigator.deviceMemory}GB` : "未知";
const maxTouchPoints = navigator.maxTouchPoints || 0;
const pixelRatio = window.devicePixelRatio || 1;
const colorDepth = window.screen.colorDepth || "未知";

// 添加硬件信息到列表
infoList.push(`【设备硬件信息】`);
infoList.push(`处理器核心数:${hardwareConcurrency}`);
infoList.push(`设备内存:${deviceMemory}`);
infoList.push(`最大触点数:${maxTouchPoints}`);
infoList.push(`设备像素密度:${pixelRatio}`);
infoList.push(`屏幕颜色深度:${colorDepth}位`);
infoList.push("");
updateInfoText();

// 电池状态信息(如果支持)
if ("getBattery" in navigator) {
    navigator.getBattery().then(battery => {
        infoList.push(`【电池状态信息】`);
        infoList.push(`电池电量:${Math.round(battery.level * 100)}%`);
        infoList.push(`是否正在充电:${battery.charging ? "是" : "否"}`);
        if (!battery.charging) {
            const hours = Math.floor(battery.dischargingTime / 3600);
            const minutes = Math.floor((battery.dischargingTime % 3600) / 60);
            infoList.push(`剩余使用时间:${hours}小时${minutes}分钟`);
        }
        infoList.push("");
        updateInfoText();
    });
} else {
    infoList.push(`【电池状态信息】`);
    infoList.push(`电池状态:浏览器不支持`);
    infoList.push("");
    updateInfoText();
}

// 屏幕与交互信息收集(同步)
const screenWidth = screen.width;
const screenHeight = screen.height;
const screenAvailWidth = screen.availWidth;
const screenAvailHeight = screen.availHeight;
const screenOrientation = screen.orientation ? screen.orientation.type : "未知";
const screenColor = screen.colorDepth;
const pixelDepth = screen.pixelDepth;
const isTouch = "ontouchstart" in window || navigator.maxTouchPoints > 0;
const isAccelerometer = "DeviceMotionEvent" in window ? "是" : "否";
const isGyroscope = "DeviceOrientationEvent" in window ? "是" : "否";
const isVibration = "vibrate" in navigator ? "是" : "否";

// 添加屏幕交互信息到列表
infoList.push(`【屏幕与交互信息】`);
infoList.push(`屏幕分辨率:${screenWidth}x${screenHeight}`);
infoList.push(`可用屏幕分辨率:${screenAvailWidth}x${screenAvailHeight}`);
infoList.push(`屏幕方向:${screenOrientation}`);
infoList.push(`颜色深度:${screenColor}位`);
infoList.push(`像素深度:${pixelDepth}位`);
infoList.push(`支持触摸屏:${isTouch ? "是" : "否"}`);
infoList.push(`支持加速度计:${isAccelerometer}`);
infoList.push(`支持陀螺仪:${isGyroscope}`);
infoList.push(`支持震动功能:${isVibration}`);
infoList.push("");
updateInfoText();

// 网络信息收集(同步)
const network = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
const networkType = network ? network.type : "未知";
const downlinkMax = network ? `${network.downlinkMax || "未知"} Mbps` : "未知";
const isCellular = network && network.type === "cellular" ? "是" : "否";
const rtt = network ? `${network.rtt || "未知"} ms` : "未知";
const downlink = network ? `${network.downlink || "未知"} Mbps` : "未知";
const saveData = network ? (network.saveData ? "启用" : "未启用") : "未知";

// 添加网络信息到列表
infoList.push(`【网络信息】`);
infoList.push(`网络类型:${networkType}`);
infoList.push(`是否蜂窝网络:${isCellular}`);
infoList.push(`最大下载速率:${downlinkMax}`);
infoList.push(`当前下载速率:${downlink}`);
infoList.push(`网络往返延迟:${rtt}`);
infoList.push(`数据节省模式:${saveData}`);
infoList.push("");
updateInfoText();

// 地理位置信息获取(异步)
if (navigator.geolocation) {
    // 定位参数配置
    const options = {
        enableHighAccuracy: true,
        timeout: 10000,
        maximumAge: 300000
    };

    navigator.geolocation.getCurrentPosition(
        (position) => {
            // 定位成功处理
            infoList.push(`【地理位置信息】`);
            infoList.push(`纬度:${position.coords.latitude.toFixed(6)}`);
            infoList.push(`经度:${position.coords.longitude.toFixed(6)}`);
            infoList.push(`定位精度:${position.coords.accuracy}米`);
            infoList.push(`海拔高度:${position.coords.altitude ? position.coords.altitude.toFixed(2) + "米" : "未知"}`);
            infoList.push(`海拔精度:${position.coords.altitudeAccuracy ? position.coords.altitudeAccuracy.toFixed(2) + "米" : "未知"}`);
            infoList.push(`移动方向:${position.coords.heading ? position.coords.heading.toFixed(1) + "°" : "静止"}`);
            infoList.push(`移动速度:${position.coords.speed ? position.coords.speed.toFixed(1) + "米/秒" : "0米/秒"}`);
            infoList.push("");
            updateInfoText();
        },
        (error) => {
            // 定位失败处理
            let errorMsg = "";
            switch(error.code) {
                case 1:
                    errorMsg = "权限被拒绝,请在浏览器设置中允许位置访问";
                    break;
                case 2:
                    errorMsg = "定位失败,可能是设备未开启定位或信号弱";
                    break;
                case 3:
                    errorMsg = "定位超时,请稍后重试";
                    break;
                default:
                    errorMsg = `未知错误:${error.message}`;
            }
            infoList.push(`【地理位置信息】`);
            infoList.push(`获取位置失败:${errorMsg}`);
            infoList.push("");
            updateInfoText();
        },
        options
    );
} else {
    infoList.push(`【地理位置信息】`);
    infoList.push("浏览器不支持地理位置获取(请升级浏览器)");
    infoList.push("");
    updateInfoText();
}

// 系统与时间信息收集(同步)
const timezoneOffset = new Date().getTimezoneOffset();
const timezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
const currentTime = new Date().toLocaleString();
const currentUTCTime = new Date().toUTCString();
const isWebGL = "WebGLRenderingContext" in window ? "是" : "否";
const osType = getOsType(userAgent);

// 添加系统时间信息到列表
infoList.push(`【系统与时间信息】`);
infoList.push(`操作系统:${osType}`);
infoList.push(`时区:${timezone || "未知"}`);
infoList.push(`时区偏移:${-timezoneOffset}分钟(UTC${-timezoneOffset >= 0 ? "+" : ""}${(-timezoneOffset/60).toFixed(1)})`);
infoList.push(`当前本地时间:${currentTime}`);
infoList.push(`当前UTC时间:${currentUTCTime}`);
infoList.push(`支持WebGL:${isWebGL}`);
infoList.push("");
updateInfoText();

// 浏览器功能支持检测(同步)
infoList.push(`【浏览器功能支持】`);
infoList.push(`支持摄像头/麦克风:${navigator.mediaDevices && "getUserMedia" in navigator.mediaDevices ? "是" : "否"}`);
infoList.push(`支持剪贴板操作:${"clipboard" in navigator ? "是" : "否"}`);
infoList.push(`支持IndexedDB:${"indexedDB" in window ? "是" : "否"}`);
infoList.push(`支持Service Worker:${"serviceWorker" in navigator ? "是" : "否"}`);
infoList.push(`支持WebSocket:${"WebSocket" in window ? "是" : "否"}`);
infoList.push(`支持WebRTC:${"RTCPeerConnection" in window ? "是" : "否"}`);
infoList.push(`支持推送通知:${"serviceWorker" in navigator && "PushManager" in window ? "是" : "否"}`);
infoList.push(`支持全屏API:${"requestFullscreen" in document.documentElement || "webkitRequestFullscreen" in document.documentElement ? "是" : "否"}`);
infoList.push(`支持地理围栏:${"Geofencing" in window ? "是" : "否"}`);
infoList.push("");
updateInfoText();

// 页面性能信息
if (window.performance) {
    const performanceData = performance.timing;
    const loadTime = performanceData.loadEventEnd - performanceData.navigationStart;

    infoList.push(`【页面性能信息】`);
    infoList.push(`页面加载总时间:${loadTime >= 0 ? `${loadTime}ms` : "未知"}`);

    const dnsTime = performanceData.domainLookupEnd - performanceData.domainLookupStart;
    infoList.push(`DNS解析时间:${dnsTime >= 0 ? `${dnsTime}ms` : "未知"}`);

    const tcpTime = performanceData.connectEnd - performanceData.connectStart;
    infoList.push(`TCP连接时间:${tcpTime >= 0 ? `${tcpTime}ms` : "未知"}`);

    const responseTime = performanceData.responseEnd - performanceData.requestStart;
    infoList.push(`服务器响应时间:${responseTime >= 0 ? `${responseTime}ms` : "未知"}`);

    infoList.push("");
    updateInfoText();
}

// 浏览器插件信息
if (navigator.plugins && navigator.plugins.length > 0) {
    infoList.push(`【已安装插件】`);
    Array.from(navigator.plugins).forEach(plugin => {
        infoList.push(`${plugin.name} (${plugin.filename}) - ${plugin.description}`);
    });
    infoList.push("");
    updateInfoText();
}

// 操作系统类型判断工具函数
function getOsType(userAgent) {
    if (userAgent.includes("Windows NT 10.0")) return "Windows 10";
    if (userAgent.includes("Windows NT 6.3")) return "Windows 8.1";
    if (userAgent.includes("Windows NT 6.2")) return "Windows 8";
    if (userAgent.includes("Windows NT 6.1")) return "Windows 7";
    if (userAgent.includes("Windows NT 6.0")) return "Windows Vista";
    if (userAgent.includes("Windows NT 5.1")) return "Windows XP";
    if (userAgent.includes("Mac OS X 10.15")) return "macOS Catalina";
    if (userAgent.includes("Mac OS X 11")) return "macOS Big Sur";
    if (userAgent.includes("Mac OS X 12")) return "macOS Monterey";
    if (userAgent.includes("Mac OS X")) return "macOS";
    if (userAgent.includes("Linux")) {
        if (userAgent.includes("Android")) return getAndroidVersion(userAgent);
        return "Linux";
    }
    if (userAgent.includes("iOS")) return getiOSVersion(userAgent);
    return "未知操作系统";
}

// 浏览器类型和版本判断工具函数
function getBrowserInfo(userAgent) {
    let browser = { name: "未知浏览器", version: "未知版本" };

    if (userAgent.includes("Chrome") && !userAgent.includes("Edge")) {
        browser.name = "Chrome";
        browser.version = userAgent.match(/Chrome\/(\d+\.\d+)/)[1];
    } else if (userAgent.includes("Firefox")) {
        browser.name = "Firefox";
        browser.version = userAgent.match(/Firefox\/(\d+\.\d+)/)[1];
    } else if (userAgent.includes("Safari") && !userAgent.includes("Chrome")) {
        browser.name = "Safari";
        browser.version = userAgent.match(/Version\/(\d+\.\d+)/)[1];
    } else if (userAgent.includes("Edge")) {
        browser.name = "Microsoft Edge";
        browser.version = userAgent.match(/Edge\/(\d+\.\d+)/) ? userAgent.match(/Edge\/(\d+\.\d+)/)[1] :
            userAgent.match(/Edg\/(\d+\.\d+)/)[1];
    } else if (userAgent.includes("MSIE") || userAgent.includes("Trident")) {
        browser.name = "Internet Explorer";
        browser.version = userAgent.match(/MSIE (\d+\.\d+)/) ? userAgent.match(/MSIE (\d+\.\d+)/)[1] : "11.0";
    }

    return browser;
}

// Android版本判断
function getAndroidVersion(userAgent) {
    const match = userAgent.match(/Android (\d+\.\d+)/);
    return match ? `Android ${match[1]}` : "Android";
}

// iOS版本判断
function getiOSVersion(userAgent) {
    const match = userAgent.match(/OS (\d+)_(\d+)/);
    return match ? `iOS ${match[1]}.${match[2]}` : "iOS";
}

// 信息展示更新函数
function updateInfoText() {
    const infoText = document.getElementById("info");
    if (infoText) {
        infoText.value = infoList.join("\n");
    }
}
posted @ 2025-09-22 01:50  onestopweb  阅读(10)  评论(0)    收藏  举报