fix: 修复background脚本中初始化右键菜单导致移动端无法使用#190
Open
SyrupyTasty wants to merge 1 commit intoBistutu:mainfrom
Open
Conversation
Reviewer's GuideGuards all background-script context menu initialization and updates behind a runtime check for browser.contextMenus support to fix Firefox mobile issues, while making minor formatting adjustments to the Microsoft translation helper. Sequence diagram for guarded context menu click handling in background scriptsequenceDiagram
actor User
participant Browser
participant BackgroundScript
participant ContentScript
rect rgb(230,230,230)
note over BackgroundScript: Extension startup
BackgroundScript->>BackgroundScript: isContextMenuSupported = !!browser.contextMenus
alt Context menus supported
BackgroundScript->>Browser: browser.contextMenus.create fluentread_parent
BackgroundScript->>Browser: browser.contextMenus.create TRANSLATE_FULL_PAGE
BackgroundScript->>Browser: browser.contextMenus.create RESTORE_ORIGINAL
Browser->>BackgroundScript: contextMenus.onClicked event registration
else Context menus not supported
BackgroundScript->>BackgroundScript: Skip context menu creation
BackgroundScript->>BackgroundScript: Log 不支持右键菜单
end
end
rect rgb(220,245,220)
note over User,Browser: User invokes full page translation via context menu
User->>Browser: Click context menu TRANSLATE_FULL_PAGE
Browser-->>BackgroundScript: contextMenus.onClicked(info, tab)
BackgroundScript->>BackgroundScript: if !tab.id return
BackgroundScript->>ContentScript: tabs.sendMessage(tab.id, type=contextMenuTranslate, action=fullPage)
ContentScript-->>BackgroundScript: Ack
BackgroundScript->>BackgroundScript: translationStateMap.set(tab.id, true)
BackgroundScript->>BackgroundScript: updateContextMenus(tab.id)
BackgroundScript->>Browser: contextMenus.update TRANSLATE_FULL_PAGE
BackgroundScript->>Browser: contextMenus.update RESTORE_ORIGINAL
end
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- Since
updateContextMenusdirectly referencesbrowser.contextMenus, it would be safer to add an earlyif (!isContextMenuSupported) return;guard inside the function rather than relying solely on callers to checkisContextMenuSupported. - Consider downgrading or removing the
console.log("不支持右键菜单")message (or using a more concise English message) to avoid noisy logs in production environments where context menus are expected to be unsupported.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Since `updateContextMenus` directly references `browser.contextMenus`, it would be safer to add an early `if (!isContextMenuSupported) return;` guard inside the function rather than relying solely on callers to check `isContextMenuSupported`.
- Consider downgrading or removing the `console.log("不支持右键菜单")` message (or using a more concise English message) to avoid noisy logs in production environments where context menus are expected to be unsupported.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
描述
FireFox移动端v0.24后因
browser.contextMenus为空,导致defineBackground内browser.runtime.onMessage无法被执行,造成使用调用browser.runtime.sendMessage时报错Could not establish connection. Receiving end does not exist.。修复
在
entrypoints/background.ts内加入对browser.contextMenus是否为空的检测。Issues
#122 #150
Summary by Sourcery
Guard background script context menu initialization behind a feature check to avoid failures on platforms where context menus are unavailable.
Bug Fixes:
Enhancements: