与外部网址的Chrome扩展程序进行通信

时间:2017-08-13 11:04:18

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

我有以下用于Chrome扩展程序的javascript代码,该扩展程序在扩展程序的网址中运行良好,我的意思是:

铬扩展://knldjmfmopnpolahpmmgbagdohdnhkik/main.html

这是代码

var port = null;

$(function(){
    $('.button_generate').click(function(){
        $('.progressbar').removeClass('hidden');
        connect();
        port.postMessage({"comment": "Overland Park"});
    });
    $('.button_save_greeting').click(function(){
        chrome.runtime.sendMessage('knldjmfmopnpolahpmmgbagdohdnhkik', {greeting: "hello"}, {}, function(response) {
            console.log(response);
        });
    });
});
function connect() {
    var hostName = "com.google.chrome.example.echo";
    port = chrome.runtime.connectNative(hostName);
    port.onMessage.addListener(onNativeMessage);
    port.onDisconnect.addListener(onDisconnected);
}
function onNativeMessage(message) {
    $('.greeting').val(message['greeting']);
    port.disconnect();
    $('.greeting').prop('disabled', false);
    $('.progressbar').addClass('hidden');
}
function onDisconnected() {
    port = null;
}

我的问题是,如果我可以从外部网址向此Chrome扩展程序发送消息,我的意思是:

http://localhost/myproject/test.html

在插件的清单上我有:

"externally_connectable": {
    "matches": ["*://localhost/*"]
}   

但是沟通并没有通过。

我的另一个问题是如何配置Chrome扩展程序以回复从 test.html 收到的消息?

0 个答案:

没有答案
相关问题