diff --git a/content/panel.js b/content/panel.js index d4176b6..7e64048 100644 --- a/content/panel.js +++ b/content/panel.js @@ -111,9 +111,16 @@ el.accLimit.addEventListener('change', function () { var n = parseInt(el.accLimit.value, 10); if (!n || n < 1000) n = 900000; state.accLimit = n; try { chrome.storage.local.set({ 'dshmem-acc-limit': n }) } catch (e) { } API.setMetrics(state.lastMetrics); }); el.save.addEventListener('click', function () { var v = el.token.value || ''; if (!v) { API.log('token not changed (empty)'); return; } state.saveCbs.forEach(function (cb) { try { cb(v); } catch (e) {} }); }); el.stg.addEventListener('click', function () { API.toggleForceInject(); }); el.health.addEventListener('click', function () { - try { globalThis.DSH_BRIDGE.health().then(function (r) { + API.log('health: click'); + if (!globalThis.DSH_BRIDGE) { API.log('health: DSH_BRIDGE undefined (bridge-client.js not loaded?)'); return; } + if (typeof globalThis.DSH_BRIDGE.health !== 'function') { API.log('health: DSH_BRIDGE.health is not a function'); return; } + try { chrome.storage.local.get('dshmem-bridge-token', function (v) { + var t = v && v['dshmem-bridge-token'] ? String(v['dshmem-bridge-token']) : ''; + API.log('health: token in storage len=' + t.length + (t ? ' head=' + t.slice(0, 8) + '...' : ' (empty)')); + globalThis.DSH_BRIDGE.health().then(function (r) { if (r && r.ok) { API.setConnected(true); API.log('health ok, bridge up'); } - else { API.setConnected(false); API.log('health fail: ' + ((r && r.error && r.error.message) || '?')); } + else { API.setConnected(false); API.log('health fail: ' + ((r && r.error && (r.error.code + ' ' + r.error.message)) || '?')); } + }).catch(function (e) { API.log('health promise reject: ' + String(e)); }); }); } catch (e) { API.log('health throw: ' + String(e)); } }); el.mcpHead.addEventListener('click', function () { @@ -291,7 +298,11 @@ el.preview.onclick = function () { cancelled = true; clearInterval(iv); el.preview.style.display = 'none'; el.preview.onclick = null; if (opt.onCancel) opt.onCancel(); }; }, hidePreview: function () { if (el.preview) el.preview.style.display = 'none'; }, - init: function () { build(); wire(); try { chrome.storage.local.get('dshmem-transport', function (v) { if (v && v['dshmem-transport']) { var tv = v['dshmem-transport']; if (tv === 'msg') tv = 'dom'; if (tv === 'xhr') tv = 'sse'; API.setTransport(tv) } }) } catch (e) { } try { chrome.storage.local.get('dshmem-acc-limit', function (v) { var n = v && v['dshmem-acc-limit'] ? parseInt(v['dshmem-acc-limit'], 10) : 0; if (n && n >= 1000) { state.accLimit = n; if (el.accLimit) el.accLimit.value = String(n); API.setMetrics(state.lastMetrics); } }) } catch (e) { } + init: function () { + try { build(); } catch (e) { console.error('[dshmem] build failed: ' + e); } + try { wire(); } catch (e) { console.error('[dshmem] wire failed: ' + e); API.log('wire failed: ' + String(e)); } + try { chrome.storage.local.get('dshmem-bridge-token', function (v) { var t = v && v['dshmem-bridge-token'] ? String(v['dshmem-bridge-token']) : ''; if (t && el.token) el.token.value = t; API.log('panel loaded, token in storage len=' + t.length); }) } catch (e) { } + try { chrome.storage.local.get('dshmem-transport', function (v) { if (v && v['dshmem-transport']) { var tv = v['dshmem-transport']; if (tv === 'msg') tv = 'dom'; if (tv === 'xhr') tv = 'sse'; API.setTransport(tv) } }) } catch (e) { } try { chrome.storage.local.get('dshmem-acc-limit', function (v) { var n = v && v['dshmem-acc-limit'] ? parseInt(v['dshmem-acc-limit'], 10) : 0; if (n && n >= 1000) { state.accLimit = n; if (el.accLimit) el.accLimit.value = String(n); API.setMetrics(state.lastMetrics); } }) } catch (e) { } try { chrome.storage.local.get('dshmem-force-inject', function (v) { state.forceInject = Boolean(v && v['dshmem-force-inject']); API._applyStg(); }) } catch (e) { } try { chrome.storage.onChanged.addListener(function (ch, area) { if (area !== 'local') return; if (ch['dshmem-force-inject']) { state.forceInject = Boolean(ch['dshmem-force-inject'].newValue); API._applyStg(); } }); } catch (e) { } } };