Skip to content

Conversation

@SyrupyTasty
Copy link

@SyrupyTasty SyrupyTasty commented Jan 6, 2026

描述

FireFox移动端v0.24后因browser.contextMenus为空,导致defineBackgroundbrowser.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:

  • Prevent background message handling from breaking on Firefox mobile when browser.contextMenus is undefined by skipping context menu setup and updates in that environment.

Enhancements:

  • Gate context menu update logic and click listeners on a shared isContextMenuSupported flag and perform minor code style cleanups in the background script.

@sourcery-ai
Copy link

sourcery-ai bot commented Jan 6, 2026

Reviewer's Guide

Guards 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 script

sequenceDiagram
    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
Loading

File-Level Changes

Change Details Files
Add a runtime feature check for browser.contextMenus and conditionally initialize and handle context-menu logic to avoid failures on platforms where it is unavailable (e.g., Firefox mobile).
  • Introduce an isContextMenuSupported flag based on the presence of browser.contextMenus within the background main entrypoint.
  • Wrap context menu creation and onClicked listener registration in a conditional block that executes only when context menus are supported.
  • Add an else branch that logs when context menus are not supported instead of attempting setup.
  • Guard calls to updateContextMenus in tab activation and tab update listeners so they only run when context menus are supported.
entrypoints/background.ts
Tidy up the Microsoft background translation helper function to improve consistency.
  • Normalize spacing in the JSON payload for the translation request body.
  • Remove stray trailing whitespace in the translation function implementation.
entrypoints/background.ts

Possibly linked issues

  • #unknown: Issue 报 Firefox 安卓端连接错误,PR 正是通过避免空 contextMenus 修复该错误场景。
  • #unknown: PR通过在background中检查browser.contextMenus修复Firefox安卓端“Receiving end does not exist”错误

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a 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 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.
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.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

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.

1 participant