{
  "markdown": "# Chrome Native Messaging with OpenCode\n\nA Chrome extension that sends messages to a Python native host. The popup can also capture visible text from the current webpage and ask the installed OpenCode CLI to summarize it.\n\n## Architecture\n\n```text\nChrome popup (hello.html + popup.js)\n        |\n        | chrome.runtime.sendMessage\n        v\nService worker (background.js)\n        |\n        | Native Messaging over stdin/stdout\n        v\nPython host (app/main.py)\n        |\n        | opencode run, prompt through stdin\n        v\nOpenCode CLI\n```\n\nThe background service worker owns the native connection. The popup does not call `connectNative()` directly.\n\n## Requirements\n\n- Windows\n- Google Chrome or Chromium-based browser\n- Python available through the `py` launcher\n- OpenCode CLI installed and authenticated\n- An extension ID matching the `allowed_origins` entry in `app/native.json`\n\nVerify Python and OpenCode:\n\n```powershell\npy --version\nopencode run \"Summarize this: Python is a programming language.\"\n```\n\n## Install OpenCode\n\nInstall OpenCode using one of the methods supported by its documentation. For an npm installation:\n\n```powershell\nnpm install -g opencode\n```\n\nConfirm it is available:\n\n```powershell\nopencode --version\n```\n\nAuthenticate and configure a provider/model according to your OpenCode setup. Test it directly before using the extension:\n\n```powershell\nopencode run \"Summarize this: Python is a programming language.\"\n```\n\n## Install the Python Host\n\nThe native host uses only Python standard-library modules. No Python package installation is required.\n\nRun a syntax check:\n\n```powershell\npy -m py_compile .\\app\\main.py\n```\n\n`app/main.bat` starts the host with:\n\n```bat\ncall py E:\\chrome-native-msg\\app\\main.py\n```\n\nUpdate that path if the project is moved.\n\n## Register the Native Host\n\nChrome must know where `com.demo.hello` is registered. The manifest is located at `app/native.json`.\n\nThe current manifest points to:\n\n```text\nE:\\chrome-native-msg\\app\\main.bat\n```\n\nRegister it for the current Windows user with this PowerShell command:\n\n```powershell\n$manifest = (Resolve-Path .\\app\\native.json).Path\nNew-Item -Path 'HKCU:\\Software\\Google\\Chrome\\NativeMessagingHosts\\com.demo.hello' -Force | Out-Null\nNew-ItemProperty -Path 'HKCU:\\Software\\Google\\Chrome\\NativeMessagingHosts\\com.demo.hello' -Name '(Default)' -Value $manifest -PropertyType String -Force | Out-Null\n```\n\nFor Chromium-based browsers that use a different registry location, register the same manifest under the browser's native-messaging host registry key as required by that browser.\n\nThe `allowed_origins` value in `app/native.json` must match the extension ID exactly:\n\n```json\n\"allowed_origins\": [\"chrome-extension://YOUR_EXTENSION_ID/\"]\n```\n\nDo not add a trailing path after the final slash.\n\n## Load the Extension\n\n1. Open `chrome://extensions`.\n2. Enable **Developer mode**.\n3. Click **Load unpacked**.\n4. Select the `extension` folder.\n5. Copy the generated extension ID.\n6. Update `allowed_origins` in `app/native.json` if the ID differs.\n7. Re-register the native host if the manifest path or registry entry changed.\n8. Click **Reload** on the extension.\n\nThe manifest uses these permissions:\n\n- `nativeMessaging`: communicate with the Python host.\n- `activeTab`: temporarily access the active page after the user opens the extension.\n- `scripting`: run the visible-text capture function in the active tab.\n\n## Use the Extension\n\n### Send a manual message\n\n1. Click the extension icon.\n2. Enter a message, such as `ping`, `status`, `time`, or custom text.\n3. Click **Send** or press Enter.\n4. The native response appears in the popup.\n\n### Summarize the current page\n\n1. Open a normal webpage.\n2. Click the extension icon.\n3. Click **Summarize current page**.\n4. The extension captures `document.body.innerText`.\n5. It sends this prompt to the host:\n\n```text\nsummarize this: [webpage content]\n```\n\n6. The host runs:\n\n```text\nopencode run\n```\n\nThe full prompt is sent through OpenCode's standard input, which avoids Windows command-line length limits.\n\n## Test Native Messaging Directly\n\nThis sends a framed `ping` request to the Python host and prints the framed response:\n\n```powershell\n$payload = [Text.Encoding]::UTF8.GetBytes('\"ping\"')\n$prefix = [BitConverter]::GetBytes([uint32]$payload.Length)\n$input = [IO.Path]::GetTempFileName()\n$output = [IO.Path]::GetTempFileName()\n$errorFile = [IO.Path]::GetTempFileName()\n[IO.File]::WriteAllBytes($input, $prefix + $payload)\n$process = Start-Process -FilePath 'py' -ArgumentList '.\\app\\main.py' -RedirectStandardInput $input -RedirectStandardOutput $output -RedirectStandardError $errorFile -PassThru -WindowStyle Hidden\n$process.WaitForExit(3000) | Out-Null\n[IO.File]::ReadAllBytes($output)\nRemove-Item $input, $output, $errorFile -Force\n```\n\nExpected response payload:\n\n```json\n\"pong\"\n```\n\n## Debugging\n\n### Service worker logs\n\n1. Open `chrome://extensions`.\n2. Find the extension.\n3. Click **service worker** or **Inspect views**.\n4. Open the **Console** tab.\n\n### Popup logs\n\nOpen the popup, right-click inside it, and select **Inspect**. Popup logs appear in the popup DevTools, not the service worker console.\n\n### Native host errors\n\nInspect the service worker console and look for errors such as:\n\n- `Specified native messaging host not found`\n- `Access to the specified native messaging host is forbidden`\n- `OpenCode CLI was not found on PATH`\n- `OpenCode timed out after 120 seconds`\n- `The system cannot find the path specified`\n\nCheck the native-host registration:\n\n```powershell\nGet-ItemProperty 'HKCU:\\Software\\Google\\Chrome\\NativeMessagingHosts\\com.demo.hello'\n```\n\nCheck the OpenCode path used by the native host:\n\n```powershell\nGet-Command opencode -All\n```\n\n`main.py` prefers `opencode.cmd`, `opencode.exe`, and `opencode`; it can also fall back to launching `opencode.ps1` through PowerShell.\n\n## Limitations\n\n- The extension captures visible body text only; it does not capture images, hidden content, or browser UI.\n- Chrome restricted pages such as `chrome://` pages, the Chrome Web Store, PDF viewer pages, and some `file://` pages may not allow script injection.\n- Prompt input is limited to 900,000 UTF-8 bytes to stay below Chrome native-messaging limits.\n- Only one native request is allowed at a time. A second simultaneous request receives a busy error instead of being queued.\n- OpenCode processing can take up to 120 seconds.\n- The native host protocol uses a 4-byte length prefix and JSON payloads. `app/main.py` currently uses Python's native `@I` packing, matching the existing Windows-oriented protocol implementation.\n- Keep stdout reserved for native-messaging protocol bytes. Host diagnostics are written to stderr.\n- Webpage text may contain sensitive or untrusted content. Review the captured text before sending it to an external model provider.\n\n## Project Files\n\n- `extension/manifest.json`: Chrome extension configuration and permissions.\n- `extension/hello.html`: popup layout and loading bar.\n- `extension/popup.js`: manual messaging and active-page capture.\n- `extension/background.js`: single native-port relay.\n- `app/main.py`: native-message framing and OpenCode invocation.\n- `app/main.bat`: starts the Python host.\n- `app/native.json`: native host registration data.\n\n## Useful Commands\n\n```powershell\n# Validate extension JavaScript\nnode --check .\\extension\\popup.js\nnode --check .\\extension\\background.js\n\n# Validate Python\npy -m py_compile .\\app\\main.py\n\n# Validate OpenCode independently\nopencode run \"Summarize this: a short test sentence\"\n```\n\n## Useful Links\n\n- [Prompt Injection Defenses](https://www.anthropic.com/research/prompt-injection-defenses)\n- [Get Started with Claude in Chrome](https://support.claude.com/en/articles/12012173-get-started-with-claude-in-chrome)\n- [Chrome Extensions Permissions List](https://developer.chrome.com/docs/extensions/reference/permissions-list)\n- [Build with AI](https://developer.chrome.com/docs/extensions/ai/build-with-ai)\n- [Claude for Chrome](https://claude.com/blog/claude-for-chrome)\n",
  "bytes": 8050,
  "sha": "b2acaf4f1bed4a807ed6a54bfea803bbdfdbb5db31239d0720821b5cef66bd51",
  "repo_slug": "faranahmadk/native-messaging",
  "fonte": "repo",
  "truncated": false,
  "api": "https://agentalog.com/api/listings/okf_faranahmadk_native_messaging_knowledge_i_d6b4e2be/readme"
}