2 changed files with 159 additions and 1 deletions
@ -0,0 +1,157 @@
@@ -0,0 +1,157 @@
|
||||
"use strict"; |
||||
|
||||
(function () { |
||||
var SEL = globalThis.DSH_SELECTORS; |
||||
var BRIDGE = globalThis.DSH_BRIDGE; |
||||
var extractCalls = null; |
||||
if (typeof globalThis.extractCalls === "function") { |
||||
extractCalls = globalThis.extractCalls; |
||||
} |
||||
if (!SEL) return; |
||||
if (!BRIDGE) return; |
||||
if (typeof extractCalls !== "function") return; |
||||
|
||||
var DEBOUNCE_MS = 800; |
||||
var processed = new WeakSet(); |
||||
var seenHashes = new Map(); |
||||
var HASH_CAP = 50; |
||||
var queue = []; |
||||
var pumping = false; |
||||
var timer = null; |
||||
var pending = new Set(); |
||||
|
||||
function hashText(s) { |
||||
var h = 5381; |
||||
var i; |
||||
for (i = 0; i < s.length; i++) { |
||||
h = ((h << 5) + h + s.charCodeAt(i)) >>> 0; |
||||
} |
||||
return h.toString(36) + ":" + s.length; |
||||
} |
||||
|
||||
function rememberHash(h) { |
||||
seenHashes.set(h, true); |
||||
if (seenHashes.size > HASH_CAP) { |
||||
var first = seenHashes.keys().next().value; |
||||
seenHashes.delete(first); |
||||
} |
||||
} |
||||
|
||||
function findMessages(node) { |
||||
var out = []; |
||||
if (!node) return out; |
||||
var i; |
||||
var j; |
||||
for (i = 0; i < SEL.MESSAGE_SELECTORS.length; i++) { |
||||
var sel = SEL.MESSAGE_SELECTORS[i]; |
||||
if (node.matches) { |
||||
if (node.matches(sel)) out.push(node); |
||||
} |
||||
if (node.querySelectorAll) { |
||||
var kids = node.querySelectorAll(sel); |
||||
for (j = 0; j < kids.length; j++) out.push(kids[j]); |
||||
} |
||||
} |
||||
return out; |
||||
} |
||||
|
||||
function handleNode(node) { |
||||
if (!node) return; |
||||
if (node.nodeType !== 1) return; |
||||
var msgs = findMessages(node); |
||||
var i; |
||||
var k; |
||||
for (i = 0; i < msgs.length; i++) { |
||||
var m = msgs[i]; |
||||
if (processed.has(m)) continue; |
||||
var text = SEL.collectText(m); |
||||
if (!text) continue; |
||||
var h = hashText(text); |
||||
if (seenHashes.has(h)) { |
||||
processed.add(m); |
||||
continue; |
||||
} |
||||
var parsed = extractCalls(text, 3); |
||||
if (!parsed) continue; |
||||
if (!parsed.calls) continue; |
||||
if (parsed.calls.length === 0) continue; |
||||
processed.add(m); |
||||
rememberHash(h); |
||||
for (k = 0; k < parsed.calls.length; k++) queue.push(parsed.calls[k]); |
||||
} |
||||
pump(); |
||||
} |
||||
function pump() { |
||||
if (pumping) return; |
||||
if (queue.length === 0) return; |
||||
pumping = true; |
||||
var step = function () { |
||||
if (queue.length === 0) { |
||||
pumping = false; |
||||
return; |
||||
} |
||||
var call = queue.shift(); |
||||
BRIDGE.sendCall(call).then(function (res) { |
||||
if (res) { |
||||
if (res.ok) { |
||||
console.log("[dsh-memory] call ok", call.tool, res.durationMs); |
||||
} else { |
||||
console.warn("[dsh-memory] call failed", call.tool, res.error); |
||||
} |
||||
} else { |
||||
console.warn("[dsh-memory] call failed", call.tool); |
||||
} |
||||
}).catch(function (err) { |
||||
console.warn("[dsh-memory] call throw", call.tool, String(err)); |
||||
}).then(step); |
||||
}; |
||||
step(); |
||||
} |
||||
|
||||
function flush() { |
||||
timer = null; |
||||
var nodes = Array.from(pending); |
||||
pending.clear(); |
||||
var i; |
||||
for (i = 0; i < nodes.length; i++) handleNode(nodes[i]); |
||||
} |
||||
|
||||
function schedule() { |
||||
if (timer) clearTimeout(timer); |
||||
timer = setTimeout(flush, DEBOUNCE_MS); |
||||
} |
||||
|
||||
var obs = new MutationObserver(function (muts) { |
||||
var i; |
||||
var j; |
||||
for (i = 0; i < muts.length; i++) { |
||||
var m = muts[i]; |
||||
if (m.type === "childList") { |
||||
for (j = 0; j < m.addedNodes.length; j++) { |
||||
var n = m.addedNodes[j]; |
||||
if (n) { |
||||
if (n.nodeType === 1) pending.add(n); |
||||
} |
||||
} |
||||
} else if (m.type === "characterData") { |
||||
if (m.target) { |
||||
if (m.target.parentNode) pending.add(m.target.parentNode); |
||||
} |
||||
} |
||||
} |
||||
if (pending.size > 0) schedule(); |
||||
}); |
||||
|
||||
BRIDGE.hasToken().then(function (has) { |
||||
if (!has) { |
||||
console.warn("[dsh-memory] no bridge token, idle"); |
||||
return; |
||||
} |
||||
obs.observe(document.documentElement, { |
||||
childList: true, |
||||
subtree: true, |
||||
characterData: true |
||||
}); |
||||
console.log("[dsh-memory] content.js active"); |
||||
}); |
||||
})(); |
||||
Loading…
Reference in new issue