来自外部网址的chrome.runtime.connectNative

时间:2017-08-14 03:46:24

标签: javascript google-chrome google-chrome-extension

我的Google Chrome扩展程序包含以下两个文件...

的manifest.json

{
    "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cuCqtkMt8hFrBGWxDdf9CQIDAQAB",
    "name": "Native Messaging Example",
    "version": "1.0",
    "manifest_version": 2,
    "description": "Send a message to a native application.",
    "app": {
        "launch": {
            "local_path": "index.html"
        }
    },
    "icons": {
        "128": "icon-128.png"
    },
    "permissions": [
        "nativeMessaging"
    ],
    "externally_connectable": {
        "matches": ["*://*.chrome-extension.com/*"]
    },
    "background": {
        "scripts": ["background.js"]
    }
}

background.js

var sendResponseCallBack;

chrome.runtime.onMessageExternal.addListener(
    function(request, sender, sendResponse) {
        sendResponseCallBack = sendResponse;
        var message = {"comment": '*** ' + request['comment'] + ' ***'};
        var useNative = false;
        if (useNative) {
            connect();
            sendNativeMessage(message);
        }
        else {
            sendResponseCallBack(message);
        }
    }
);
function connect() {
    var hostName = "com.google.chrome.example.echo";
    port = chrome.runtime.connectNative(hostName);
    port.onMessage.addListener(onNativeMessage);
    port.onDisconnect.addListener(onDisconnected);
}
function sendNativeMessage(message) {
    port.postMessage(message);
}
function onNativeMessage(message) {
    port.disconnect();
    sendResponseCallBack(message);
}

我还配置了虚拟主机: chrome-extension.com ,以便从本地服务器访问网址:

http://www.chrome-extension.com/

安装并启用Chrome扩展程序后,如果我访问:

http://www.chrome-extension.com/

和变量useNative = false然后我通过以下内容获得插件的响应:sendResponseCallBack(message);,但是如果useNative = true然后我没有从插件中获得任何响应,我得到:undefined以及应该花费大约5秒的本机操作,因为在0秒内返回undefined响应,所以不会通过。

我还启用了另一个html页面,我通过扩展程序URL访问:

铬扩展://knldjmfmopnpolahpmmgbagdohdnhkik/calc-with-os.html

在该页面中,我添加了包含上述功能的calc-with-os.js文件:connect() sendNativeMessage(message) onNativeMessage(message)以及功能:chrome.runtime.connectNative正常执行原生功能处理所有阶段。

关于如何从外部网址连接到本机进程的任何想法?

[编辑:尝试编号2]

基于以下评论: @wOxxOm 我对代码进行了以下修改,目的是不要将消息快速发送并等待本机进程启动,但是它还没有工作。

还有其他建议吗?

var port = null;
var sendResponseCallBack;

chrome.runtime.onMessageExternal.addListener(
    function(request, sender, sendResponse) {
        sendResponseCallBack = sendResponse;
        connect(request);
    }
);
function connect(request) {
    chrome.runtime.onConnect.addListener(function(p){
        port = p;
        port.onMessage.addListener(onNativeMessage);
        port.onDisconnect.addListener(onDisconnected);
        var message = {"comment": '*** ' + request['comment'] + ' ***'};
        sendNativeMessage(message);
    });
    var hostName = "com.google.chrome.example.echo";
    chrome.runtime.connectNative(hostName);
}
function sendNativeMessage(message) {
    port.postMessage(message);
}
function onNativeMessage(message) {
    port.disconnect();
    sendResponseCallBack(message);
}

0 个答案:

没有答案