簡體   English   中英

Chrome 本地消息傳遞 — 為什么我會收到“未找到指定的本地消息傳遞主機”錯誤?

[英]Chrome Native Messaging — Why am I receiving a “Specified native messaging host not found” error?

根據 Chrome Native Messaging 文檔,成功調用 connectNative() 會返回一個端口,您可以使用該端口將消息發布到本機應用程序(Mac 應用程序)。 在我的情況下, nativeConnect() 確實返回了一個有效的端口,但對 onDisconnected() 偵聽器的調用幾乎立即被觸發。 每當監聽器被觸發時,它就會將“lastError”屬性打印到瀏覽器的控制台,這給出:

Specified native messaging host not found.

為什么要這樣做? 生成 msg 的偵聽器如下所示:

function onDisconnected() {
  console.log("Inside onDisconnected(): " + chrome.runtime.lastError.message);
  port = null;
}

文檔底部有一個關於此特定錯誤的完整部分 ( Native Messaging ),並且建議的補救措施說明清單文件的命名、放置或定義 (JSON) 不正確,或者主機應用程序未命名或定位清單說它應該在哪里。 該文檔說 connectNative() 將“在單獨的進程中啟動主機”,但活動監視器沒有提供本地主機應用程序已啟動的證據。

我調用 connectNative() 如下:

chrome.runtime.onMessageExternal.addListener(

  function(request, sender, sendResponse) {
    //var imgdata = JSON.stringify(request.imgdata);
    //process it somehow here

    port = chrome.runtime.connectNative("com.allinlearning.nmhforbrowserextension");

    if (port)
    {
       console.log("connectNative() returned a non-null port");

       port.onMessage.addListener(onNativeMessage);
       port.onDisconnect.addListener(onDisconnected);
    }
});

我的本機主機清單文件根據文檔位於正確的文件夾中,解析為 JSON,看起來像:

{
  "name": "com.allinlearning.nmhforbrowserextension",
  "description": "Manifest for native messaging host for Google browser extension",
  "path": "/Users/mycomputer1/Documents/nmhost.app",
  "type": "stdio",
  "allowed_origins": ["chrome-extension://gldheanjpgopipommeingjlnoiamdfol/"]
}

Chrome 擴展程序也需要一個清單,在我正確設置權限部分之前,我無法從 connectNative() 獲取非空端口,所以我很確定這現在是正確的:

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

更新:

想出如何從 Mac 的終端啟動 Chrome 瀏覽器,並帶有能夠查看更多“詳細”日志記錄的標志。 然后當我運行時,我注意到這個輸出:

[21285:38915:1231/164417:ERROR:native_process_launcher.cc(131)] Can't find manifest for native messaging host com.allinlearning.nmhforbrowserextension

很明顯它找不到主機清單,但為什么?

對於 Google Chrome,清單文件的系統范圍目錄是:

~/Library/Application Support/Google/Chrome/NativeMessagingHosts/

特定於用戶的安裝路徑位於:

~/Library/Application Support/Chromium/NativeMessagingHosts/

(Mac 當前記錄的路徑不正確( patch )。不過install.sh 中的路徑(來自文檔中的示例)是正確的)。

只是想提一下,如果您使用的是不同的 Chrome 發布渠道,例如 Canary,這在開發過程中很常見,您將不得不相應地調整路徑。

~/Library/Application Support/Google/Chrome Canary/NativeMessagingHosts/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM