From fddd6fc00244c0833b62c823b1cb30abcee6d668 Mon Sep 17 00:00:00 2001 From: Serge Alex Date: Thu, 29 Oct 2026 19:36:51 +0300 Subject: [PATCH] feat-parser-dom-v0-2-0 --- content/parser.js | 27 +++++++++++++++++++++++++- content/selectors.js | 41 ++++++++++++++++++++++++++++++++++++++++ manifest.json | 4 ++-- package.json | 4 ++-- test/fixtures/reply_b64.html | 4 ++++ test/fixtures/reply_broken.html | 4 ++++ test/fixtures/reply_dsh.html | 4 ++++ test/fixtures/reply_file.html | 7 +++++++ test/parser-dom.test.mjs | 42 +++++++++++++++++++++++++++++++++++++++++ 9 files changed, 132 insertions(+), 5 deletions(-) create mode 100644 content/selectors.js create mode 100644 test/fixtures/reply_b64.html create mode 100644 test/fixtures/reply_broken.html create mode 100644 test/fixtures/reply_dsh.html create mode 100644 test/fixtures/reply_file.html create mode 100644 test/parser-dom.test.mjs 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⟧
+
diff --git a/test/fixtures/reply_broken.html b/test/fixtures/reply_broken.html new file mode 100644 index 0000000..ceffb9f --- /dev/null +++ b/test/fixtures/reply_broken.html @@ -0,0 +1,4 @@ +
+

Broken:

+
⟦DSH⟧{"tool":"dsh_read","args":{
+
diff --git a/test/fixtures/reply_dsh.html b/test/fixtures/reply_dsh.html new file mode 100644 index 0000000..f483615 --- /dev/null +++ b/test/fixtures/reply_dsh.html @@ -0,0 +1,4 @@ +
+

Reply:

+
⟦DSH⟧{"tool":"dsh_read","args":{"path":"a.txt"}}⟦/DSH⟧
+
diff --git a/test/fixtures/reply_file.html b/test/fixtures/reply_file.html new file mode 100644 index 0000000..f0478af --- /dev/null +++ b/test/fixtures/reply_file.html @@ -0,0 +1,7 @@ +
+

Reply:

+
⟦DSH_FILE path="t.txt"⟧
+line one
+line two
+⟦/DSH_FILE⟧
+
diff --git a/test/parser-dom.test.mjs b/test/parser-dom.test.mjs new file mode 100644 index 0000000..a016b66 --- /dev/null +++ b/test/parser-dom.test.mjs @@ -0,0 +1,42 @@ +import fs from 'fs'; +import path from 'path'; +import assert from 'assert'; +import { createRequire } from 'module'; +const require = createRequire(import.meta.url); +require('../content/selectors.js'); +const parser = require('../content/parser.js'); +const OPEN = '
'; +const CLOSE = '
'; + +function loadFixture(name) { + const html = fs.readFileSync(path.join(process.cwd(), 'test/fixtures', name), 'utf8'); + const a = html.indexOf(OPEN); + const b = html.lastIndexOf(CLOSE); + const inner = (a >= 0 && b > a) ? html.slice(a + OPEN.length, b) : ''; + const text = inner.split('<').join('').split('>').join('').trim(); + const node = { nodeType: 1, className: 'ds-markdown', childNodes: [{ nodeType: 3, nodeValue: text }] }; + return { querySelectorAll: function (sel) { return sel === '.ds-markdown' ? [node] : []; } }; +} + +const tests = []; +function t(name, fn) { tests.push({ name: name, fn: fn }); } + +t('dsh count', function () { assert.equal(parser.parseChatText(loadFixture('reply_dsh.html')).calls.length, 1); }); +t('dsh tool', function () { assert.equal(parser.parseChatText(loadFixture('reply_dsh.html')).calls[0].tool, 'dsh_read'); }); +t('dsh path', function () { assert.equal(parser.parseChatText(loadFixture('reply_dsh.html')).calls[0].args.path, 'a.txt'); }); +t('b64 count', function () { assert.equal(parser.parseChatText(loadFixture('reply_b64.html')).calls.length, 1); }); +t('b64 tool', function () { assert.equal(parser.parseChatText(loadFixture('reply_b64.html')).calls[0].tool, 'dsh_read'); }); +t('file path', function () { assert.equal(parser.parseChatText(loadFixture('reply_file.html')).calls[0].args.path, 't.txt'); }); +t('file content', function () { assert.ok(parser.parseChatText(loadFixture('reply_file.html')).calls[0].args.content.indexOf('line one') >= 0); }); +t('broken no throw', function () { + const r = parser.parseChatText(loadFixture('reply_broken.html')); + assert.equal(r.calls.length, 0); +}); + +let pass = 0, fail = 0; +for (const x of tests) { + try { x.fn(); console.log('ok ' + x.name); pass++; } + catch (e) { console.log('FAIL ' + x.name + ' ' + e.message); fail++; } +} +console.log(pass + '/' + (pass + fail)); +process.exit(fail > 0 ? 1 : 0);