Skip to content
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

[webextension] Add support for background.scripts and background.page #8906

Conversation

kuroppe1819
Copy link

↪️ Pull Request

🚨 Test instructions

Add this to your Firefox Add-ons manifest:

"background": {
  "scripts": ["background.js"]
}
"background": {
  "page": index.html
}

✔️ PR Todo

  • Added/updated unit tests for this change
  • Filled out test instructions (In case there aren't any unit tests)
  • Included links to related issues/PRs

},
);
}
if (hot) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are now two separate blocks of HMR handling code. This is going to cause issues with HMR: Chrome requires different handling of v2 and v3 to do HMR properly. I'm not sure which version is needed for Firefox's version of Manifest V3. Could you try to investigate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way to fix the double-handling of hot would be to keep the isMV2 check but copy the background.scripts code into the else block as well. But you'd still need to investigate if this HMR code actually works in Firefox.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review! I have pushed the commit with the fix. 1fbbe8b

I investigated whether HMR works in Firefox and having trouble getting HMR to work in Firefox.

In Manifest V3, the script-src directives may only have 'self', 'none' and 'wasm-unsafe-eval'.

Overwrite at the following value in the dist/manifest.json file after the build, It will work HMR when saving the files specified in the background.scripts.

  "content_security_policy": {
    "extension_pages": "script-src 'self';"
  },

But, occur the following errors when saving the files specified in the content scripts

Screen Shot 2023-05-10 at 2 39 55

It's possible that the reason is the stricter CSP constraints in Manifest V3.

In Manifest V3, all CSP sources that refer to external or non-static content are forbidden. The only permitted values are 'none', 'self', and 'wasm-unsafe-eval'. In Manifest V2, a source for a script directive is considered secure if it meets these criteria:
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/content_security_policy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense: Chrome and Firefox both treat MV3 the same with regards to the Content Security Policy. Instead of splitting the HMR logic into two blocks for Chrome and Firefox when isMV2 is false, you'll need to use the Chrome version in both cases, but modify it such that if it's running in Firefox, it also checks for background.scripts.

@kuroppe1819 kuroppe1819 force-pushed the fix/manifest-v3-background-options branch from 9d44c28 to 1fbbe8b Compare May 9, 2023 16:34
context: 'service-worker',
sourceType:
program.background.type == 'module' ? 'module' : 'script',
if (program.browser_specific_settings?.gecko) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Using browser_specific_settings.gecko to switch between Firefox and Chrome. Firefox add-on need an ID in their manifest.json when submitted to AMO.

All Manifest V3 extensions need an add-on ID in their manifest.json when submitted to AMO.
https://extensionworkshop.com/documentation/develop/extensions-and-the-add-on-id/

But, it's not necessary in debugging. I want to know some useful flags.

@sailerinteractive
Copy link

I learned that this should be fixed in Parcel >= 2.10.0 However, when im trying to build a Firefox V3 extension with Parcel 2.10.2 im still getting the "Missing property service_worker" error as before with older versions of parcel. Do i somehow need to specify for which browser i would like to bulld in order to activate this fix?

@mischnic
Copy link
Member

@sailerinteractive

Do i somehow need to specify for which browser i would like to bulld in order to activate this fix?

No

The current logic says that (among others) these two versions are possible

{
	"background": {
		"service_worker": "xyz"
	}
	...
}
{
	"background": {
		"scripts": ["xyz"]
	}
	...
}

What does your manifest look like?

The corresponding JSON schema sections

background: {
oneOf: [
{
type: 'object',
properties: {
service_worker: string,
type: {
type: 'string',
enum: ['classic', 'module'],
},
},
additionalProperties: false,
required: ['service_worker'],
},
mv2Background,
], // for Firefox
},

const mv2Background = {
type: 'object',
properties: {
scripts: arrStr,
page: string,
persistent: boolean,
},
additionalProperties: false,
};

@sailerinteractive
Copy link

sailerinteractive commented Nov 14, 2023

Here is a shortened version of the manifest i'm using:

{
    "manifest_version": 3,
    "name": "My Extension",
    "short_name": "MyE",
    "description": "",
    "homepage_url": "https://example.com",
    "version": "10.2",
    "background": {
      "scripts": ["javascript/sw.js"],
      "persistent": false,
      "type": "module"
    },
    "content_scripts": [
    	...
    ],
    "action": {
    	...
    },
    "icons": {
      	...
    },
    "permissions": [
    	...
    ],
    "web_accessible_resources": [ 
    	...
    ],
    "host_permissions": [
    	...
    ]
}

@mischnic
Copy link
Member

The schema didn't allow scripts together with type: #9385
You could remove type as a workaround

@sailerinteractive
Copy link

Thanks! I can confirm the extension builds when removing the type property. I'm not sure yet what implications that might have when running the extension in Firefox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants