I want to build a chrome extension and I met some exception. I cant inject the content.js correctly. chrome.scripting.getRegisteredContentScripts get empty list and the console error:
Unchecked runtime.lastError: Could not establish connection. Receiving end does not exist.
I searched the error and it's brcause it can't find the function in content.js.
anyone can help
here are some main file: manifest.json:
{
"manifest_version": 3,
"name": "Demo",
"version": "0.0.0.1",
"description": "demo ext",
"icons": {
"16": "images/icon-16.png",
"32": "images/icon-32.png",
"48": "images/icon-48.png",
"128": "images/icon-128.png"
},
"action": {
"default_icon": "images/icon-32.png",
"default_popup": "html/popup.html",
"executeScript": [
{
"matches": ["<all_urls>"],
"js": ["scripts/content.js"]
}
]
},
"permissions": [
"declarativeNetRequest",
"declarativeNetRequestFeedback",
"storage",
"webRequest",
"contextMenus",
"nativeMessaging",
"scripting"
],
"host_permissions": [
"*://*.example.com/*"
],
"background": {
"service_worker": "scripts/background.js"
}
}
background.js:
chrome.scripting.getRegisteredContentScripts()
.then(scripts => console.log("registered content scripts", scripts));
chrome.runtime.sendMessage(
{
type: 'fetch',
url: url,
body: reqBody
},
response => {
console.log("response");
console.log(response);
});
content.js
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
console.log(request, sender, sendResponse);
sendResponse('receive msg:'+JSON.stringify("request"));
});
I searched and try a lot. But still error