0

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

Hosein Aqajani
  • 1,553
  • 4
  • 26
  • 46
  • 1
    All chrome.* API callbacks are asynchronous so `version` is assigned only after the main body of the listener function completes. There are many existing duplicate questions, I've linked the first one found. – wOxxOm Dec 20 '15 at 12:28
  • Not the best duplicate question maybe because it doesn't mention that in the single message case you need to add `return true` at the end of the listener. – wOxxOm Dec 20 '15 at 12:34
  • You mean `sendResponse` should set to `true`? – Hosein Aqajani Dec 20 '15 at 12:42
  • [Calling a asynchronous function in a callback](http://stackoverflow.com/a/22696866) – wOxxOm Dec 20 '15 at 12:48
  • Dear @wOxxOm I have done what you said, `sendResponse({verrsion:"1.0",OS:"Windows"});` and then `return true;` however the problem is still exist :( – Hosein Aqajani Dec 20 '15 at 13:15
  • You have a typo `verrsion` also I've examined your code and now I see `function(request, sender, function sendResponse)` instead of `function(request, sender, sendResponse)`. Also did you use `sendResponse` correctly from inside `chrome.management.get` callback? Did you remove the assignments of `sendReponse`? – wOxxOm Dec 20 '15 at 13:24
  • Dear @wOxxOm I have done every correction you notified, but I get undefined for response in the external page! Editions: 1- replace `function sendResponse` by `sendResponse` 2- remove the assignment for `sendResponse` as mentioned in the previous comment 3- remove `sendResponse` in the `chrome.management.get` 4- check for typo errors – Hosein Aqajani Dec 21 '15 at 05:17
  • And what about `return true` at the end of the listener? – wOxxOm Dec 21 '15 at 12:29
  • @wOxxOm Yes of course, `return true` inserted at the end of the listener – Hosein Aqajani Dec 22 '15 at 05:54
  • Can you update your code now that we had this long debate in comments without an actual answer? (do note it's discouraged to do so after someone answers) – Xan Dec 22 '15 at 11:48
  • Dear @Xan , I have updated for you, this is ready for your discussion and maybe your up-voting if you like ;) – Hosein Aqajani Dec 22 '15 at 12:15
  • It would help if you also clearly show what the problem is and where in the code it occurs. – Xan Dec 22 '15 at 12:52
  • Dear @Xan , we have two part messaging: `single` and `long-lived`. In fact I had the long-lived messaging which works properly and when I added the single messaging part, I receive `undefined` for `response` in my `ExternalWebPage` which is the `content.js`. – Hosein Aqajani Dec 23 '15 at 11:01
  • In that case it's a very specific but common mistake. You can't change `myVersion` inside a callback and expect it to be immediately available. Closed as dupe of the canonical question on the topic. – Xan Dec 23 '15 at 11:38

0 Answers0