fix(tmwd): show real WS status on badge, click-through + lower opacity

This commit is contained in:
Liang Jiaqing committed 2026-08-13 07:54:14 +08:00
1 parent 63f9db74e6
commit e519734ab0
2 files changed
+13 -4

No files matched your search

+7
View File
@@ -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();
};
+6 -4
View File
@@ -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);
})();