Porting WebExtension from Chrome to Firefox?

前端 未结 1 1797
难免孤独
难免孤独 2021-01-27 01:35

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

相关标签:
1条回答
  • 2021-01-27 02:21

    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.
    0 讨论(0)
提交回复
热议问题