4

While attempting to port the extension from manifest version 1 to version 2, this appeared:

Port error: Could not establish connection. Receiving end does not exist. chromeHidden.Port.dispatchOnDisconnect miscellaneous_bindings:232

This appeared in Console in developer tools. I have no idea where to start fixing this cause i don't know what's causing it to begin with..

what can cause this problem? and is there someway to know exactly what's causing it? Thanks.

Reyno
  • 583
  • 13
  • 28

2 Answers2

10

The most likely cause of failure is the activation of the default Content security policy when "manifest_version": 2 is active. A consequence of the default CSP is that inline JavaScript will not be executed.

<script>chrome.extension.onConnect.addListener(...);</script>

The previous line is an example of inline code. The solution is to place the script in an external JS file:

<script src="script.js"><!--original contents moved to script.js--></script>

Background pages/scripts

When you were using background pages, do not use:

  • "background_page": "background.htm", or
  • "background": {"page": "background.htm"},
    but
  • "background": {"scripts": ["background.js"]}
    where background.js contains the script which was initially placed within the <script> tags at background.htm.

Inline event listeners

Browser action popups, app launchers, option pages, etc. often contain inline event listeners. By the CSP, these are also forbidden.

<button onclick="test();"> does not work. The solution is to add the event in an external JS file using addEventListener. Have a look at the documentation or this answer for an example.

Other

  • JavaScript creation from strings (eval, Function, setTimeout, ...) is forbidden. Rewrite your code to not create code from strings, or use the sandbox manifest option (introduced in Chrome 21). Since Chrome 22, the unsafe-eval CSP policy can be used to lift this restriction.
  • JSONP does not work, because external (JavaScript) resources cannot be loaded in the extension's context. Use an ordinary XMLHttpRequest instead of JSONP (more information + example).
    The only exception is when the resource is fetched over httpsnot http. The CSP can be adjusted to introduce this exception - see documentation:

    "content_security_policy": "script-src 'self' https://example.com; object-src 'self'",
    

Official documentation

The official documentation also provides an excellent explanation on the topic, see "Tutorial: Migrate to Manifest V2".

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678
  • exactly what i did before reading your answer here but i did read the replies you added to other questions which led me to to doing that. my extension works now after removing inline JavaScripts.But the "Port error" still exist.i'll mark this as an answer cause this is exactly what made my extension work after porting it from manifest v1 to v2.i'll review my code more to see if i missed any more inlines.I now have popup.html **and** popup.js containing the inline javascripts (including events like onclick). Included the js inside the html file. Thanks Rob. i'll post any updates if any. – Reyno Aug 13 '12 at 09:32
  • More than a "too soon" update lol... i really though i did this already, but in my manifest i was doing **"background": {"page": "background.htm"}** when i now switched to **"background": {"scripts":["background.js"]}** all errors in console are gone! again, thanks so much. +1 – Reyno Aug 13 '12 at 09:40
  • You're welcome. Did you mean [this answer](http://stackoverflow.com/a/11922770/938089)? – Rob W Aug 13 '12 at 14:51
  • not really 1 question lol.. a few of them. but yes this is one of the recent ones. Thanks again Rob. i really appreciate it. – Reyno Aug 14 '12 at 10:47
0

For me solution was changing:

<script type="text/javascript" src="bgScript.js"></script>

to:

<script src="bgScript.js"></script>

Maybe it's also help others!

WooCaSh
  • 5,180
  • 5
  • 36
  • 54
  • 3
    The existence of `type="text/javascript"` does not matter at all. It's more likely that you were experiencing incomplete reload issues (caching), which disappeared after a proper reload (coincidentally after applying the change as stated in your answer). – Rob W Apr 03 '13 at 22:19