From e519734ab0144e4fa2fd642f0110ca55b1f0b69e Mon Sep 17 00:00:00 2001 From: Liang Jiaqing Date: Thu, 13 Aug 2026 07:54:14 +0800 Subject: [PATCH] fix(tmwd): show real WS status on badge, click-through + lower opacity --- assets/tmwd_cdp_bridge/background.js | 7 +++++++ assets/tmwd_cdp_bridge/content.js | 10 ++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/assets/tmwd_cdp_bridge/background.js b/assets/tmwd_cdp_bridge/background.js index 6dbd4b79..1ef32695 100644 --- a/assets/tmwd_cdp_bridge/background.js +++ b/assets/tmwd_cdp_bridge/background.js @@ -16,6 +16,7 @@ chrome.runtime.onInstalled.addListener(() => { }); async function handleExtMessage(msg, sender) { + if (msg.cmd === 'status') return { ok: true, data: status }; if (msg.cmd === 'cookies') return await handleCookies(msg, sender); if (msg.cmd === 'cdp') return await handleCDP(msg, sender); if (msg.cmd === 'batch') return await handleBatch(msg, sender); @@ -211,7 +212,9 @@ function buildCdpScript(code) { // --- WebSocket Client for TMWebDriver --- let ws = null; +let status = 'connected'; const WS_URL = 'ws://127.0.0.1:18765'; +function setStatus(s){if(s===status)return;status=s;chrome.tabs.query({}).then(T=>T.forEach(t=>chrome.tabs.sendMessage(t.id,{type:'tmwd_status',data:s}).catch(()=>{})));} function scheduleProbe() { // Use chrome.alarms to survive MV3 service worker suspension @@ -256,6 +259,7 @@ chrome.alarms.onAlarm.addListener(async (alarm) => { console.log('[TMWD-WS] Server detected, connecting...'); connectWS(); } else { + setStatus('disconnected'); scheduleProbe(); // Server not up, keep probing } } @@ -339,6 +343,7 @@ function connectWS() { console.log('[TMWD-WS] Connecting to', WS_URL); try { ws = new WebSocket(WS_URL); + setStatus('connecting'); } catch (e) { console.error('[TMWD-WS] Constructor error:', e); ws = null; @@ -347,6 +352,7 @@ function connectWS() { } ws.onopen = async () => { console.log('[TMWD-WS] Connected!'); + setStatus('connected'); scheduleKeepalive(); // Keep SW alive while connected const tabs = (await chrome.tabs.query({})).filter(t => isScriptable(t.url)); ws.send(JSON.stringify({ @@ -385,6 +391,7 @@ function connectWS() { }; ws.onclose = () => { console.log('[TMWD-WS] Disconnected'); + setStatus('connecting'); ws = null; scheduleProbe(); }; diff --git a/assets/tmwd_cdp_bridge/content.js b/assets/tmwd_cdp_bridge/content.js index ef2db253..952c5378 100644 --- a/assets/tmwd_cdp_bridge/content.js +++ b/assets/tmwd_cdp_bridge/content.js @@ -7,10 +7,12 @@ document.querySelectorAll('meta[http-equiv="Content-Security-Policy"]').forEach( (function(){ if(window.self!==window.top)return; const d=document.createElement('div'); - d.id='ljq-ind'; - d.innerText='ljq_driver: 已连接'; - d.style.cssText='position:fixed;bottom:8px;right:8px;background:#4CAF50;color:white;padding:4px 7px;border-radius:4px;font-size:11px;font-weight:bold;z-index:99999;cursor:pointer;box-shadow:0 2px 4px rgba(0,0,0,0.2);opacity:0.5;'; - d.addEventListener('click',()=>alert('会话活跃\nURL: '+location.href)); + d.textContent='ljq_driver: 已连接'; + d.style.cssText='position:fixed;bottom:8px;right:8px;background:#4CAF50;color:white;padding:4px 7px;border-radius:4px;font-size:11px;font-weight:bold;z-index:99999;box-shadow:0 2px 4px rgba(0,0,0,0.2);opacity:0.2;pointer-events:none;'; + const S={connected:['已连接','#4CAF50'],connecting:['重连中','#FF9800'],disconnected:['断开','#F44336']}; + const R=s=>{const[t,c]=S[s]||S.disconnected;d.textContent='ljq_driver: '+t;d.style.background=c}; + chrome.runtime.onMessage.addListener(m=>{if(m?.type==='tmwd_status')R(m.data)}); + chrome.runtime.sendMessage({cmd:'status'},r=>R(r?.ok?r.data:'connected')); (document.body||document.documentElement).appendChild(d); })();