integrating single and long-lived messaging [duplicate]

有些话、适合烂在心里 提交于 2020-01-06 19:57:37

问题


I need to have two type communications, single messaging for retrieving initial information and long-lived messaging. Both messaging communicate with an external web page as follow. The returned response in the web page always is undefined!!! Where is the problem?


manifest.json

{
  "manifest_version": 2,
  "name": "external-long-lived",
  "description": "message test",
  "version": "1.0",
  "browser_action": {
    "default_icon": "icon.png"
  },
  "background": {
    "scripts": ["background.js"],
    "persistent": true
  },
  "permissions": [
    "tabs",
    "http://*/*",
    "management"
  ],
  "offline_enabled": true,
  "externally_connectable": {
     "matches": ["http://localhost:8081/myWeb.html"],
     "accept_tls_channel_id":true            
  }
}

background.js

//single messaging part
chrome.runtime.onMessageExternal.addListener(function(request, sender, sendResponse){
    if(navigator.appVersion.indexOf("Win"!=-1)) {myOS = "Windows";}
    if(navigator.appVersion.indexOf("Mac"!=-1)) {myOS = "MacOS";}
    if(navigator.appVersion.indexOf("X11"!=-1)) {myOS = "UNIX";}
    if(navigator.appVersion.indexOf("Linux"!=-1)) {myOS = "Linux";}     
    chrome.management.get("iacgjcchbmaofapfmlfmdneaaimagjej",function(result){
        if(result) myVersion = "1.0";
    });
    if(request.message == "initialize" && myOS = "Windows" && myVersion == "1.0"){
        sendResponse({version:"1.0" , OS:"Windows"});
    }
    return true;
};


//long-lived messsaging part
console.log("hey back");
chrome.runtime.onConnectExternal.addListener(function(port){

  port.onMessage.addListener(function(message, sender){
  if(message.greeting == "hello"){
    console.log("message from content: " + message.greeting);
    port.postMessage({greeting:"hey"});
  }

  else if(message.greeting == "salam"){
    console.log("message from content: " + message.greeting);
    port.postMessage({greeting:"H R U?"});
  }
  else if(message.greeting == "khobam")
  {
      console.log("message from content: " + message.greeting);
      port.postMessage({greeting:"it's great"});
  }
  else{
    console.log("background did not receive salam");
  }
});
});

content.js which invoke in myWeb.html (external web page)

//single messaging part
var extensionId = 'iacgjcchbmaofapfmlfmdneaaimagjej';
chrome.runtime.sendMessage(extensionId, {message:"initialize"},function(response){
    console.log(response);
    if(!response){alert("not installed");}
    else{
        console.log(response.version);
        console.log(response.OS);
    }
});



//long-lived messaging part   
console.log("hey content");
var port = chrome.runtime.connect("iacgjcchbmaofapfmlfmdneaaimagjej",{name:"external-long-lived"});
port.postMessage({greeting:"hello"});
port.onMessage.addListener(function(message, sender){
    if(message.greeting == "hey"){
        console.log("message from background: " + message.greeting);
        port.postMessage({greeting:"salam"});
    }
    else if(message.greeting == "H R U?"){
        console.log("message from background: " + message.greeting);
        port.postMessage({greeting:"khobam"});
    }
    else if(message.greeting == "it's great"){
        console.log("message from background: " + message.greeting);
    }
    else{
        console.log("content did not receive hello");
        port.postMessage({greeting:"no salam"});
    }
});

thanks in advance

来源:https://stackoverflow.com/questions/34380953/integrating-single-and-long-lived-messaging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!