Chrome无法在Mac上启动本机消息传递主机

时间:2020-06-29 19:00:38

标签: google-chrome google-chrome-extension chromium chrome-native-messaging

仅作为序言,我对所有这些都是新手,所以请让我知道我是否缺少任何基本知识,或者我是否正在以正确的方式进行尝试。

我正在尝试创建一个Chrome扩展程序,该扩展程序可以读取Gmail中电子邮件的发件人,然后启动python脚本(fmconnect.py)并将该发件人的名称发送到该脚本。我可以使用gmail.js检索发件人的姓名,但是当我尝试将其发送到python脚本时,我一直收到错误消息:

Unchecked runtime.lastError: Failed to start native messaging host.

以下是所有相关文件:

background.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    console.log(request);
    sendResponse({message: "Message received"});
    if (request.length > 0) {
        const hostName = "com.google.chrome.example.echo";
        var port = chrome.runtime.connectNative(hostName);
        if (port) {
            port.postMessage({text: request});
        }
    }
    return true;
});

manifest.json的相关部分

    "permissions": [
        "https://*/*",
        "http://localhost/",
        "nativeMessaging",
        "background",
        "tabs"
    ]

fmconnect.py

def read_thread_func():
    text_length_bytes = sys.stdin.read(4)
    sys.stdout.write(text_length_bytes) 

def Main():
    read_thread_func()
    sys.exit(0)

if __name__ == '__main__':
 Main()

com.google.chrome.example.echo.json

{
  "name": "com.google.chrome.example.echo",
  "description": "Chrome Native Messaging API Host",
  "path": "HOST_PATH",
  "type": "stdio",
  "allowed_origins": [
    "chrome-extension://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx/"
  ]
}

install_host.sh

set -e
DIR="$( cd "$( dirname "$0" )" && pwd )"
if [ "$(uname -s)" = "Darwin" ]; then
  if [ "$(whoami)" = "root" ]; then
    TARGET_DIR="/Library/Google/Chrome/NativeMessagingHosts"
  else
    TARGET_DIR="$HOME/Library/Application Support/Google/Chrome/NativeMessagingHosts"
  fi
else
  if [ "$(whoami)" = "root" ]; then
    TARGET_DIR="/etc/opt/chrome/native-messaging-hosts"
  else
    TARGET_DIR="$HOME/.config/google-chrome/NativeMessagingHosts"
  fi
fi
HOST_NAME=com.google.chrome.example.echo
# Create directory to store native messaging host.
mkdir -p "$TARGET_DIR"
# Copy native messaging host manifest.
cp "$DIR/$HOST_NAME.json" "$TARGET_DIR"
# Update host path in the manifest.
HOST_PATH=$DIR/fmconnect.py
ESCAPED_HOST_PATH=${HOST_PATH////\\/}
sed -i -e "s/HOST_PATH/$ESCAPED_HOST_PATH/" "$TARGET_DIR/$HOST_NAME.json"
# Set permissions for the manifest so that all users can read it.
chmod o+r "$TARGET_DIR/$HOST_NAME.json"
echo "Native messaging host $HOST_NAME has been installed."

我知道在本机消息传递主机documentation上,它说要检查我是否具有足够的权限来启动文件。我该怎么做?

0 个答案:

没有答案