Skip to content

JavaScript 实现获取系统和网络信息

本文介绍了如何使用 JavaScript 获取用户系统和网络信息的方法。以下是实现这些功能的代码示例和说明。

获取更多系统和网络信息

javascript
// 获取更多系统和网络信息
var extendedSystemInfo = {
  userAgent: navigator.userAgent,
  appName: navigator.appName,
  appVersion: navigator.appVersion,
  platform: navigator.platform,
  language: navigator.language,
  cookiesEnabled: navigator.cookieEnabled,
  javaEnabled: navigator.javaEnabled(),
  screen: {
    width: window.screen.width,
    height: window.screen.height,
    colorDepth: window.screen.colorDepth
  },
  online: navigator.onLine,
  // 获取操作系统信息
  os: detectOS(),
  // 获取网络连接信息
  connection: navigator.connection || navigator.mozConnection || navigator.webkitConnection || navigator.msConnection,
  // 获取CPU信息
  cpu: navigator.hardwareConcurrency || 'Unknown',
  // 获取浏览器支持的特性
  features: detectFeatures(),
  // 获取标签页信息
  tabs: getTabsInfo(),
  // 获取Cookie信息
  cookies: getCookies(),
  // 获取LocalStorage信息
  localStorage: getLocalStorage(),
  // 获取SessionStorage信息
  sessionStorage: getSessionStorage()
};

// 检测操作系统
function detectOS() {
  var platform = navigator.platform.toLowerCase();
  if (platform.indexOf('win') !== -1) return 'Windows';
  if (platform.indexOf('mac') !== -1) return 'Mac OS';
  if (platform.indexOf('linux') !== -1) return 'Linux';
  if (/iphone|ipad|ipod|ios/.test(platform)) return 'iOS';
  if (/android/.test(platform)) return 'Android';
  return 'Unknown';
}

// 检测浏览器支持的特性
function detectFeatures() {
  var features = {
    localStorage: !!window.localStorage,
    sessionStorage: !!window.sessionStorage,
    indexedDB: !!window.indexedDB,
    webWorkers: !!window.Worker,
    serviceWorker: 'serviceWorker' in navigator,
    webAssembly: typeof WebAssembly === 'object' && typeof WebAssembly.compile === 'function',
    webGL: !!window.WebGLRenderingContext,
    cookiesEnabled: navigator.cookieEnabled,
    touchEvents: 'ontouchstart' in window || navigator.maxTouchPoints > 0,
    fetchAPI: 'fetch' in window,
    historyAPI: !!window.history && !!history.pushState,
    webRTC: !!navigator.mediaDevices && !!navigator.mediaDevices.getUserMedia,
    webSockets: 'WebSocket' in window || 'MozWebSocket' in window
    // Add more features as needed
  };
  return features;
}

// 获取当前打开的标签页信息
function getTabsInfo() {
  var tabsInfo = [];
  var tabs = window.top || window;
  for (var i = 0; i < tabs.length; i++) {
    var tab = tabs[i];
    tabsInfo.push({
      title: tab.document.title,
      url: tab.location.href
    });
  }
  return tabsInfo;
}

// 获取Cookie信息
function getCookies() {
  var cookies = document.cookie;
  return cookies;
}

// 获取LocalStorage信息
function getLocalStorage() {
  var localStorageInfo = {};
  for (var key in localStorage) {
    if (localStorage.hasOwnProperty(key)) {
      localStorageInfo[key] = localStorage[key];
    }
  }
  return localStorageInfo;
}

// 获取SessionStorage信息
function getSessionStorage() {
  var sessionStorageInfo = {};
  for (var key in sessionStorage) {
    if (sessionStorage.hasOwnProperty(key)) {
      sessionStorageInfo[key] = sessionStorage[key];
    }
  }
  return sessionStorageInfo;
}

// 输出信息到控制台
console.log('Extended System Info:', extendedSystemInfo);

以上代码展示了如何利用 JavaScript 获取用户设备、浏览器和网络信息,并将这些信息存储在一个对象中。通过这些函数,可以获取操作系统、浏览器特性、标签页信息、Cookie、LocalStorage 和 SessionStorage 的详细信息。

Released under the MIT License.