2

I'm trying to communicate from a web page to a packaged app. The idea is to have the web page read a number from a serial device. Because I want to access the serial device, I need a packaged app and can't use an extension. This is pretty similar to Keep Chrome Packaged App running in background? and it seems that Chrome documentation says this is possible.

How can I execute the chrome.runtime.sendMessage from a regular web page? When I do so, I get *Uncaught TypeError: Cannot read property 'sendMessage' of undefined. My simple function is:

function doFunction(){
    chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
      function(response) {
        if (!response.success)
          handleError(url);
      });
}

My packaged app loads and can access the serial ports. But my suspicion is the manifest isn't "enabling" the chrome.runtime of the regular webpage. Manifest.json:

{
  "name": "Hello World!",
  "description": "My first Chrome App.",
  "version": "0.1",
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
  "icons": { "16": "calculator-16.png", "128": "calculator-128.png" },
  "permissions": [
    "serial",
    "*://localhost/*"
  ],
  "externally_connectable": {
  "matches": [
      "*://localhost/*"]
}
}

Maybe it's the ://localhost/ which I'm using for testing. But Chrome does not complain.

Any ideas out there? Thanks in advance.

Community
  • 1
  • 1
Crivens
  • 333
  • 3
  • 13
  • 1
    Err, `// For tests.` is not valid JSON, you cannot have comments inside this file. Try taking that out. – Xan May 08 '14 at 09:33
  • Thanks for the idea. I took it out and no change. Chrome loads the packaged app and runs it OK. Note that it's not packed. Unfortunately, my regular web page (from file://localhost/) still has chrome.runtime as undefined. – Crivens May 08 '14 at 11:01
  • 6
    From the docs: 'The URL pattern must contain at least a second-level domain - that is, hostname patterns like `"*"`, `"*.com"`, `"*.co.uk"`, and `"*.appspot.com"` are prohibited.' Try defining a second-level alias for 127.0.0.1 in your hosts file and use that. – Xan May 08 '14 at 11:13

2 Answers2

12

Xan's comment did the trick.

While Chrome did not complain about *://localhost/*, it did not work. Chrome did complain about other combinations such as file://localhost/.

I added foo.com to host file and then served up my web page through a web server, and it worked! I can communicate from my web page to my packaged app.

Note that browsing to file://www.foo.com/hostpage.html did not work. But browing to http://www.foo.com:3000/hostpage.html did. (I'm using Rails, hence the 3000 port).

Morale of the story: When testing locally, you need to add an entry with a bogus second level domain to your host file.

Here's my manifest.json:

{
  "name": "RFID Tag Reader",
  "description": "Reads RFID Tags connected via USB reader",
  "version": "0.0.0.1",
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
  "icons": {
    "16": "rfid-16.png",
    "128": "rfid-128.png"
  },
  "permissions": [
    "serial",
    "*://www.foo.com/*",
    "*://arealsite.net/*"
  ],
  "externally_connectable": {
    "matches": [
      "*://www.foo.com/*",
      "*://arealsite.net/*"
    ]
  }
}
Crivens
  • 333
  • 3
  • 13
  • 1
    You could've asked me to convert my comment to an answer.. But oh well. My own fault for answering in the comments. – Xan May 10 '14 at 19:03
  • 1
    Sorry. I'm not too stack overflow savvy. If you want to convert it to an answer and then I accept it, that would be just fine with me. – Crivens May 10 '14 at 20:56
  • I did what was suggested but had no luck. How did your manifest ended up looking like? @Crivens – Dema Oct 27 '14 at 18:51
  • I tried localhost in externally_connectable, and it actually works – aaron Sep 05 '19 at 03:37
  • 1
    For those who are new to the host file, just add this entry to `/etc/hosts` on a mac: `127.0.0.1 your-test-domain.com`. If it does not work right away, try clearing your cache. It might be browser related. – ypicard Nov 13 '19 at 19:45
  • 2
    adding `"*://localhost/*"` as a "matches" in "externally_connectable" worked for me too (Chrome 86.0.4240.111) – mark Oct 29 '20 at 00:40
3

Adding "*://localhost/*" to externally_connectable worked for me.