You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firefox扩展中调用launchWebAuthFlow后sendResponse无法向React端返回响应的问题求助

Firefox扩展中调用launchWebAuthFlow后sendResponse无法向React端返回响应的问题求助

大家好,我正在用React开发一款Firefox浏览器扩展,目前遇到了一个卡壳的问题,想请各位帮忙看看:

当我点击扩展中的按钮时,会触发下面的回调,向后台脚本发送消息:

browser.runtime.sendMessage({ type: "auth", url: "url" })
    .then((response) => {
      console.log("Response:", response);
    })
    .catch((error) => {
      console.error("Error:", error);
    });

我的后台脚本确实能收到这条消息,处理OAuth流程的代码如下:

browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
    if (message.type === "auth") {
      browser.identity.launchWebAuthFlow({ url: message.url, interactive:true })
        .then((responseUrl) => {
          console.log("callback!")
          sendResponse({ success: true, url: responseUrl });
        })
        .catch((error) => {
          sendResponse({ success: false, error: error.message });
        });
  
      return true;
    }
  });

OAuth的调用流程是正常的,后台也输出了callback!的日志,说明已经执行到了sendResponse这一步,但奇怪的是,React端完全收不到返回的响应消息。

下面是我的manifest配置文件:

{
  "manifest_version": 2,
  "name": "Open Link Extension",
  "version": "1.0",
  "description": "An extension that opens a link when a button is clicked.",
  "permissions": [
    "tabs",
    "identity",
    "activeTab",
    "webRequest",
    "<all_urls>",
    "cookies"
  ],
  "browser_action": {
    "default_popup": "html/popup.html"
  },
  "background": {
    "scripts": [
      "src/background.js"
    ],
    "persistent": false
  },
  "icons": {
    "48": "icon.png"
  }
}

麻烦各位帮忙分析下可能的问题所在,谢谢大家了!

备注:内容来源于stack exchange,提问作者Axel.A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 08:28:06