You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Chrome扩展原生通信报错‘Native host has exited’排查求助

Troubleshooting "Failed to connect: Native host has exited" in Chrome Native Messaging (Ubuntu/Chrome 84)

Hey there, I’ve dug into your setup and the error you’re seeing—let’s walk through the most likely reasons this is happening, plus actionable fixes to get your native messaging working:

Common Causes & Fixes

1. Missing Python 2 Shebang Line (Critical!)

Chrome doesn’t automatically know to run your script with Python 2 unless you tell it explicitly. Add this as the very first line of your native-messaging-example-host file:

#!/usr/bin/env python2

Without this, Chrome tries to execute the script as a binary, which fails immediately—this is a super common culprit on Linux systems.

2. Missing Tkinter Dependency for Python 2

Your host script relies on Tkinter to create a GUI window, but Ubuntu doesn’t include Python 2’s Tkinter by default. When the script starts up and can’t find the module, it crashes before connecting to Chrome.

Fix options:

  • Install the Tkinter package for Python 2:
    sudo apt-get install python-tk
    
  • Or, if you don’t need the GUI, modify your Main() function to skip the Tkinter check and run in headless mode directly:
    def Main():
        # Bypass Tkinter check entirely
        send_message('"Running in headless mode"')
        read_thread_func(None)
        sys.exit(0)
    

3. Invalid Allowed Origin in JSON Config

Chrome is strict about the format of allowed_origins—your current entry has a trailing slash, which might be causing a mismatch. Update your com.google.chrome.example.echo.json file:

"allowed_origins": [ "chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik" ]

(Remove the trailing / at the end of the extension ID URL.)

4. Script Execution Errors (Check Manually)

Run your host script directly from the terminal to see if it throws any errors:

/home/moon/BrowserExtensions/nativeMessage/host/native-messaging-example-host

If you see tracebacks (like missing modules or syntax errors), that’s exactly why Chrome can’t connect—fix those first before testing the extension.

5. Chrome Debug Logs for Detailed Errors

Enable Chrome’s debug logging to get more context about why the host is exiting:

google-chrome --enable-logging --v=1

After reproducing the error, check the log file at ~/.config/google-chrome/chrome_debug.log—it will often show specific errors like "Failed to execute native host" with a reason (e.g., permission denied, invalid shebang, missing interpreter).

6. Encoding Issues in Python 2

Python 2 uses ASCII as the default encoding, which can cause problems when sending UTF-8 messages to Chrome. Add these lines at the top of your host script (right after the shebang) to force UTF-8 output:

import codecs
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
sys.stdin = codecs.getreader('utf-8')(sys.stdin)

Quick Recap of Your Setup Checks

  • Confirm your host file has executable permissions (you already set rwxrwxrwx, which is fine)
  • Double-check the path in your JSON config points to the exact location of native-messaging-example-host
  • Your manifest.json looks correct (manifest_version 2, nativeMessaging permission, valid extension ID in allowed_origins once fixed)

Start with the shebang line and manual script execution—those are the easiest fixes to verify first. Let me know if any of these steps resolve the issue!

内容的提问来源于stack exchange,提问作者moon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 07:02:41