chrome расширения, содержание. js отправить сообщение в фоновый режим. js в порядке. но когда в фоновом режиме. js отправить сообщение на contentcript. js Не проверено runtime.lastError: Не удалось установить sh соединение. Получающий конец не существует.
chrome расширения, contentcript. js sendMessage to background. js в порядке. но когда в фоновом режиме. js отправить сообщение в contentcript. js Не проверено runtime.lastError: Не удалось установить sh соединение. Получающий конец не существует.
background. js
```
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
console.log('tabs', tabs);
chrome.tabs.sendMessage(tabs[0].id, { greeting: "hello" }, function (response) {
console.log('response', response);
});
});
```
contentcript. js `` `
chrome.runtime.onMessage.addListener(
function (request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
// if (request.greeting == "hello")
sendResponse({ farewell: "goodbye" });
});
манифест. json
```json
{
"name": "udemy translate",
"version": "1.0",
"description": "udemy translate",
"background": {
"persistent": false,
"scripts": [
"./dist/background.js"
]
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*",
"<all_urls>"
],
"js": [
"./dist/contentscript.js"
],
"run_at": "document_end"
}
],
"permissions": [
"http://*/*",
"https://*/*",
"file://*/*",
"tabs",
"contextMenus",
"notifications",
"webRequestBlocking",
"storage",
"activeTab",
"declarativeContent"
],
...
}
```