admin管理员组文章数量:1401672
I made this simple messaging example chrome extension, where a message is supposed to be sent from the background script to the content script. Unfortunately, it seems the content script doesn't receive the message.
Background script:
// background.js
function sendMessage(tabId, hostname) {
console.log("Sending message to tabId: ", tabId)
chrome.tabs.sendMessage(tabId, {hostname: hostname}, (resp) => {console.log("response: ", resp)});
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url) {
console.log(changeInfo.url)
var hostname = new URL(changeInfo.url).hostname;
sendMessage(tabId, hostname)
}
});
Content script:
// content.js
console.log("injected");
function logMessage(message) {
console.log("Message from background: ", message)
}
chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
logMessage(request.hostname)
}
);
Manifest (v3):
// manifest.json
{
"name": "Messaging test",
"description": "",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"permissions": ["tabs"],
"content_scripts": [
{
"matches": [
"<all_urls>",
"https://*/*",
"http://*/*"
],
"js": ["content.js"]
}
]
}
I made sure to reload the extension and use a new tab for testing.
Here is the dev console output from the background script: dev console output of background script
And here is the dev console output from the content script (injected into google): dev console output of content script
So, the content script gets injected, but doesn't receive the message from the background script. I recall this working in manifest v2, so I'm not sure what's wrong. Any ideas?
I made this simple messaging example chrome extension, where a message is supposed to be sent from the background script to the content script. Unfortunately, it seems the content script doesn't receive the message.
Background script:
// background.js
function sendMessage(tabId, hostname) {
console.log("Sending message to tabId: ", tabId)
chrome.tabs.sendMessage(tabId, {hostname: hostname}, (resp) => {console.log("response: ", resp)});
}
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.url) {
console.log(changeInfo.url)
var hostname = new URL(changeInfo.url).hostname;
sendMessage(tabId, hostname)
}
});
Content script:
// content.js
console.log("injected");
function logMessage(message) {
console.log("Message from background: ", message)
}
chrome.runtime.onMessage.addListener(
(request, sender, sendResponse) => {
logMessage(request.hostname)
}
);
Manifest (v3):
// manifest.json
{
"name": "Messaging test",
"description": "",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"permissions": ["tabs"],
"content_scripts": [
{
"matches": [
"<all_urls>",
"https://*/*",
"http://*/*"
],
"js": ["content.js"]
}
]
}
I made sure to reload the extension and use a new tab for testing.
Here is the dev console output from the background script: dev console output of background script
And here is the dev console output from the content script (injected into google.): dev console output of content script
So, the content script gets injected, but doesn't receive the message from the background script. I recall this working in manifest v2, so I'm not sure what's wrong. Any ideas?
Share Improve this question asked Dec 28, 2021 at 18:11 veloxvelox 431 silver badge3 bronze badges 01 Answer
Reset to default 8Content scripts run after DOMContentLoaded by default, but onUpdated
event is triggered when the tab starts loading a URL, so when sendMessage is called there's no content script yet in this tab.
Solution: specify "run_at": "document_start"
so the content script already runs when onUpdated reports changeInfo.url
:
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["content.js"],
"run_at": "document_start"
}],
Another solution would be to reverse the direction of munication and let the content script call sendMessage while the background script would return the data in onMessage, see messaging.
本文标签: javascriptContent script not receiving message from background script (Mv3)Stack Overflow
版权声明:本文标题:javascript - Content script not receiving message from background script (Mv3) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744251796a2597286.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论