-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Firefox support? Got POC working #48
Comments
It seems that I got PnP JS Console successfully working in Firefox 108 with my hacky changes, but other functionalities seem to have small issues. |
Hi @jhholm ! |
Small update. Got 6.6.0 popup working with Firefox. I had to change chrome.runtime.executeScript to use world: 'ISOLATED' instead of 'MAIN' as 'MAIN' is not yet supported in Firefox. The change also worked in Edge, though I had to wrap the subsequent fetch calls to chrome.runtime.executeScripts as well, cause in Firefox I would be otherwise running the fetches in the popup context (without auth). The current issue that I'm facing is with any action from devtools that expects to get a message back from an eval. See below:
The documentation is a bit lacking on this subject, but I could find this stackoverflow. I guess I would start by creating a smaller sample and play around how to get the messaging working back to devtools. Would it be possible to use chrome.runtime.executeScript instead? |
Nice work @jhholm!
I will test how the executeScript could work for dev_tools messaging part and will post the results here. |
I guess if executeScript approach works, that would be lovely. This is my first deep dive into extensions and messaging seems to be a bit... interesting. I started playing around with messaging with this sample. The diagram in Step 4 seems to show a similar messaging pattern as Chrome devtools documentation. Though if you look at the source code for the sample the code doesn't follow the diagram at all. I got messaging successfully working in both Chrome and Firefox with following the pattern. See my gist here for a quick sample.
AFAIK Content Script and DevTools have to initiate the connection and background can listen on connections. I do not think it works the other way around. What makes this even more fun, is that Firefox doesn't yet support background: { service_worker: "background.js"}, you'll have to use background: { scripts: ["background.js"] } which is not actually proper MV3. Hooray, two different manifests needed! 🥳 OT/PROTIP: web-ext makes extension development for Firefox a breeze - should work with chrome as well
|
It seems that Chrome might have access to scripting and tabs in devtools.js, if permissions are set in manifest.json. At least console.logging them gives an object after proper permissions. Firefox only shows undefined, but they are reachable in background.js. |
Thanks @jhholm ! I added host_perimssions property to manifest "manifest_version": 3,
"devtools_page": "devtools.html",
"permissions": ["activeTab", "scripting"],
...
"host_permissions": [
"<all_urls>"
],
... ..and got executeScript working also from dev_tools ext: chrome.tabs.query({ currentWindow: true, active: true }, (tabs: any) => {
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
world: 'MAIN',
func: myfunction,
}).then(injectionResults => {
if (injectionResults[0].result) {
console.log(injectionResults[0].result)
}
});
}); Just converting spshooter to use it, will do PR and you can maybe test it in FF? |
interesting, |
If you got time, could you test if these changes in spshooter-with-executescript branch works with FF, it works in Chrome/Edge? And if it does not work, could you try to change reference of chrome.scripting.executeScript to browser.scripting.executeScript, thanks! |
Doesn't seem to work. Weirdly both Chrome and Mozilla documentation say that this should not work, and you would need to run this through background pages/scripts. |
Those docs seems to be old IMO. BTW, when you change permissions, have you also removed and re-added the extension? The permissions do not get applied just to update the manifest. |
If you have created branch in your sp-editor fork for your changes, I would like to test it, just to understand whats going on in FF. I tried loading my current build from file but it could not accept it, so propably some changes in manifest needed. |
Might be that in FF user needs to grant the host permission to work, in MV3. |
That was actually a thing that I noticed quite early on after I couldn't figure out why nothing seemed to work. After allowing the use I got the messaging working. So currently I can get scripting with browser.scripting and chrome.scripting working in background, but not in content scripts or devtools IMO. But I think it might be a good thing to test properly again tomorrow, cause it still might be that I missed something. |
Small update on this. I've had some spare time this year so I've been playing around with a few extension templates that use vite instead of CRA. Firefox is pretty obedient on the rules on how an extension should be built, thus not allowing a lot of the stuff that chromium does. E.g. certain extension APIs can only be run on a background script, not on content scripts or popup. Thus requiring you to use messaging API to make calls between different scripts components. What I've done so far:
So I just noticed that you started working with version 7. How far along is that branch? Should I just mirror my changes using that branch and publish my POC branch for you to take a look at? I guess what I'm asking here, do the changes mentioned above look like something that would be usable? There are a few other vite powered extension templates/projects that I tried, but thus far WXT seemed to work the best. List of other templates that I checked thus far
The main reason I sticked with WXT, is because it makes sense to use MV2 with Firefox and MV3 for Chromium. MV3 support just isn't there for Firefox yet. WXT seems to handle this considerably well. So far I haven't needed to do any browser specific code. |
TLDR;
Ok? |
Wow @jhholm ! Yeah, I I'm almost done with v7, the idea there was to upgrade to latest react/cra etc, also made the debuging simpler and converted all the remaining message api to use scripting api 😅 Yeah, please use the v7 branch as base. Im on vacation atm, but will look forward to see your work when I get a moment. Really appreciate you time and efforts. |
|
but, seems like the scripting api is not available in devtools, bummer. |
This is something that makes cross browser extension development experience a living nightmare. It seems that Firefox 128 (current release) finally provided support for scripting worlds. The popup might actually work in both Firefox and Chrome without any changes. For now it seems that when using MV2, the only required change to get popup working in Firefox was changing the namespace chrome.scripting to browser.scripting. This could also be a MV2 quirk or an issue with bundling, polyfills or the lunar calendar. Using MV3 in any case is IMO out of question for now for Firefox as no localhost connections are allowed, so development experience would be even worse (no HMR). Also background scripts work differently between Chrome and Firefox. |
I was able to get the extension somehow working in Firefox with a few changes
sp-editor/public/chrome/event.js
Line 1 in 8e2b147
That needs to changed to
The bigger refactor was for the definition file loads for monaco-editor. Firefox does not support the following API that is used.
Thus I created a bit hacky workaround: creating a gulp task, that creates dependency.ts file with all the definitions as an JSON array that can be created on build time.
After that I was able to load the extension successfully in Firefox. I think Firefox 109 finally supports officially manifest v3.
I can share my hack, but I guess gulp is a bit deprecated. But I recommend the library definitions would be loaded on buildtime, instead of runtime.
The text was updated successfully, but these errors were encountered: