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

Firefox扩展弹窗与background.js通信失败问题求助

Firefox扩展弹窗无法与background.js建立消息连接问题

发送消息时收到错误:

Error sending message to background script: Error: Could not establish connection. Receiving end does not exist.

内容脚本代码

const pasteBtn = document.getElementById('pasteBtn');

pasteBtn.addEventListener('click', () => {
    // Send a message to the background script to request the address object
    browser.runtime.sendMessage({ action: "getAddressObject" })
        .then(() => console.log('grabbing addressObj'))
        .catch(error => {
            console.error('Error sending message to background script:', error);
    });
});

background.js脚本代码

该脚本在弹窗打开时能正常打印日志,但无法响应消息:

console.log('background.js is running') // for debug purposes to check script loaded

// Listen for messages from content scripts
browser.runtime.onMessage.addListener((message, sender, sendResponse) => {
    if (message.action === "getAddressObject") {
        console.log('successfully connected to background.js')
        // Send message to the active tab
        browser.tabs.query({ active: true, currentWindow: true })
            .then(tabs => {
                browser.tabs.sendMessage(tabs[0].id, { action: "sendAddressObject", addressObj });
            })
            .catch(error => {
                console.error('Error querying tabs:', error);
            });
    }
});

已尝试将消息发送改为异步函数,检查过manifest.json配置,确认扩展启动时能正常加载脚本。


内容的提问来源于stack exchange,提问作者Yeahx1000

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 10:55:03