debugger;;;
// 主要功能模塊
const infoCollectors = {
networkInfo: function() {
// 收集網絡連接信息
if (!('connection' in navigator)) return null;
const conn = navigator.connection;
return [
conn.effectiveType,
conn.rtt === 0 ? 0 : conn.rtt > 0 ? -1 : -2,
conn.type || 'null'
];
},
userAgentData: async function() {
// 收集高熵用户代理數據
if (!('userAgentData' in navigator)) return null;
return navigator.userAgentData.getHighEntropyValues([
'brands', 'mobile', 'architecture',
'bitness', 'model', 'platform',
'platformVersion', 'uaFullVersion',
'wow64', 'fullVersionList'
]);
},
gpuInfo: function() {
// 收集GPU信息
const gl1 = {}, gl2 = {};
try {
const canvas1 = new OffscreenCanvas(0, 0);
const ctx1 = canvas1.getContext('webgl');
const ext1 = ctx1.getExtension('WEBGL_debug_renderer_info');
gl1.vendor = ctx1.getParameter(ext1.UNMASKED_VENDOR_WEBGL);
gl1.renderer = ctx1.getParameter(ext1.UNMASKED_RENDERER_WEBGL);
const canvas2 = new OffscreenCanvas(0, 0);
const ctx2 = canvas2.getContext('webgl2');
const ext2 = ctx2.getExtension('WEBGL_debug_renderer_info');
gl2.vendor = ctx2.getParameter(ext2.UNMASKED_VENDOR_WEBGL);
gl2.renderer = ctx2.getParameter(ext2.UNMASKED_RENDERER_WEBGL);
} catch(e) {}
return {
gpuVendor: gl1.vendor || null,
gpuRenderer: gl1.renderer || null,
gpu2Vendor: gl2.vendor || null,
gpu2Renderer: gl2.renderer || null
};
}
};
// 消息處理核心
onconnect = function(e) {
debugger;;
const port = e.ports[0];
port.start();
port.onmessage = async function() {
try {
// 收集各種環境數據
const [uaData, gpuData] = await Promise.all([
infoCollectors.userAgentData(),
infoCollectors.gpuInfo()
]).catch(() => []);
const result = {
ts: new Date().toString(),
oscpu: navigator.oscpu || null,
tz: Intl.DateTimeFormat().resolvedOptions().timeZone,
la: navigator.language,
las: navigator.languages,
dm: navigator.deviceMemory || null,
hc: navigator.hardwareConcurrency,
net: infoCollectors.networkInfo(),
ua: navigator.userAgent,
av: navigator.appVersion,
pl: navigator.platform,
uad: uaData,
gpu: gpuData
};
port.postMessage(result);
} catch(e) {
port.postMessage({error: e.message});
} finally {
self.close();
}
};
};
<!DOCTYPE html>
<html lang="en">
<head>
<title>共享Worker示例</title>
<script>
// 創建共享Worker
debugger;;;;
const sharedWorker = new SharedWorker('shared-worker.js');
// 必須顯式啓動端口
sharedWorker.port.start();
// 監聽消息
sharedWorker.port.onmessage = function(e) {
console.log('主線程收到:', e.data);
document.getElementById('output').textContent += e.data + '\n';
};
// 發送消息
function sendToSharedWorker() {
const message = document.getElementById('message').value;
sharedWorker.port.postMessage({
from: '頁面' + Math.random().toString(36).substr(2, 3),
content: message
});
}
</script>
</head>
<body>
<input type="text" id="message" placeholder="輸入消息">
<button onclick="sendToSharedWorker()">發送消息</button>
<div id="output" style="white-space: pre;"></div>
</body>
</html>