1

I am developing an extension for Firefox for Android, but since tabs APIs are not supported on Firefox(Android), I am using the following code. It is working fine on Firefox but when porting it to Firefox Android(52 version), background script messages are not being passed to content script listener.

//Content script code

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: ");
  console.log(m.greeting);
});

// background script

var portFromCS;

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

browser.runtime.onConnect.addListener(connected);

//manifest

{
  "version": "0.1.5",
  "content_scripts": [
    {
      "js": [
        "js/myContentScript.js",
        "js/lib/jquery-1.9.1.min.js"
      ],
      "matches": [
        "<all_urls>"
      ],
      "run_at": "document_start"
    }
  ],
  "description": "xxx",
  "manifest_version": 2,
  "name": "xx",
  "applications": {
      "gecko": {
          "id": "vpt@mozilla.org"
       }
  },
  "permissions": [
    "webRequest",
    "notifications",
    "http://*/",
    "https://*/",
    "storage",
    "webRequestBlocking"
  ],
  "background": {
    "scripts": [
      "js/background.js"
    ]
  },
  "web_accessible_resources": [
    "xxx.js"
  ]
}

content script is passing the message to background script, but background script messages are caught by portFromCS.onMessage listener. Is my approach correct?

Prerna Rana
  • 95
  • 1
  • 9
  • Why are you not using the method to which you were pointed yesterday? Did it not work for you? – Makyen Mar 28 '17 at 23:21
  • The above approach is working fine in Firefox but not on Firefox android, I wanted to keep the code similar on both add-ons. Also, there is plenty of data that needs to be transferred from background script to content script every few milliseconds, keeping all of it in storage might not be possible due to Storage and throttling limits I think. – Prerna Rana Mar 29 '17 at 03:38
  • Are you saying you've tried it & storing in the background script doesn't generate a `storage.onChanged` event? Or, that you can't read the info? You don't have to *keep* the data in `storage.local`. You can remove it as soon as you receive it in the destination. Storage events will be seen in all content scripts. It would be reasonable to have the content script `runtime.sendMessage()` the background script saying that it is listening to a specific key. The background script then knows how to send to a specific tab. – Makyen Mar 29 '17 at 03:48
  • The problem is that without `tabs`, there just isn't another way, that isn't more convoluted. Although, I'd be happy to find out I'm wrong about that. – Makyen Mar 29 '17 at 03:49
  • I am talking about the approach I have mentioned in the question itself, it is working on Firefox but not on Firefox for Android. I will try with the storage.onChanged approach now. Thanks :) – Prerna Rana Mar 29 '17 at 06:50
  • You're right, my bad. Sorry about that. This sounds like a bug in Firefox for Android. I would suggest you [file a bug](https://bugzilla.mozilla.org/enter_bug.cgi) on [Bugzilla](https://bugzilla.mozilla.org/). – Makyen Mar 29 '17 at 07:05
  • Please include a *manifest.json* in the question. It would be helpful to have this be complete. – Makyen Mar 29 '17 at 07:12
  • added the bug on Bugzilla, manifest added as part of question. – Prerna Rana Mar 29 '17 at 10:04

0 Answers0