1

app.js

chrome.runtime.onMessageExternal.addListener(
  function (request, sender, sendResponse) {
      console.log("Message Recived");        
  });

Page.html

chrome.runtime.sendMessage("From WebPage", { openUrlInEditor: "http://localhost:54854/MainPage.html" },
          function (response) {             

          });

manifest.json

"externally_connectable": {
  "matches": ["*://*.example.com/*"]
}

But still I cant send message from Web Page to Chrome APP. Reference: https://developer.chrome.com/extensions/messaging#external

Kindly Help me :(

Waqar Ahmed
  • 1,414
  • 2
  • 15
  • 35

1 Answers1

1

The first argument of chrome.runtime.sendMessage for sending an external message is not an arbitrary ID, but the ID of the extension/app that will receive the message.

For published apps, the ID is fixed at the moment you first submit it to the store.

For unpacked apps, the ID is determined by the "key" field in the manifest, if any, or by the path to the folder. It may be useful to fix the ID for development, so that it does not change from computer to computer.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • Okay I got the web page message to Chrome APP. but can I do it vice versa? that is send message from Chrome APP to Web Page? – Waqar Ahmed Jul 22 '16 at 04:34
  • You can't initiate it from the app. You can only reply to messages with `sendResponse` or open a Port for 2-directional communication with `connect()`, but either has to be initiated by the page. – Xan Jul 22 '16 at 06:05
  • Well I guess I can then Communicate using the this flow : Chrome APP => Chrome Extension => My WebPage? :P – Waqar Ahmed Jul 22 '16 at 06:38
  • That's overkill. Why not open a Port immediately by the page, and then message from app using the port as required? – Xan Jul 22 '16 at 06:40
  • Well I will have to call Web Page function on hardware event from Chrome APP – Waqar Ahmed Jul 22 '16 at 07:07
  • And that's exactly why I'm suggesting Ports. Once established from web page side, you can send a message from app upon event, repeatedly. – Xan Jul 22 '16 at 07:09