how to handle chrome.runtime.sendNativeMessage() in native app

前端 未结 2 2000
广开言路
广开言路 2021-01-03 10:15

I am working on native messaging host. I am able to launch my custom application by using api

var port = chrome.runtime.connectNative(\'com.my_company.my_app         


        
2条回答
  •  清歌不尽
    2021-01-03 10:50

    UPDATE:

    Regarding how to listen for the messages on the native app, they are sent to the stdio (for the time being this is the only available communication channel between Chrome extensions and native apps). Take a look at this sample app featuring a native messaging host implemented in python.


    You listen for messages registering a listener on port's onMessage event.

    Use sendNativeMessag() only if you want a one-time communication (not a persistent port). In that case, do not use chrome.runtime.connectNative(...). Instead, do something like this:

    var msg = {...};
    chrome.runtime.sendNativeMessage("", msg, function(response) {
        if (chrome.runtime.lastError) {
            console.log("ERROR: " + chrome.runtime.lastError.message);
        } else {
            console.log("Messaging host sais: ", response);
        }
    });
    

    The docs' section about Native Messaging is pretty detailed and a great source of information.

提交回复
热议问题