Merge "Lazily connect to background port from content script"
diff --git a/extension/src/content/index.js b/extension/src/content/index.js
index cf9ae18..09d32ef 100644
--- a/extension/src/content/index.js
+++ b/extension/src/content/index.js
@@ -10,8 +10,15 @@
 // Port to communicate with background js.
 // One content script runs per iframe / tabs so there may be multiple ports
 // per page.
-// TODO(bprosnitz) Change this to lazily connect.
-var backgroundPort = chrome.runtime.connect();
+var _backgroundPort;
+function getBackgroundPort() {
+  // Lazily connect to background port.
+  if (!_backgroundPort) {
+    _backgroundPort = chrome.runtime.connect();
+    _backgroundPort.onMessage.addListener(backgroundPageMessageForwarder);
+  }
+  return _backgroundPort;
+}
 
 // We generate and send different instanceIds to the background page than those
 // coming from the web app. This prevents the web app from intentionally
@@ -69,7 +76,7 @@
   }
 
   try {
-    backgroundPort.postMessage({
+    getBackgroundPort().postMessage({
       type: this.event,
       body: body
     });
@@ -80,7 +87,7 @@
 });
 
 // Forward any messages from the background page to the webApp.
-backgroundPort.onMessage.addListener(function(msg) {
+function backgroundPageMessageForwarder(msg) {
   debug('content script received message of type', msg.type,
     'from background script:', msg.body);
 
@@ -100,6 +107,6 @@
   }
 
   pageEventProxy.send(msg.type, msg);
-});
+}
 
 debug('content script loaded');