# Listen to message from different tab

**URL:** <https://discourse.mozilla.org/t/listen-to-message-from-different-tab/10176>\
**Category:** Development\
**Created:** [August 8, 2016, 3:11pm UTC](https://discourse.mozilla.org/t/listen-to-message-from-different-tab/10176 "2016-08-08T15:11:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![h4ever](https://avatars.discourse-cdn.com/v4/letter/h/dc4da7/32.png) [@h4ever](https://discourse.mozilla.org/u/h4ever)\
**Post date:** [August 8, 2016, 3:11pm UTC](https://discourse.mozilla.org/t/listen-to-message-from-different-tab/10176/1 "2016-08-08T15:11:02Z")

</div>

I have addon which creates new tab when I click on element. In the new tab I load html page inspector.html which loads page script inspector.js:

```
chrome.runtime.onMessage.addListener(
function (package){
  if (package.command !== "add" )
    return false;
  $("div#content").append(package.innerhtml);     
}
);

```

So when I clicked in the original tab, message has been send:

if (make\_it\_easy.tabOpened)  
browser.runtime.sendMessage(  
{ message:“add”,  
subject: “innerhtml”,  
innerhtml: innnerhtml,  
target: { tab: { id: make\_it\_easy.tabID } }  
// sender:{id:tabs[0].id }  
});

Which sends message with innerHTML. However I cannot intercept the message in the page script in the tab. Where is problem? Is the message sent to all existing tabs or to tabs of the addon? I added breakpoint to the line with `if (package.command !== "add" )` but the code is not paused on the line when I click element. Notice: when I click the element, the new tab exists but is not selected nor active.

---

<div class="post-metadata">

**Author:** ![jorgev](https://avatars.discourse-cdn.com/v4/letter/j/71c47a/32.png) [@jorgev](https://discourse.mozilla.org/u/jorgev)\
**Post date:** [August 12, 2016, 11:33pm UTC](https://discourse.mozilla.org/t/listen-to-message-from-different-tab/10176/2 "2016-08-12T23:33:22Z")

</div>

Messages need to go through the background script, I think.

---

<div class="post-metadata">

**Author:** ![NilkasG](https://avatars.discourse-cdn.com/v4/letter/n/0ea827/32.png) [@NilkasG](https://discourse.mozilla.org/u/NilkasG)\
**Post date:** [August 13, 2016, 3:13am UTC](https://discourse.mozilla.org/t/listen-to-message-from-different-tab/10176/3 "2016-08-13T03:13:28Z")

</div>

If you have access to `chrome.runntime.getBackgroundPage()` then you should also be able to send messages to content tabs (you may have to use the `chrome` object of the background page).

I also encountered a Firefox bug that the `chrome` object of an embedded iframe was less capable than that of it’s `window.top`…

---

<div class="post-metadata">

**Author:** ![h4ever](https://avatars.discourse-cdn.com/v4/letter/h/dc4da7/32.png) [@h4ever](https://discourse.mozilla.org/u/h4ever)\
**Post date:** [August 13, 2016, 3:15pm UTC](https://discourse.mozilla.org/t/listen-to-message-from-different-tab/10176/4 "2016-08-13T15:15:51Z")

</div>

I have managed it some how.

What I do is that I click on a web page element, and I open new tab where I sent the content of the element - innerHTML. Now I found that I got the element listed 4 times instead once only. I’ll need to make some diagram to think about it how this can happen.
