Javascript 对chrome.runtime.sendMessage()的单个函数调用中包含多条消息;

Javascript 对chrome.runtime.sendMessage()的单个函数调用中包含多条消息;,javascript,google-chrome,google-chrome-extension,message-passing,Javascript,Google Chrome,Google Chrome Extension,Message Passing,我想将多条(具体为2条)消息从我的contentscript.js传递到popup.js 我不需要这个函数的其他参数,我只需要message参数 在我的contentscript.js中,我有以下内容: chrome.runtime.sendMessage(); 这是我的popup.js: chrome.runtime.sendMessage(message1); chrome.runtime.sendMessage(message2); 我想将这两个函数组合成一个函数,并处理如下消息:

我想将多条(具体为2条)消息从我的contentscript.js传递到popup.js

我不需要这个函数的其他参数,我只需要
message
参数

在我的contentscript.js中,我有以下内容:

chrome.runtime.sendMessage();
这是我的popup.js:

chrome.runtime.sendMessage(message1);

chrome.runtime.sendMessage(message2);
我想将这两个函数组合成一个函数,并处理如下消息:

chrome.runtime.onMessage.addListener(function(messsage1){
 //code for handling the message
});

chrome.runtime.onMessage.addListener(function(messsage2){
 //code for handling the message
});

如何才能做到这一点?

我将以JSON对象的形式发送消息,并使用附加属性指定消息的类型。例如:

 chrome.runtime.onMessage.addListener(function(){
   // how to write the parameter for this function, I can't use ',' right?
   // code for handling the message1, message2
});
然后您可以将消息侦听器组合成一个函数:

chrome.runtime.sendMessage({content: "Message1", type: "m1"});

chrome.runtime.sendMessage({content: "Message2", type: "m2"});

当然,这只是一个粗略的示例-您应该根据扩展的要求定制JSON对象的结构,但这是我将使用的模式。

我将以JSON对象的形式发送消息,并使用附加属性指定消息的类型。例如:

 chrome.runtime.onMessage.addListener(function(){
   // how to write the parameter for this function, I can't use ',' right?
   // code for handling the message1, message2
});
然后您可以将消息侦听器组合成一个函数:

chrome.runtime.sendMessage({content: "Message1", type: "m1"});

chrome.runtime.sendMessage({content: "Message2", type: "m2"});

当然,这只是一个粗略的示例-您应该根据扩展的要求定制JSON对象的结构,但这是我将使用的模式。

contentscript.js

chrome.runtime.onMessage.addListener(function(message) {
  if(message.type == "m1") {
    console.log("First message: ", message.content);
  }
  if(message.type == "m2") {
    console.log("Second message: ", message.content);
  }
}
popup.js

 chrome.runtime.sendMessage({
                greeting: "message1"
            })
chrome.runtime.sendMessage({
                greeting: "message2"
            })

API:

contentscript.js

chrome.runtime.onMessage.addListener(function(message) {
  if(message.type == "m1") {
    console.log("First message: ", message.content);
  }
  if(message.type == "m2") {
    console.log("Second message: ", message.content);
  }
}
popup.js

 chrome.runtime.sendMessage({
                greeting: "message1"
            })
chrome.runtime.sendMessage({
                greeting: "message2"
            })

API:

runtime.sendMessage只能用于根据的一次性请求。考虑使用Tab.Snter代替.< /P> 这是扩展页连接到选项卡的代码:

  chrome.runtime.onMessage.addListener(
        function (request, sender, sendResponse) {
        if (request.greeting == "message1") {
        //action 1
        } else if (request.greeting == "message2") {
            //action 2
        }
    });
应在内容脚本中指定此代码:

var port = chrome.tabs.connect(tab.id);
port.postMessage({type: "first", content: "Hi!"});
port.postMessage({type: "second", content: "How are you?"});

runtime.sendMessage只能用于根据的一次性请求。考虑使用Tab.Snter代替.< /P> 这是扩展页连接到选项卡的代码:

  chrome.runtime.onMessage.addListener(
        function (request, sender, sendResponse) {
        if (request.greeting == "message1") {
        //action 1
        } else if (request.greeting == "message2") {
            //action 2
        }
    });
应在内容脚本中指定此代码:

var port = chrome.tabs.connect(tab.id);
port.postMessage({type: "first", content: "Hi!"});
port.postMessage({type: "second", content: "How are you?"});

@nikki听众从未被呼叫过?不,没有被呼叫过。我们可以在popup.js中有两个侦听器吗?@nikki不,一个就足够了。当您从contentscript.js发送消息时,popup.js正在运行?消息实际上是一个变量(数字),我应该使用“”Buddy,我想传递变量,而不仅仅是文本。@nikki侦听器从未调用过?不,它没有被调用。我们可以在popup.js中有两个侦听器吗?@nikki不,一个就足够了。当您从contentscript.js发送消息时,popup.js正在运行?消息实际上是一个变量(数字),我应该使用“”Buddy,我想传递变量,而不仅仅是文本。