Porting WebExtension from Chrome to Firefox?
Asked Answered
K

1

2

I made a working Chrome extension that is not packaged and is just a directory on my computer. I found out that I should be able to port this to Firefox rather easily.

I followed the "Porting a Google Chrome extension" guide on MDN and found that my manifest file is perfect.

I then followed the instructions on how to perform "Temporary Installation in Firefox" of the extension.

However, when I click on any file inside the directory, nothing happens. The extension doesn't load. Any advice? I know the extension works in Chrome fine and loads without error.

manifest.json:

{
  "manifest_version": 2,
  "name": "ER",
  "description": "P",
  "version": "1.0",

  "browser_action": {
    "default_icon": "icon.png",
    "default_popup": "popup.html"
  },

  "background": {
    "scripts": [ "background.js" ]
  },

  "content_scripts": [
    {
      "matches": [ "SiteIwant" ],
      "js": [ "ChromeFormFill.js" ],
      "run_at":  "document_idle" 

    }
  ],

  "permissions": [
    "*://*/*",
    "cookies",
    "activeTab",
    "tabs",
    "https://ajax.googleapis.com/"
  ],
  "externally_connectable": {
    "matches": ["SiteIwant"]
  }
}

ChromeFormFill.js:

// JavaScript source c
console.log("inside content");
console.log(chrome.runtime.id);
document.getElementById("ID").value = chrome.runtime.id.toString();

Background.js

chrome.runtime.onMessage.addListener(
  function (request, sender, sendResponse) {
      if (request.data === "info") {
          console.log("Recieving Info");
          return true;
      }
 });

chrome.tabs.create(
{
    url: 'myUrl' 
    active: true
}, function (tab) {
    chrome.tabs.executeScript(tab.id, { file: 'Run.js', runAt: "document_idle" });
});

Run.js will just alert('hi').

It just won't do anything when I try to load it on Firefox; nothing will happen.

Kersten answered 11/8, 2016 at 18:37 Comment(4)
It won't work in Firefox before 48 without "applications" key in manifest.json.Coble
im fully updated on firefox. @wOxxOmKersten
The manifest declares background.js but it's named Background.js in the subsequent quote. Although Chrome is case-insensitive on Windows but Firefox could be more properly strict about that.Coble
tried it nothing. I have even made a directory that just has a basic manifest and tried porting, nothing loads. No indication that the manifest was read at allKersten
C
5

Issues:

In manifest.json:

externally_connectable is not supported:1

Firefox does not support externally_connectable. You can follow bug 1319168 for more information. There is, currently, no expected time when this will be implemented.

You will need to communicate between the code on your site and the WebExtension using a different method. The way to do so is to inject a content script and communicate between the site's code and the content script. The common ways to do this are CustomEvent() or window.postMessage(). My preference is CustomEvent().

Using window.postMessage() is like yelling your message outside and hoping that either nobody else is listening, or that they know that they should ignore the message. Other people's code that is also using window.postMessage() must have been written to ignore your messages. You have to write your code to ignore any potential messages from other code. If either of those were not done, then your code or the other code can malfunction.

Using CustomEvent() is like talking directly to someone in a room. Other people could be listening, but they need to know about the room's existence in order to do so, and specifically choose to be listening to your conversation. Custom events are only received by code that is listening for the event type which you have specified, which could be any valid identifier you choose. This makes it much less likely that interference will happen by mistake. You can also choose to use multiple different event types to mean different things, or just use one event type and have a defined format for your messages that allows discriminating between any possible types of messages you need.

matches value needs to be valid (assumed to be intentionally redacted):

You have two lines (one with a trailing ,, one without; both syntactically correct):

"matches": ["SiteIwant"]

"SiteIwant" needs to be a valid match pattern. I'm assuming that this was changed away from something valid to obfuscate the site that you are working with. I used:

"matches": [ "*://*.mozilla.org/*" ]

In Background.js:

The lines:

    url: 'myUrl' 
    active: true

need to be:

    url: 'myUrl',
    active: true

[Note the , after 'myUrl'.] In addition, myUrl needs to be a valid URL. I used:

    url: 'http://www.mozilla.org/',

A Firefox 48 bug (now long fixed):

Your line:

chrome.tabs.executeScript(tab.id, { file: 'Run.js', runAt: "document_idle" });

In Firefox 48 this line is executed prior to the tab being available. This is a bug. It is fixed in Firefox Developer Edition and Nightly. You will need one of those to test/continue development.

Issues in ChromeFormFill.js:

Another Firefox 48 bug (now long fixed):

chrome.runtime.id is undefined. This is fixed in Developer Edition and Nightly.

Potential content script issue:

I'm going to assume your HTML has an element with an ID = 'ID'. If not, your document.getElementById("ID") will be null. You don't check to see if the returned value is valid.

Running your example code

Once all those errors were resolved, and running under Firefox Nightly, or Developer Edition, it worked fine. However, you didn't have anything that relied on being externally_connectable, which won't function.


  1. agaggi noticed that I had forgotten to include this issue in the original version of my answer.
Coed answered 12/8, 2016 at 5:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.