# Webextension firefox andoid - content script does not receive messages

**URL:** <https://discourse.mozilla.org/t/webextension-firefox-andoid-content-script-does-not-receive-messages/16194>\
**Category:** Development\
**Created:** [June 9, 2017, 6:57pm UTC](https://discourse.mozilla.org/t/webextension-firefox-andoid-content-script-does-not-receive-messages/16194 "2017-06-09T18:57:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![aliquip](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aliquip](https://discourse.mozilla.org/u/aliquip)\
**Post date:** [June 9, 2017, 6:57pm UTC](https://discourse.mozilla.org/t/webextension-firefox-andoid-content-script-does-not-receive-messages/16194/1 "2017-06-09T18:57:13Z")

</div>

Messages send from a background-script to a content-script are not received in the content-script in ff on android.

**content script**

```
var myPort = browser.runtime.connect({name:"port-from-cs"});
myPort.postMessage({greeting: "hello from content script"});

myPort.onMessage.addListener(function(m) {
  console.log("In content script, received message from background script: "); // never fires
  console.log(m.greeting); // never fires
});

document.body.addEventListener("click", function() {
  myPort.postMessage({greeting: "they clicked the page!"});
});

```

**background script**

```
var portFromCS;

function connected(p) {
  portFromCS = p;
  console.log("post message from background") // ok
  portFromCS.postMessage({greeting: "hi there content script!"}); // never received
  portFromCS.onMessage.addListener(function(m) {
    console.log("In background script, received message from content script")
    console.log(m.greeting); // ok
    console.log(portFromCS); // ok
    console.log(portFromCS.name) // ok
   portFromCS.postMessage({greeting: "hi there FROM BACKROUND script!"}); // never received
  });

```

On firefox on windows, things work, on android, the messages to the content-script are lost in the void 😕

Is this a known bug, and are there any known solutions?

---

<div class="post-metadata">

**Author:** ![aliquip](https://avatars.discourse-cdn.com/v4/letter/a/ac8455/32.png) [@aliquip](https://discourse.mozilla.org/u/aliquip)\
**Post date:** [June 12, 2017, 1:15pm UTC](https://discourse.mozilla.org/t/webextension-firefox-andoid-content-script-does-not-receive-messages/16194/2 "2017-06-12T13:15:06Z")

</div>

After more research, the problem seems to be that in the background-script when a content-script connects by the listener `browser.runtime.onConnect.addListener(connected)`, the property `port.sender.tab` is not set for the `port` argument in the callback function `connected`. (oddly, `port.sender.url` is set)

This only goes wrong for the first tab, when i open a new tab, then when a content script connects the tab object is set and the content-script receives messages from the background script. Pretty troubling that things don’t seem to work well on android, especially seeing web-extension are the only extension allowed nowadays.

As a crude, hopefully temporary, workaround i now check whether the tab is set in my connect callback. If not, i close the tab and open a new one with the same location. It’s an annoying user experience, but at least it makes messaging work.

```
> function connected(port) {
> var id = globalId++
> console.log(port)
> console.log("BS: connect with id: " + id)
> if(!port.sender.tab) {
> // close tab, reopen
> console.log("trouble, port not set!!!!!")
> browser.tabs.query({url: port.sender.url}).then(function(tabs) {
> console.log("AFTER TROUBLE TABS FOUNND");
> if(tabs.length >= 1) {
> var tab = tabs[0]				
> browser.tabs.remove(tab.id)
> browser.tabs.create({url: port.sender.url, active: tab.active})
> }
> })
> } else {
> portsToCSs.set(id, port)
> port.onMessage.addListener(function(m) {
> console.log("BS: received message from content script: " + m)
> for (var i = listeners.length - 1; i >= 0; i--) {
> listeners[i](m, id, "")
> }
> });
> port.onDisconnect.addListener(function(p) { 
> portsToCSs.delete(id)
> })
> }
> }
```

---

<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:** [June 12, 2017, 8:28pm UTC](https://discourse.mozilla.org/t/webextension-firefox-andoid-content-script-does-not-receive-messages/16194/3 "2017-06-12T20:28:49Z")

</div>

Now that you mention it, I actually ran into the missing `port.sender.tab` problem as well. I didn’t know that it only happens for some tabs. Unfortunately, everything that’s got to do with tabs is still quite unstable in Fennec.

---

<div class="post-metadata">

**Author:** ![DaveRo](https://avatars.discourse-cdn.com/v4/letter/d/838e76/32.png) [@DaveRo](https://discourse.mozilla.org/u/DaveRo)\
**Post date:** [November 17, 2017, 3:51pm UTC](https://discourse.mozilla.org/t/webextension-firefox-andoid-content-script-does-not-receive-messages/16194/4 "2017-11-17T15:51:18Z")

</div>

I think I hit this ‘first tab’ problem using browser.tabs.sendMessage in Fx Android 56, but it seems to be OK in 57.  
Was there a bug for this?
