0

I have developed extensions for my web-application for Chrome and Firefox. Now I would like to check, if a user has already installed the extension. If not, a message with a download link pops up.

In order do have a common code basis, I chose an option that should work with Chrome as well as with Firefox. Unfortunately it only does on Chrome.

My Code:

//page.js

window.addEventListener("message", function(event){
  if(event.data && event.data.direction == "from-content-script") {
    document-getElementById("message").style = "display: none";
  }
});

//content-script.js

window.postMessage({
  direction: "from-content-script",
  message: "Message from the content script"
}, "*");

//manifest.jsons

"content_scripts": [{
   "matches": ["http://localhost:3000/*],
    "js": ["content-script.js"]
}]

So on Chrome it works perfectly, but on Firefox it does not. I wonder if the content script is injected correctly, because if I include an alert for test purpose it does not show up.

I am referring to this: How to check if a Firefox WebExtension is installed or not with page JavaScript? but I cannot get it to work with firefox.

Thanks for your help!

tobiasb96
  • 1
  • 1
  • http:localhost:3000/* is not a valid match pattern. The two forward slashes are missing. – Smile4ever Apr 10 '18 at 07:29
  • Oh you're right, but it is correct in my code, just a copying-mistake. – tobiasb96 Apr 10 '18 at 07:33
  • Possible duplicate of [Detecting programatically whether addon installed on firefox](https://stackoverflow.com/questions/34963137/detecting-programatically-whether-addon-installed-on-firefox) – danwellman Apr 10 '18 at 07:48
  • Checkout https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Sharing_objects_with_page_scripts – Smile4ever Apr 10 '18 at 08:36
  • Thanks, found a solution. Firefox seems to not be able to match localhost - on every other page it works. – tobiasb96 Apr 10 '18 at 09:07

1 Answers1

0

As the docs note:

The path pattern string should not include a port number. Adding a port, as in: "http://localhost:1234/"* causes the match pattern to be ignored.

Don't use

   "matches": ["http://localhost:3000/*"],

but

   "matches": ["http://localhost/*"],
fregante
  • 29,050
  • 14
  • 119
  • 159