diff --git a/content/parser.js b/content/parser.js index 4ee5e0d..b703f42 100644 --- a/content/parser.js +++ b/content/parser.js @@ -90,4 +90,29 @@ function extractCalls(text, maxCalls) { return { calls: calls, consumed: pos }; } -module.exports = { extractCalls: extractCalls, b64DecodeUtf8: b64DecodeUtf8 }; +function parseChatText(root) { + const SEL = (typeof globalThis !== 'undefined' && globalThis.DSH_SELECTORS) ? globalThis.DSH_SELECTORS : null; + if (!SEL || !root || typeof root.querySelectorAll !== 'function') { + return { calls: [], consumed: 0 }; + } + const texts = []; + for (let k = 0; k < SEL.MESSAGE_SELECTORS.length; k++) { + const sel = SEL.MESSAGE_SELECTORS[k]; + const nodes = root.querySelectorAll(sel); + if (!nodes || nodes.length === 0) continue; + for (let j = 0; j < nodes.length; j++) { + texts.push(SEL.collectText(nodes[j])); + } + if (texts.length > 0) break; + } + const joined = texts.join('\n'); + return extractCalls(joined, 3); +} +if (typeof module !== 'undefined' && module.exports) { + module.exports = { + extractCalls: extractCalls, + b64DecodeUtf8: b64DecodeUtf8, + parseChatText: parseChatText + }; +} + diff --git a/content/selectors.js b/content/selectors.js new file mode 100644 index 0000000..3180c18 --- /dev/null +++ b/content/selectors.js @@ -0,0 +1,41 @@ +'use strict'; + +// Selectors for assistant replies in DeepSeek chat. +// Fallback hierarchy: from specific to generic. Order matters. +const MESSAGE_SELECTORS = [ + '.ds-markdown', + '[class="markdown"]', + '[data-testid="message"]', + '[class*="message"]' +]; + +// Collect text leaf nodes only (no nested blocks). +// Join with LF to keep paragraph separators. +function collectText(root) { + if (!root) return ''; + const parts = []; + const walk = function (n) { + if (!n) return; + if (n.nodeType === 3) { + const t = (n.nodeValue || '').trim(); + if (t.length > 0) parts.push(t); + return; + } + const kids = n.childNodes; + if (!kids) return; + for (let i = 0; i < kids.length; i++) walk(kids[i]); + }; + walk(root); + return parts.join(String.fromCharCode(10)); +}; + +const DSH_SELECTORS = { + MESSAGE_SELECTORS: MESSAGE_SELECTORS, + collectText: collectText +}; + +globalThis.DSH_SELECTORS = DSH_SELECTORS; + +if (typeof module !== 'undefined' && module.exports) { + module.exports = DSH_SELECTORS; +} diff --git a/manifest.json b/manifest.json index 01ec0ef..37d0631 100644 --- a/manifest.json +++ b/manifest.json @@ -1,13 +1,13 @@ { "manifest_version": 3, "name": "DSH Memory (unified)", - "version": "0.1.0", + "version": "0.2.0", "description": "DSH tools + persistent memory, unified", "permissions": ["storage", "scripting", "activeTab"], "host_permissions": ["https://chat.deepseek.com/"], "background": { "service_worker": "background.js" }, "content_scripts": [{ "matches": ["https://chat.deepseek.com/"], - "js": ["content/parser.js"] + "js": ["content/selectors.js", "content/parser.js"] }] } diff --git a/package.json b/package.json index 8472ddf..6711952 100644 --- a/package.json +++ b/package.json @@ -1,11 +1,11 @@ { "name": "dsh-memory", - "version": "0.1.0", + "version": "0.2.0", "private": true, "description": "Unified DSH tools + persistent memory extension", "type": "commonjs", "scripts": { - "test": "node test/parser.test.mjs" + "test": "node test/parser.test.mjs && node test/parser-dom.test.mjs" }, "license": "MIT" } diff --git a/test/fixtures/reply_b64.html b/test/fixtures/reply_b64.html new file mode 100644 index 0000000..ba851c5 --- /dev/null +++ b/test/fixtures/reply_b64.html @@ -0,0 +1,4 @@ +
Reply:
+⟦DSH_B64⟧eyJ0b29sIjoiZHNoX3JlYWQiLCJhcmdzIjp7InBhdGgiOiJhLnR4dCJ9fQ==⟦/DSH_B64⟧+
Broken:
+⟦DSH⟧{"tool":"dsh_read","args":{
+Reply:
+⟦DSH⟧{"tool":"dsh_read","args":{"path":"a.txt"}}⟦/DSH⟧
+Reply:
+⟦DSH_FILE path="t.txt"⟧ +line one +line two +⟦/DSH_FILE⟧+