Add explicit Chrome debug sweep
This commit is contained in:
parent
216d4e6fea
commit
fa3b9c7778
9 changed files with 320 additions and 8 deletions
|
|
@ -5,6 +5,7 @@
|
|||
"description": "Publishes Chrome tab metadata to the local Loki CultMesh daemon.",
|
||||
"permissions": [
|
||||
"alarms",
|
||||
"debugger",
|
||||
"storage",
|
||||
"tabs"
|
||||
],
|
||||
|
|
|
|||
|
|
@ -85,6 +85,12 @@ button {
|
|||
cursor: pointer;
|
||||
}
|
||||
|
||||
button + button {
|
||||
margin-top: 8px;
|
||||
border-color: #34516f;
|
||||
background: #386181;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
cursor: wait;
|
||||
opacity: 0.65;
|
||||
|
|
|
|||
|
|
@ -26,11 +26,16 @@
|
|||
<dt>Endpoint</dt>
|
||||
<dd id="endpoint">-</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt>Debug</dt>
|
||||
<dd id="debug">-</dd>
|
||||
</div>
|
||||
</dl>
|
||||
|
||||
<p id="error" class="error" hidden></p>
|
||||
|
||||
<button id="sync" type="button">Sync now</button>
|
||||
<button id="debug-sweep" type="button">Debug sweep</button>
|
||||
</main>
|
||||
<script src="popup.js" type="module"></script>
|
||||
</body>
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@ const stateEl = document.querySelector("#state");
|
|||
const tabsEl = document.querySelector("#tabs");
|
||||
const lastSyncEl = document.querySelector("#last-sync");
|
||||
const endpointEl = document.querySelector("#endpoint");
|
||||
const debugEl = document.querySelector("#debug");
|
||||
const errorEl = document.querySelector("#error");
|
||||
const syncButton = document.querySelector("#sync");
|
||||
const debugSweepButton = document.querySelector("#debug-sweep");
|
||||
|
||||
syncButton.addEventListener("click", async () => {
|
||||
syncButton.disabled = true;
|
||||
|
|
@ -12,6 +14,13 @@ syncButton.addEventListener("click", async () => {
|
|||
syncButton.disabled = false;
|
||||
});
|
||||
|
||||
debugSweepButton.addEventListener("click", async () => {
|
||||
debugSweepButton.disabled = true;
|
||||
await chrome.runtime.sendMessage({ type: "loki.debug-sweep" });
|
||||
await render();
|
||||
debugSweepButton.disabled = false;
|
||||
});
|
||||
|
||||
await render();
|
||||
|
||||
async function render() {
|
||||
|
|
@ -23,6 +32,7 @@ async function render() {
|
|||
stateEl.textContent = "waiting";
|
||||
stateEl.className = "state";
|
||||
tabsEl.textContent = "-";
|
||||
debugEl.textContent = "-";
|
||||
lastSyncEl.textContent = "-";
|
||||
errorEl.hidden = true;
|
||||
return;
|
||||
|
|
@ -31,6 +41,7 @@ async function render() {
|
|||
stateEl.textContent = status.ok ? "connected" : "offline";
|
||||
stateEl.className = `state ${status.ok ? "ok" : "bad"}`;
|
||||
tabsEl.textContent = String(status.tabCount ?? 0);
|
||||
debugEl.textContent = formatDebugProbe(status.debugProbe);
|
||||
lastSyncEl.textContent = status.lastSyncAt ? new Date(status.lastSyncAt).toLocaleTimeString() : "-";
|
||||
|
||||
if (status.error) {
|
||||
|
|
@ -40,3 +51,9 @@ async function render() {
|
|||
errorEl.hidden = true;
|
||||
}
|
||||
}
|
||||
|
||||
function formatDebugProbe(debugProbe) {
|
||||
if (!debugProbe) return "not run";
|
||||
const summary = debugProbe.summary ?? {};
|
||||
return `${summary.attachedTabCount ?? 0}/${summary.attemptedTabCount ?? 0} attached`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,6 +31,10 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
|
|||
syncTabs("manual").then(sendResponse);
|
||||
return true;
|
||||
}
|
||||
if (message?.type === "loki.debug-sweep") {
|
||||
debugSweep().then(sendResponse);
|
||||
return true;
|
||||
}
|
||||
if (message?.type === "loki.get-status") {
|
||||
getStatus().then(sendResponse);
|
||||
return true;
|
||||
|
|
@ -42,7 +46,7 @@ chrome.runtime.onMessage.addListener((message, _sender, sendResponse) => {
|
|||
return false;
|
||||
});
|
||||
|
||||
async function syncTabs(reason) {
|
||||
async function syncTabs(reason, extra = {}) {
|
||||
const statusStartedAt = new Date().toISOString();
|
||||
try {
|
||||
const endpoint = await getEndpoint();
|
||||
|
|
@ -62,7 +66,9 @@ async function syncTabs(reason) {
|
|||
pageContent: "not-read",
|
||||
cookies: "not-read",
|
||||
tabMetadata: "title-url-favicon-window-state",
|
||||
debugProbe: extra.debugProbe ? "runtime-frame-performance-security-metadata" : "not-run",
|
||||
},
|
||||
...extra,
|
||||
};
|
||||
|
||||
const response = await fetch(endpoint, {
|
||||
|
|
@ -82,6 +88,7 @@ async function syncTabs(reason) {
|
|||
reason,
|
||||
lastSyncAt: new Date().toISOString(),
|
||||
tabCount: tabs.length,
|
||||
debugProbe: responseBody.debugProbe ?? null,
|
||||
activeSource: responseBody.activeSource ?? "unknown",
|
||||
error: null,
|
||||
};
|
||||
|
|
@ -95,6 +102,7 @@ async function syncTabs(reason) {
|
|||
reason,
|
||||
lastSyncAt: new Date().toISOString(),
|
||||
tabCount: 0,
|
||||
debugProbe: null,
|
||||
activeSource: "none",
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
};
|
||||
|
|
@ -103,6 +111,142 @@ async function syncTabs(reason) {
|
|||
}
|
||||
}
|
||||
|
||||
async function debugSweep() {
|
||||
const startedAt = new Date().toISOString();
|
||||
const tabs = await chrome.tabs.query({});
|
||||
const targets = await chromeDebuggerGetTargets();
|
||||
const probe = {
|
||||
schema: "gamecult.loki.chrome_debug_probe.v0",
|
||||
startedAt,
|
||||
completedAt: null,
|
||||
protocolVersion: "1.3",
|
||||
targetCount: targets.length,
|
||||
targets: targets.map(projectDebugTarget),
|
||||
tabs: [],
|
||||
summary: {
|
||||
attemptedTabCount: 0,
|
||||
attachedTabCount: 0,
|
||||
failedTabCount: 0,
|
||||
commandCount: 0,
|
||||
commandFailureCount: 0,
|
||||
},
|
||||
redaction: {
|
||||
pageContent: "not-read",
|
||||
cookies: "not-read",
|
||||
responseBodies: "not-read",
|
||||
domTree: "root-node-only",
|
||||
runtimeEvaluation: "location-title-readyState-visibility-performance-navigator",
|
||||
},
|
||||
};
|
||||
|
||||
for (const tab of tabs) {
|
||||
if (!Number.isInteger(tab.id)) continue;
|
||||
probe.summary.attemptedTabCount += 1;
|
||||
const tabProbe = await probeTab(tab);
|
||||
probe.tabs.push(tabProbe);
|
||||
if (tabProbe.attached) probe.summary.attachedTabCount += 1;
|
||||
else probe.summary.failedTabCount += 1;
|
||||
probe.summary.commandCount += tabProbe.commands.length;
|
||||
probe.summary.commandFailureCount += tabProbe.commands.filter((command) => !command.ok).length;
|
||||
}
|
||||
|
||||
probe.completedAt = new Date().toISOString();
|
||||
return syncTabs("debug-sweep", { debugProbe: probe });
|
||||
}
|
||||
|
||||
async function probeTab(tab) {
|
||||
const target = { tabId: tab.id };
|
||||
const tabProbe = {
|
||||
tabId: tab.id,
|
||||
windowId: tab.windowId,
|
||||
title: tab.title ?? null,
|
||||
url: tab.url ?? null,
|
||||
attached: false,
|
||||
attachError: null,
|
||||
commands: [],
|
||||
frameTree: null,
|
||||
runtime: null,
|
||||
performanceMetrics: null,
|
||||
securityState: null,
|
||||
domRoot: null,
|
||||
};
|
||||
|
||||
try {
|
||||
await chromeDebuggerAttach(target, "1.3");
|
||||
tabProbe.attached = true;
|
||||
|
||||
await recordCommand(tabProbe, target, "Page.enable");
|
||||
await recordCommand(tabProbe, target, "Runtime.enable");
|
||||
await recordCommand(tabProbe, target, "Log.enable");
|
||||
await recordCommand(tabProbe, target, "Performance.enable");
|
||||
await recordCommand(tabProbe, target, "Security.enable");
|
||||
await recordCommand(tabProbe, target, "DOM.enable");
|
||||
|
||||
tabProbe.frameTree = await recordCommand(tabProbe, target, "Page.getFrameTree");
|
||||
tabProbe.runtime = await recordCommand(tabProbe, target, "Runtime.evaluate", {
|
||||
expression: `({
|
||||
href: location.href,
|
||||
title: document.title,
|
||||
readyState: document.readyState,
|
||||
visibilityState: document.visibilityState,
|
||||
referrer: document.referrer,
|
||||
origin: location.origin,
|
||||
navigation: performance.getEntriesByType("navigation").map((entry) => entry.toJSON?.() ?? {}),
|
||||
screen: { width: screen.width, height: screen.height, colorDepth: screen.colorDepth },
|
||||
navigator: {
|
||||
userAgent: navigator.userAgent,
|
||||
language: navigator.language,
|
||||
languages: navigator.languages,
|
||||
hardwareConcurrency: navigator.hardwareConcurrency,
|
||||
deviceMemory: navigator.deviceMemory ?? null,
|
||||
platform: navigator.platform,
|
||||
cookieEnabled: navigator.cookieEnabled
|
||||
}
|
||||
})`,
|
||||
returnByValue: true,
|
||||
awaitPromise: false,
|
||||
});
|
||||
tabProbe.performanceMetrics = await recordCommand(tabProbe, target, "Performance.getMetrics");
|
||||
tabProbe.securityState = await recordCommand(tabProbe, target, "Security.getSecurityState");
|
||||
tabProbe.domRoot = await recordCommand(tabProbe, target, "DOM.getDocument", { depth: 0, pierce: false });
|
||||
} catch (error) {
|
||||
tabProbe.attachError = error instanceof Error ? error.message : String(error);
|
||||
} finally {
|
||||
if (tabProbe.attached) {
|
||||
await chromeDebuggerDetach(target).catch(() => {});
|
||||
}
|
||||
}
|
||||
|
||||
return tabProbe;
|
||||
}
|
||||
|
||||
async function recordCommand(tabProbe, target, method, params = undefined) {
|
||||
try {
|
||||
const result = await chromeDebuggerSendCommand(target, method, params);
|
||||
tabProbe.commands.push({ method, ok: true });
|
||||
return result ?? {};
|
||||
} catch (error) {
|
||||
tabProbe.commands.push({
|
||||
method,
|
||||
ok: false,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function projectDebugTarget(target) {
|
||||
return {
|
||||
id: target.id ?? null,
|
||||
tabId: target.tabId ?? null,
|
||||
type: target.type ?? null,
|
||||
title: target.title ?? null,
|
||||
url: target.url ?? null,
|
||||
attached: Boolean(target.attached),
|
||||
extensionId: target.extensionId ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
function projectTab(tab) {
|
||||
return {
|
||||
id: tab.id,
|
||||
|
|
@ -128,6 +272,46 @@ async function getStatus() {
|
|||
};
|
||||
}
|
||||
|
||||
function chromeDebuggerGetTargets() {
|
||||
return new Promise((resolve, reject) => {
|
||||
chrome.debugger.getTargets((targets) => {
|
||||
const error = chrome.runtime.lastError;
|
||||
if (error) reject(new Error(error.message));
|
||||
else resolve(targets ?? []);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function chromeDebuggerAttach(target, protocolVersion) {
|
||||
return new Promise((resolve, reject) => {
|
||||
chrome.debugger.attach(target, protocolVersion, () => {
|
||||
const error = chrome.runtime.lastError;
|
||||
if (error) reject(new Error(error.message));
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function chromeDebuggerSendCommand(target, method, params) {
|
||||
return new Promise((resolve, reject) => {
|
||||
chrome.debugger.sendCommand(target, method, params ?? {}, (result) => {
|
||||
const error = chrome.runtime.lastError;
|
||||
if (error) reject(new Error(error.message));
|
||||
else resolve(result);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function chromeDebuggerDetach(target) {
|
||||
return new Promise((resolve, reject) => {
|
||||
chrome.debugger.detach(target, () => {
|
||||
const error = chrome.runtime.lastError;
|
||||
if (error) reject(new Error(error.message));
|
||||
else resolve();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function getEndpoint() {
|
||||
const values = await chrome.storage.local.get(CONFIG_KEY);
|
||||
return values[CONFIG_KEY]?.endpoint ?? DEFAULT_ENDPOINT;
|
||||
|
|
|
|||
Reference in a new issue