gpt4 book ai didi

javascript - chrome.tabs.sendRequest : "Port error: Could not establish connection. Receiving end does not exist."

转载 作者:行者123 更新时间:2023-11-30 18:24:13 26 4
gpt4 key购买 nike

我正在开发一个 Google Chrome 扩展程序,它产生了一个我无法修复的错误。

我的 manifest.json 看起来像这样:

{
"name": "my extension",
"version": "1.0",
"background_page": "background.html",
"permissions": [
"tabs",
"<all_urls>"
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"],
"all_frames": true
}
]
}

background.html 尝试与 content.js 对话:

<script>
chrome.tabs.onUpdated.addListener
(
function(tabId, changeInfo)
{
chrome.tabs.sendRequest(tabId, {action : 'getMyValue'}, function(output) {
console.log(output);
});
}
);
</script>

最后,content.js:

chrome.extension.onRequest.addListener(function(request, sender, callback)
{
if (request.action == 'getMyValue')
{
callback('test');
}
});

开发者工具控制台打印:“端口错误:无法建立连接。接收端不存在。”在第 232 行的“miscellaneous_bindings”中。

有什么想法吗?

最佳答案

chrome.tabs.onUpdated在选项卡更新时运行。还包括开发工具、chrome(扩展)页面等。要消除错误,您必须过滤无法访问的 URL。

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo) {
// Example: allow http:, https: and file:
if (changeInfo.status === 'complete' &&
/^(https?|file):/.test(changeInfo.url)) {
chrome.tabs.sendRequest(tabId, {action: 'getMyValue'}, function(output) {
console.log(output);
});
}
});

关于javascript - chrome.tabs.sendRequest : "Port error: Could not establish connection. Receiving end does not exist.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11329828/

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