chrome.extension.onConnect.addListener(function(port) {
  port.onMessage.addListener(function(msg) {
    port.postMessage({counter: msg.counter+1});
  });
});

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
  sendResponse({counter: request.counter+1});
});

if (window == top) {
  chrome.extension.onConnect.addListener(function(port) {
    port.onMessage.addListener(function(msg) {
      port.postMessage({counter: msg.counter+1});
    });
  });

  chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
      sendResponse({counter: request.counter+1});
    });
}

Fix the messaging timing extension to only message the toplevel frame.