在Web开发领域,浏览器提供的API库远比大多数开发者想象的丰富。从《你不知道的神奇的Web API(一)》中,我们已窥见部分冷门但强大的功能,而本文将继续深入挖掘更多”隐藏技能”,帮助开发者突破传统Web应用的边界,打造更智能、更贴近原生体验的交互场景。
一、设备控制:让Web应用”掌控”硬件
1. Web Bluetooth API:低功耗设备的无线连接
传统Web应用受限于安全沙箱,无法直接与蓝牙设备通信。但Web Bluetooth API打破了这一壁垒,允许网页通过BLE(低功耗蓝牙)与硬件设备(如心率带、智能锁、温度传感器)建立连接。其核心流程包括:
// 请求设备连接(需用户交互触发)async function connectToDevice() {try {const device = await navigator.bluetooth.requestDevice({filters: [{ services: ['heart_rate'] }], // 筛选支持心率服务的设备optionalServices: ['generic_access'] // 可选服务});const server = await device.gatt.connect();const service = await server.getPrimaryService('heart_rate');const characteristic = await service.getCharacteristic('heart_rate_measurement');characteristic.addEventListener('valuechanged', event => {const heartRate = event.target.value.getUint8(1); // 解析心率数据console.log(`当前心率: ${heartRate} BPM`);});await characteristic.startNotifications();} catch (error) {console.error('连接失败:', error);}}
应用场景:健身应用实时显示心率数据、智能家居控制面板调节灯光亮度、工业IoT设备监控。
2. WebUSB API:直接访问USB外设
对于需要高速数据传输的场景(如3D打印机控制、数据采集仪),WebUSB API提供了比串口更高效的解决方案。开发者可通过navigator.usb.requestDevice()选择设备,并使用标准USB协议通信:
// 示例:连接USB串口设备async function connectUSB() {const device = await navigator.usb.requestDevice({filters: [{ vendorId: 0x1A86 }] // 常见USB转串口芯片厂商ID});await device.open();if (device.configuration === null) {await device.selectConfiguration(1);}await device.claimInterface(0); // 声明接口// 写入数据const encoder = new TextEncoder();const data = encoder.encode('AT+CMD\r\n');await device.transferOut(1, data); // 端点1通常为OUT端点}
注意事项:需用户主动触发操作(如点击按钮),且浏览器会提示权限请求。
二、文件系统与存储:超越LocalStorage的局限
1. File System Access API:原生文件系统操作
传统文件上传依赖<input type="file">,而File System Access API允许网页直接读写用户指定的文件或目录,甚至创建新文件:
// 示例:保存文本到用户选择的文件async function saveFile() {try {const handle = await window.showSaveFilePicker({suggestedName: 'notes.txt',types: [{description: 'Text Files',accept: { 'text/plain': ['.txt'] }}]});const writable = await handle.createWritable();await writable.write('Hello, File System Access API!');await writable.close();} catch (err) {console.error('用户取消或出错:', err);}}
优势:支持大文件(如视频编辑)、断点续传、直接修改文件而非覆盖。
2. IndexedDB进阶:结构化数据存储
对于需要复杂查询的应用(如离线CRM系统),IndexedDB的索引和事务机制远超LocalStorage:
// 创建带索引的数据库const request = indexedDB.open('MyDatabase', 2);request.onupgradeneeded = (e) => {const db = e.target.result;const store = db.createObjectStore('customers', { keyPath: 'id' });store.createIndex('name', 'name', { unique: false });store.createIndex('email', 'email', { unique: true });};// 查询示例function getCustomerByName(name) {return new Promise((resolve) => {const request = indexedDB.open('MyDatabase');request.onsuccess = (e) => {const db = e.target.result;const transaction = db.transaction('customers', 'readonly');const store = transaction.objectStore('customers');const index = store.index('name');const cursorRequest = index.openCursor(IDBKeyRange.only(name));const results = [];cursorRequest.onsuccess = (e) => {const cursor = e.target.result;if (cursor) {results.push(cursor.value);cursor.continue();} else {resolve(results);}};};});}
三、传感器与环境感知:让Web应用”感知”世界
1. Ambient Light Sensor API:自动调节屏幕亮度
移动设备可通过此API检测环境光强度,动态调整页面亮度或切换深色模式:
if ('AmbientLightSensor' in window) {const sensor = new AmbientLightSensor();sensor.onreading = () => {const lux = sensor.illuminance;document.body.className = lux < 50 ? 'dark-mode' : 'light-mode';};sensor.start();} else {console.warn('环境光传感器不支持');}
2. Proximity Sensor API:防误触设计
手机靠近物体时(如放入口袋),可通过此API暂停视频播放或锁定屏幕:
if ('ProximitySensor' in window) {const sensor = new ProximitySensor();sensor.onreading = () => {if (sensor.near) {document.videoPlayer.pause();}};sensor.start();}
四、性能优化:利用隐藏API提升体验
1. Performance Observer API:监控资源加载
实时跟踪页面资源(如脚本、图片)的加载时间,优化关键渲染路径:
const observer = new PerformanceObserver((list) => {for (const entry of list.getEntries()) {if (entry.name.endsWith('.js')) {console.log(`脚本 ${entry.name} 加载耗时: ${entry.duration}ms`);}}});observer.observe({ entryTypes: ['resource'] });
2. ResizeObserver API:精准响应布局变化
替代传统的window.onresize,精确监听特定元素的尺寸变化:
const element = document.getElementById('resizable-box');const observer = new ResizeObserver((entries) => {for (const entry of entries) {const { width, height } = entry.contentRect;console.log(`元素尺寸变化: ${width}x${height}`);}});observer.observe(element);
五、安全与兼容性建议
- 渐进增强:使用
if ('API' in window)检测支持性,提供降级方案。 - 权限管理:蓝牙/USB操作需用户主动触发,避免自动请求。
- Polyfill方案:对于不支持的API(如旧版Chrome),可引入
@webbluetooth/polyfill等库。 - 性能监控:通过
navigator.connection.effectiveType检测网络状况,动态调整资源加载策略。
这些”神奇的Web API”正在重新定义Web应用的边界。从硬件控制到环境感知,从文件系统操作到性能优化,开发者可通过合理利用这些API,打造出媲美原生应用的体验。未来,随着浏览器标准的演进,更多隐藏能力将被解锁,值得持续关注。