!Next steps1) Approve the UAC prompt. 2) Wait for the PowerShell window to print "Press Enter to close" and press Enter. 3) Fully close and reopen Brave. 4) Click Verify Registration.
`;
}
function renderBlockedByNativeIssue(out, title) {
out.innerHTML = `
i${escapeHtml(title)}Blocked until this PC registers the native host for the current extension ID. Use the setup card above.
`;
}
async function getPackagedHostPaths() {
try {
const resp = await fetch(chrome.runtime.getURL("host/com.rcjav.host.json"));
if (!resp.ok) return {};
const manifest = await resp.json();
const bat = manifest.path || "";
const hostDir = bat.replace(/[\\/][^\\/]+$/, "");
return {
hostBat: bat,
hostDir,
registerBat: hostDir ? hostDir + "\\register-host.bat" : "",
installPs1: hostDir ? hostDir + "\\install-host.ps1" : "",
};
} catch {
return {};
}
}
async function renderNativeMessagingFailure(response) {
const card = document.getElementById("native-repair-card");
const out = document.getElementById("native-repair-results");
if (!card || !out) return;
card.style.display = "";
const title = document.getElementById("native-repair-title");
if (title) title.textContent = "Register host on this PC";
const error = response?.error || "no response";
const kind = response?.error_kind || (/forbidden/i.test(error) ? "forbidden" : "unknown");
const extensionId = response?.extension_id || chrome.runtime.id;
const paths = await getPackagedHostPaths();
const installCommand = paths.installPs1
? `pwsh -ExecutionPolicy Bypass -File "${paths.installPs1}"`
: `pwsh -ExecutionPolicy Bypass -File ".\\host\\install-host.ps1"`;
const registerCommand = paths.registerBat ? `"${paths.registerBat}"` : ".\\host\\register-host.bat";
const hostDir = paths.hostDir || "";
const hostFolderUrl = hostDir ? "file:///" + hostDir.replace(/\\/g, "/").replace(/^([A-Za-z]:)/, "$1") : "";
let cause = "This extension cannot launch the native messaging host yet.";
let fix = "Run register-host.bat once on this PC, fully restart Brave, then verify registration.";
if (kind === "forbidden") {
cause = "Brave found the native host but the extension ID is not in its allowlist on this PC.";
fix = "Run register-host.bat to refresh the manifest from allowed-extension-ids.json.";
} else if (kind === "not_found") {
cause = "Brave could not find a registered native messaging host for com.rcjav.host on this PC.";
fix = "Run register-host.bat from the extension host folder.";
} else if (kind === "disconnected") {
cause = "The native host started and then disconnected or crashed.";
fix = "After registration is fixed, run Runtime diagnostics again to check Python, rc-jav, and rclone.";
} else if (kind === "timeout") {
cause = "The native host did not respond before the timeout.";
fix = "Restart Brave and check whether a scan or rclone command is stuck.";
}
const openFolderBtn = hostFolderUrl
? ``
: "";
out.innerHTML = `
!Setup requiredNative host registration must be fixed before cache, runtime, and host checks can run.
!Likely cause${escapeHtml(cause)}
iHost message${escapeHtml(error)}
→Fix on this PC${escapeHtml(fix)}
1Run register-host${escapeHtml(registerCommand)}
${escapeHtml(`Double-click ${registerCommand}\nor run the PowerShell alternative:\n${installCommand}\n\nThe script reads the extension ID from allowed-extension-ids.json — no paste step.`)}
${openFolderBtn}
2Restart BraveClose every Brave window/process, reopen Brave, then reload the extension.
3Verify
`;
for (const btn of out.querySelectorAll("button[data-copy]")) {
btn.addEventListener("click", async () => {
await navigator.clipboard.writeText(btn.dataset.copy || "");
btn.textContent = "Copied";
setTimeout(() => { btn.textContent = btn.dataset.copyLabel || "Copy"; }, 1200);
});
}
for (const btn of out.querySelectorAll("button[data-open-folder]")) {
btn.addEventListener("click", async () => {
const url = btn.dataset.openFolder;
const folderPath = btn.dataset.folderPath || "";
try {
// file:// URLs require "Allow access to file URLs" toggled on for the
// extension. If Brave silently blocks it (no tab opens), fall back to
// clipboard so the user can paste into File Explorer (Win+E).
await chrome.tabs.create({ url });
btn.textContent = "Opening…";
setTimeout(() => { btn.textContent = "Open Host Folder"; }, 1500);
} catch (_) {
try { await navigator.clipboard.writeText(folderPath); } catch {}
btn.textContent = "Blocked — path copied";
setTimeout(() => { btn.textContent = "Open Host Folder"; }, 2500);
}
});
}
for (const btn of out.querySelectorAll("button[data-verify-registration]")) {
btn.addEventListener("click", runHostStatus);
}
}