首页 > 解决方案 > How to get Chrome Native Messaging example to work with Python script on Mac

问题描述

I am trying to get the Chrome Native Messaging example found here to work. I have had to make some changes to the host script to work with Python 3.

I successfully ran the install_host.sh file which created the file /Library/Application Support/Google/Chrome/NativeMessagingHosts/com.google.chrome.example.echo.json. This file correctly contains the path to the python host file.

Changes to example

I have edited the host file as advised here to fix the issues relating to the type. The send message function now reads

def send_message(message):
    encodedContent = json.dumps(message)
    encodedLength = struct.pack('@I', len(encodedContent))
    encodedMessage = {'length': encodedLength, 'content': encodedContent}
    sys.stdout.buffer.write(encodedMessage['length'])
    sys.stdout.write(encodedMessage['content'])
    sys.stdout.flush()

Problem

I think the issue is connecting the chrome application to the python script. When i run the the chrome extension at chrome-extension://knldjmfmopnpolahpmmgbagdohdnhkik/main.html i get this error

Connecting to native messaging host com.google.chrome.example.echo

Failed to connect: Specified native messaging host not found.

Could anyone please advise as to what step i might be missing in order to get the Chrome web extension to send and receive messages to the python script.

标签: pythonpython-3.xgoogle-chromegoogle-chrome-extensionchrome-native-messaging

解决方案


推荐阅读