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
相关产品推荐
相关产品推荐

