gpt4 book ai didi

javascript - 将数据从页面发送到 Chrome 扩展程序

转载 作者:行者123 更新时间:2023-12-03 00:04:21 26 4
gpt4 key购买 nike

我尝试将一些数据从我的 Web 应用程序发送到我的 chrome 扩展(如 google documentation 中所述),但出现错误:Unchecked runtime.lastError: 无法建立连接。接收端不存在。

我的内容脚本:

chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
if (sender.url == blocklistedWebsite)
return; // don't allow this web page access
if (request.openUrlInEditor)
openUrl(request.openUrlInEditor);
});

这是我的 list :

{
"name": "test-extension",
"version": "0.0.1",
"manifest_version": 2,
"background": {
"scripts": ["src/bg/background.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["http://localhost/*"],
"js": ["src/inject/inject.js"]
}
],
"externally_connectable": {
"ids": ["abcdefghijklmnoabcdefhijklmnoabc"],
"matches": ["http://localhost/*"],
"accepts_tls_channel_id": false
}
}

还有测试页面,我正在尝试发送数据:

<body>
<button onclick="processData()">Send data to extension</button>
</body>
<script>
function processData() {
/* ... */
// The ID of the extension we want to talk to.
var editorExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";

// Make a simple request:
chrome.runtime.sendMessage(
editorExtensionId,
{ openUrlInEditor: 'https://google.com' },
function(response) {
if (!response.success) handleError(url);
}
);
}
</script>

最佳答案

问题出在externally_connectable 配置中。它不适用于localhost。为了在 localhost 上使用它,我在主机文件中添加了以下几行:

127.0.0.1 my.localhost

然后将 list 更改为:

 "externally_connectable": {
"ids": ["*"],
"matches": [
"http://my.localhost/*",
]

关于javascript - 将数据从页面发送到 Chrome 扩展程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55033073/

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