Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 来自外部url的chrome.runtime.connectNative_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 来自外部url的chrome.runtime.connectNative

Javascript 来自外部url的chrome.runtime.connectNative,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我有一个Google Chrome扩展名,其中包含以下两个文件 manifest.json { "key": "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDcBHwzDvyBQ6bDppkIs9MP4ksKqCMyXQ/A52JivHZKh4YO/9vJsT3oaYhSpDCE9RPocOEQvwsHsFReW2nUEc6OLLyoCFFxIb7KkLGsmfakkut/fFdNJYh0xOTbSN8YvLWcqph09XAY2Y/f0AL7vfO1cu

我有一个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配置为从本地服务器访问url:

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

变量
useNative=false
然后我通过:
sendResponseCallBack(message)从插件获得响应
,但是如果
useNative=true
那么我没有从插件中得到任何响应,我得到:
undefined
,并且本机操作(大约需要5秒)没有通过,因为
undefined
响应在0秒后返回

我还通过扩展url启用了另一个
html
页面:

铬-extension://knldjmfmopnpolahpmmgbagdohdnhkik/calc-with-os.html

在该页面中,我包含了
calc with os.js
文件,该文件包含以下函数:
connect()
sendNativeMessage(message)
onNativeMessage(message)
和函数:
chrome.runtime.connectNative
在所有阶段正确执行本机进程

有没有关于如何从外部url连接到本机进程的想法

[编辑:尝试第2项]

根据:@wOxxOm的评论,我对代码做了以下修改,目的是不要将消息发送到fast并等待本机进程启动,但它仍然不工作

还有其他建议吗

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);
}

我猜您发送消息太快了:本机进程需要一些时间才能启动。您可以让进程在开始时向您的分机发送消息。然后您可以从extension.so发送消息,那么,是否可以从外部url连接到本机进程?。我要检查一下你在说什么。对不起,我的意思是,通过chrome extensionYeah将外部url上的脚本连接到本机进程,为什么不呢。只要解决时间问题。@wOxxOm,我对上面的问题做了
[编辑:尝试第2步]
,但没有成功。还有其他建议吗?