diff --git a/background.js b/background.js index 945ed1a..959d426 100644 --- a/background.js +++ b/background.js @@ -3,7 +3,7 @@ // Единственное место, откуда идут HTTP-запросы к локальному мосту (CORS не мешает). const BRIDGE_BASE = "http://127.0.0.1:8766"; -const TOKEN_KEY = "dshpro-bridge-token"; +const TOKEN_KEY = "dshmem-bridge-token"; const TIMEOUT_MS = 65000; async function readToken() { diff --git a/content/bridge-client.js b/content/bridge-client.js index f063c3e..c1fe8d1 100644 --- a/content/bridge-client.js +++ b/content/bridge-client.js @@ -3,7 +3,7 @@ else root.DSH_BRIDGE = factory(); }(typeof globalThis !== "undefined" ? globalThis : this, function () { "use strict"; - const TOKEN_KEY = "dshpro-bridge-token"; + const TOKEN_KEY = "dshmem-bridge-token"; const ENABLED_KEY = "dsh-memory-enabled"; function available() { diff --git a/content/content.js b/content/content.js index cbd8a64..8f13245 100644 --- a/content/content.js +++ b/content/content.js @@ -7,6 +7,7 @@ if (typeof globalThis.extractCalls === "function") { extractCalls = globalThis.extractCalls; } + var UI = globalThis.DSHMEM || null; if (!SEL) return; if (!BRIDGE) return; if (typeof extractCalls !== "function") return; @@ -78,6 +79,7 @@ processed.add(m); rememberHash(h); for (k = 0; k < parsed.calls.length; k++) queue.push(parsed.calls[k]); + if (UI) UI.setPhase('executing'); } pump(); } @@ -99,6 +101,7 @@ var step = function () { if (queue.length === 0) { pumping = false; + if (UI) UI.setPhase('idle'); return; } var call = queue.shift(); @@ -106,11 +109,14 @@ if (res) { if (res.ok) { console.log("[dsh-memory] call ok", call.tool, res.durationMs); + if (UI) UI.log('ok ' + call.tool); } else { console.warn("[dsh-memory] call failed", call.tool, res.error); + if (UI) UI.log('fail ' + call.tool); } } else { console.warn("[dsh-memory] call failed", call.tool); + if (UI) UI.log('fail ' + call.tool); } }).catch(function (err) { console.warn("[dsh-memory] call throw", call.tool, String(err)); @@ -156,19 +162,27 @@ BRIDGE.isEnabled().then(function (enabled) { if (!enabled) { console.warn("[dsh-memory] disabled (dsh-memory-enabled=false), idle"); + if (UI) { UI.setPhase('off'); UI.setConnected(false); UI.log('disabled'); } return; } return BRIDGE.hasToken().then(function (has) { if (!has) { console.warn("[dsh-memory] no bridge token, idle"); + if (UI) { UI.setPhase('off'); UI.setConnected(false); UI.log('no token'); } return; } + if (UI) { + UI.onResetLoops(function () { queue.length = 0; pending.clear(); seenHashes.clear(); pumping = false; if (UI) UI.log('loops reset'); }); + UI.onStop(function () { queue.length = 0; if (UI) UI.log('stopped'); }); + UI.onSaveToken(function (v) { chrome.storage.local.set({ 'dshmem-bridge-token': v }, function () { if (UI) UI.log('token saved, reload page'); }); }); + } obs.observe(document.documentElement, { childList: true, subtree: true, characterData: true }); console.log("[dsh-memory] content.js active"); + if (UI) { UI.setPhase('idle'); UI.setConnected(true); UI.log('active'); } }); }); })(); diff --git a/content/overlay.css b/content/overlay.css new file mode 100644 index 0000000..5d708fc --- /dev/null +++ b/content/overlay.css @@ -0,0 +1,113 @@ +/* DSH PRO - overlay.css : 页面右下角浮动面板样式 */ + +.dshmem-panel { + position: fixed; + right: 18px; + bottom: 18px; + width: 320px; + max-height: 70vh; + z-index: 2147483646; + background: rgba(24, 26, 31, 0.92); + color: #e8eaed; + border: 1px solid rgba(255, 255, 255, 0.14); + border-radius: 12px; + font: 12px/1.5 "Segoe UI", "Microsoft YaHei", sans-serif; + box-shadow: 0 8px 32px rgba(0, 0, 0, 0.45); + backdrop-filter: blur(8px); + display: flex; + flex-direction: column; + overflow: hidden; +} +.dshmem-panel.dshmem-min { max-height: none; } + +.dshmem-head { + display: flex; + align-items: center; + gap: 8px; + padding: 8px 12px; + background: rgba(255, 255, 255, 0.06); + cursor: pointer; + user-select: none; +} +.dshmem-dot { + width: 9px; height: 9px; border-radius: 50%; + background: #666; flex: none; +} +.dshmem-dot.ok { background: #34c759; } +.dshmem-dot.err { background: #ff5f57; } +.dshmem-dot.busy { background: #ffcc00; animation: dshmem-blink 1s infinite; } +@keyframes dshmem-blink { 50% { opacity: 0.35; } } + +.dshmem-title { font-weight: 600; letter-spacing: 0.4px; } +.dshmem-phase { color: #9aa0a6; font-size: 11px; } +.dshmem-toggle { margin-left: auto; color: #9aa0a6; cursor: pointer; } + +.dshmem-body { padding: 8px 12px 12px; overflow-y: auto; } +.dshmem-min .dshmem-body { display: none; } + +.dshmem-row { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; } +.dshmem-btn { + flex: 1 1 auto; + padding: 5px 10px; + border: 1px solid rgba(255,255,255,0.18); + border-radius: 8px; + background: rgba(255,255,255,0.08); + color: inherit; + cursor: pointer; + font: inherit; + white-space: nowrap; +} +.dshmem-btn:hover { background: rgba(255,255,255,0.16); } +.dshmem-btn.primary { background: #2f81f7; border-color: #2f81f7; } +.dshmem-btn.primary:hover { background: #4a93ff; } +.dshmem-btn.danger { background: #b62324; border-color: #b62324; } +.dshmem-btn:disabled { opacity: 0.45; cursor: not-allowed; } + +.dshmem-select, .dshmem-input { + width: 100%; + box-sizing: border-box; + padding: 5px 8px; + border-radius: 8px; + border: 1px solid rgba(255,255,255,0.18); + background: rgba(0,0,0,0.3); + color: inherit; + font: inherit; +} + +.dshmem-log { + margin-top: 8px; + padding: 6px 8px; + background: rgba(0,0,0,0.35); + border-radius: 8px; + max-height: 140px; + overflow-y: auto; + font: 11px/1.5 Consolas, monospace; + color: #b9bbbe; + white-space: pre-wrap; + word-break: break-all; +} + +.dshmem-preview { + margin-top: 8px; + padding: 6px 8px; + background: rgba(255, 204, 0, 0.08); + border: 1px dashed rgba(255, 204, 0, 0.5); + border-radius: 8px; + font: 11px/1.5 Consolas, monospace; + max-height: 120px; + overflow-y: auto; + white-space: pre-wrap; + word-break: break-all; +} +.dshmem-confirm-box { + margin-top: 8px; + padding: 8px; + background: rgba(255, 95, 87, 0.12); + border: 1px solid rgba(255, 95, 87, 0.5); + border-radius: 8px; +} +.dshmem-confirm-title { font-weight: 600; color: #ff8780; } + +.dshmem-settings { margin-top: 8px; border-top: 1px solid rgba(255,255,255,0.12); padding-top: 8px; } +.dshmem-settings .dshmem-input { margin-top: 4px; } +.dshmem-label { color: #9aa0a6; font-size: 11px; margin-top: 6px; } diff --git a/content/panel.js b/content/panel.js new file mode 100644 index 0000000..cd152b8 --- /dev/null +++ b/content/panel.js @@ -0,0 +1,133 @@ +'use strict'; +(function () { + var UI_ID = 'dshmem-panel'; + var LOG_CAP = 60; + var state = { + phase: 'off', + connected: false, + loopsLeft: 0, loopsMax: 0, + dailyCount: 0, dailyMax: 0, + resetCbs: [], stopCbs: [], saveCbs: [] + }; + var el = {}; + function byId(id) { return document.getElementById(id); } + function mk(tag, cls, txt) { + var n = document.createElement(tag); + if (cls) n.className = cls; + if (txt !== undefined) n.textContent = txt; + return n; + } + function build() { + if (byId(UI_ID)) return; + var root = mk('div', 'dshmem-panel'); + root.id = UI_ID; + var head = mk('div', 'dshmem-head'); + el.dot = mk('span', 'dshmem-dot'); + el.dot.id = 'dshmem-dot'; + var title = mk('span', 'dshmem-title', 'DSH Memory'); + el.phase = mk('span', 'dshmem-phase', 'Off'); + el.phase.id = 'dshmem-phase'; + el.toggle = mk('span', 'dshmem-toggle', '-'); + head.appendChild(el.dot); head.appendChild(title); head.appendChild(el.phase); head.appendChild(el.toggle); + el.body = mk('div', 'dshmem-body'); + root.appendChild(head); root.appendChild(el.body); + var row1 = mk('div', 'dshmem-row'); + el.reset = mk('button', 'dshmem-btn', 'Reset loops'); + el.stop = mk('button', 'dshmem-btn danger', 'Stop'); + row1.appendChild(el.reset); row1.appendChild(el.stop); + el.body.appendChild(row1); + el.loops = mk('div', 'dshmem-label', 'loops: 0/0'); + el.daily = mk('div', 'dshmem-label', 'daily: 0/0'); + el.body.appendChild(el.loops); el.body.appendChild(el.daily); + var set = mk('div', 'dshmem-settings'); + var lbl = mk('div', 'dshmem-label', 'Token (save, then reload page)'); + el.token = mk('input', 'dshmem-input'); + el.token.type = 'password'; + el.token.placeholder = 'dshmem-bridge-token'; + var row2 = mk('div', 'dshmem-row'); + el.save = mk('button', 'dshmem-btn primary', 'Save'); + el.health = mk('button', 'dshmem-btn', 'Health'); + row2.appendChild(el.save); row2.appendChild(el.health); + set.appendChild(lbl); set.appendChild(el.token); set.appendChild(row2); + el.body.appendChild(set); + el.log = mk('div', 'dshmem-log'); + el.log.id = 'dshmem-log'; + el.body.appendChild(el.log); + el.preview = mk('div', 'dshmem-preview'); + el.preview.style.display = 'none'; + el.body.appendChild(el.preview); + el.confirm = mk('div', 'dshmem-confirm-box'); + el.confirm.style.display = 'none'; + el.body.appendChild(el.confirm); + document.body.appendChild(root); + } + function wire() { + el.toggle.addEventListener('click', function () { + el.body.style.display = el.body.style.display === 'none' ? '' : 'none'; + }); + el.reset.addEventListener('click', function () { state.resetCbs.forEach(function (cb) { try { cb(); } catch (e) {} }); }); + el.stop.addEventListener('click', function () { state.stopCbs.forEach(function (cb) { try { cb(); } catch (e) {} }); }); + el.save.addEventListener('click', function () { var v = el.token.value || ''; state.saveCbs.forEach(function (cb) { try { cb(v); } catch (e) {} }); }); + el.health.addEventListener('click', function () { + try { globalThis.DSH_BRIDGE.health().then(function (r) { + if (r && r.ok) { API.setConnected(true); API.setPhase('Ready'); API.log('health ok'); } + else { API.setConnected(false); API.log('health fail: ' + ((r && r.error && r.error.message) || '?')); } + }); } catch (e) { API.log('health throw: ' + String(e)); } + }); + } + var API = { + setPhase: function (p) { state.phase = p; if (el.phase) el.phase.textContent = p; }, + setConnected: function (b) { state.connected = !!b; if (el.dot) el.dot.className = 'dshmem-dot ' + (b ? 'ok' : 'err'); }, + log: function (m) { + if (!el.log) return; + var t = new Date().toLocaleTimeString(); + var line = '[' + t + '] ' + m; + var txt = (line + String.fromCharCode(10) + (el.log.textContent || '')).split(String.fromCharCode(10)).slice(0, LOG_CAP).join(String.fromCharCode(10)); + el.log.textContent = txt; + }, + setLoops: function (left, max) { state.loopsLeft = left; state.loopsMax = max; if (el.loops) el.loops.textContent = 'loops: ' + left + '/' + max; }, + setDaily: function (c, m) { state.dailyCount = c; state.dailyMax = m; if (el.daily) el.daily.textContent = 'daily: ' + c + '/' + m; }, + onResetLoops: function (cb) { state.resetCbs.push(cb); }, + onStop: function (cb) { state.stopCbs.push(cb); }, + onSaveToken: function (cb) { state.saveCbs.push(cb); }, + getToken: function () { return el.token ? el.token.value : ''; }, + setToken: function (v) { if (el.token) el.token.value = v || ''; }, + showConfirm: function (opt) { + if (!el.confirm) return; + opt = opt || {}; + el.confirm.textContent = ''; + el.confirm.appendChild(mk('div', 'dshmem-confirm-title', opt.title || 'Confirm')); + el.confirm.appendChild(mk('div', 'dshmem-label', opt.detail || '')); + var r = mk('div', 'dshmem-row'); + var ok = mk('button', 'dshmem-btn primary', 'Allow'); + var no = mk('button', 'dshmem-btn danger', 'Deny'); + ok.addEventListener('click', function () { API.hideConfirm(); if (opt.onAllow) opt.onAllow(); }); + no.addEventListener('click', function () { API.hideConfirm(); if (opt.onDeny) opt.onDeny(); }); + r.appendChild(ok); r.appendChild(no); el.confirm.appendChild(r); + el.confirm.style.display = 'block'; + }, + hideConfirm: function () { if (el.confirm) el.confirm.style.display = 'none'; }, + showPreview: function (opt) { + if (!el.preview) return; + opt = opt || {}; + var sec = opt.seconds || 2; + var text = opt.text || ''; + var cancelled = false; + el.preview.style.display = 'block'; + var render = function () { el.preview.textContent = 'Sending in ' + sec + 's (click to cancel):' + String.fromCharCode(10) + text.slice(0, 500); }; + render(); + var iv = setInterval(function () { + sec--; + if (sec <= 0) { clearInterval(iv); el.preview.style.display = 'none'; el.preview.onclick = null; if (!cancelled && opt.onSend) opt.onSend(); } + else render(); + }, 1000); + 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(); } + }; + globalThis.DSHMEM = API; + if (document.readyState === 'loading') { + document.addEventListener('DOMContentLoaded', function () { API.init(); }); + } else { API.init(); } +})(); \ No newline at end of file diff --git a/manifest.json b/manifest.json index ba5cc54..0d2204a 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "DSH Memory (unified)", - "version": "0.3.0", + "version": "0.3.2", "description": "DSH tools + persistent memory, unified", "permissions": [ "storage", @@ -24,7 +24,11 @@ "content/selectors.js", "content/parser.js", "content/bridge-client.js", + "content/panel.js", "content/content.js" + ], + "css": [ + "content/overlay.css" ] } ]