You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
4.7 KiB
188 lines
4.7 KiB
"use strict"; |
|
|
|
(function () { |
|
var SEL = globalThis.DSH_SELECTORS; |
|
var BRIDGE = globalThis.DSH_BRIDGE; |
|
var extractCalls = null; |
|
if (typeof globalThis.extractCalls === "function") { |
|
extractCalls = globalThis.extractCalls; |
|
} |
|
var UI = globalThis.DSHMEM || null; |
|
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]); |
|
if (UI) UI.setPhase('executing'); |
|
} |
|
pump(); |
|
} |
|
function executeOne(call) { |
|
return BRIDGE.sendCall(call).then(function (res) { |
|
var e = res && !res.ok && res.error; |
|
var needConfirm = e && e.code === "E_NEEDS_CONFIRM" && e.extra && e.extra.confirmToken; |
|
if (needConfirm) { |
|
console.log("[dsh-memory] needs confirm, retrying", call.tool); |
|
return BRIDGE.sendCall({ tool: call.tool, args: call.args, confirmToken: e.extra.confirmToken }); |
|
} |
|
return res; |
|
}); |
|
} |
|
function pump() { |
|
if (pumping) return; |
|
if (queue.length === 0) return; |
|
pumping = true; |
|
var step = function () { |
|
if (queue.length === 0) { |
|
pumping = false; |
|
if (UI) UI.setPhase('idle'); |
|
return; |
|
} |
|
var call = queue.shift(); |
|
executeOne(call).then(function (res) { |
|
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)); |
|
}).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.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'); } |
|
}); |
|
}); |
|
})();
|
|
|