-
Notifications
You must be signed in to change notification settings - Fork 278
fix: 修复background脚本中初始化右键菜单导致移动端无法使用 #190
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
base: main
Are you sure you want to change the base?
fix: 修复background脚本中初始化右键菜单导致移动端无法使用 #190
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
描述
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: