gpt4 book ai didi

javascript - 获取 "Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist"但监听器存在

转载 作者:行者123 更新时间:2023-12-05 05:42:19 27 4
gpt4 key购买 nike

所以我尝试从 background.js 脚本向内容脚本发送消息,但我收到此错误 Unchecked runtime.lastError: Could not establish connection。接收端不存在.

背景.js

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, {data: {
message: 'createProfileFrame',
userData: userData
}}, function(res) {
console.log(res);
});
})

popup.js(内容脚本)

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

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log(request);
sendResponse(true);
if (request.data.message == 'createProfileFrame') {
const li = document.createElement('li');
li.style.display = 'block';

const frame = document.createElement('div');
frame.style.backgroundColor = 'white',
frame.style.borderRadius = '20px';
frame.style.width = '90%';
frame.style.height = '3rem';
li.appendChild(frame);

framesContainer.appendChild(li);
}
})

弹出窗口

<!DOCTYPE html>

<html>
<head>
<link rel="stylesheet" href="popup.css">
</head>
<body>
<div class="container">
<ul id="framesContainer"></ul>
</div>
<script src="popup.js"></script>
</body>
</html>

list .json

{
"name": "Extension",
"description": "Test extension",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"permissions": [
"storage",
"activeTab",
"scripting",
"cookies",
"webRequest"
],
"action": {
"default_popup": "popup.html"
},
"host_permissions": ["<all_urls>"]
}

我多次阅读文档,对我来说似乎一切都是正确的,所以我不知道我做错了什么,我将不胜感激任何帮助。

最佳答案

问题是对 sendMessage() 的调用是在内容脚本的监听器加载之前完成的,向调用添加超时解决了这个问题。

关于javascript - 获取 "Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist"但监听器存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72069730/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com