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

Make background script of context-menu-copy-link-with-types non-persistent? #501

Merged
merged 6 commits into from
Mar 18, 2023
Merged

Make background script of context-menu-copy-link-with-types non-persistent? #501

merged 6 commits into from
Mar 18, 2023

Conversation

panasenco
Copy link
Contributor

Mozilla documentation recommends changing background scripts to non-persistent to ensure future compatibility with Manifest V3.

I'm pretty sure the example context-menu-copy-link-with-types can be made non-persistent as-is. I tested in Firefox and all the functionality seems to work after switching it to non-persistent.

However, I'm not super familiar with background script persistence, so if there are gotchas I missed, let me know!

Copy link
Member

@Rob--W Rob--W left a comment

Choose a reason for hiding this comment

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

The contextMenus.create call in the background page is going to result in an inresolved error, when it wakes up, because the menu will be registered twice. To avoid logspam, check the error by reading the lastError property.

This can be done by adding a callback with void browser.runtime.lastError; and adding a comment before that explaining why it's needed.

@panasenco
Copy link
Contributor Author

panasenco commented Oct 16, 2022

I finally found the error message you were referring to in the Multiprocess Browser Console (Ctrl+Shift+J -> Multiprocess):
image

I also realized that menu-demo already has an example of the creation callback.

I'll force-push an update with the changes you proposed.

I also realized that event pages (non-persistent background scripts) are not supported on current stable FF (105), so I added the "strict_min_version" setting and set it 106.0. Haven't tested yet on FF 106 myself yet though...

…stent. Now clearing errors that could result from background.js context menu creation happening more than once.
@Rob--W
Copy link
Member

Rob--W commented Oct 16, 2022

I finally found the error message you were referring to in the Multiprocess Browser Console (Ctrl+Shift+J -> Multiprocess): image

Didn't you see this error in the console of the background script? about:debugging -> This Firefox -> Inspect button at the extension.

I also realized that event pages (non-persistent background scripts) are not supported on current stable FF (105), so I added the "strict_min_version" setting and set it 106.0. Haven't tested yet on FF 106 myself yet though...

Even if event pages are not supported, Firefox 105 should still be able to load the extension (and just print a message in the console), right?

The functionality is still the same, an event page is a background page that may be suspended. An event page that never suspends is equivalent to a background page.

@panasenco
Copy link
Contributor Author

Didn't you see this error in the console of the background script? about:debugging -> This Firefox -> Inspect button at the extension.

Nope, didn't see it there at all. It might be related to bug 1410932.

Even if event pages are not supported, Firefox 105 should still be able to load the extension (and just print a message in the console), right?

The functionality is still the same, an event page is a background page that may be suspended. An event page that never suspends is equivalent to a background page.

Yup, it works on 105, no problem.

context-menu-copy-link-with-types/background.js Outdated Show resolved Hide resolved
@@ -3,12 +3,18 @@
"name": "Context menu: Copy link with types",
"description": "Add a context menu option to links to copy the link to the clipboard, as plain text and as a link in rich HTML.",
"version": "1.0",
"browser_specific_settings": {
"gecko": {
"strict_min_version": "106.0"
Copy link
Member

Choose a reason for hiding this comment

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

Remove this addition since it's not needed.

@@ -1,8 +1,12 @@
// Callback clears runtime.lastError to prevent logspam errors from the menu
Copy link
Member

Choose a reason for hiding this comment

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

Please change it to:
"Callback reads runtime.lastError to prevent an unchecked error from being logged when the extension attempt to register the already-registered menu again. Menu registrations in event pages persist across extension restarts."

@github-actions github-actions bot added the idle Issues and pull requests with no activity for three months. label Jan 22, 2023
@github-actions github-actions bot removed the idle Issues and pull requests with no activity for three months. label Mar 5, 2023
context-menu-copy-link-with-types/background.js Outdated Show resolved Hide resolved
Co-authored-by: Rob Wu <rob@robwu.nl>
@rebloor rebloor merged commit a2da599 into mdn:main Mar 18, 2023
@rebloor
Copy link
Collaborator

rebloor commented Mar 18, 2023

@panasenco many thanks for kicking this off

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

Successfully merging this pull request may close these issues.

3 participants