0

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

vacuity
  • 21
  • 2
  • 1) If you used ChatGPT it lied to you, as usual: there's no "executeScript" in manifest.json, it's `content_scripts` and it shouldn't be inside `action`, see the documentation. 2) You can't just send a message from the service worker, you should do it inside an event listener for some event, but depending on what you want to implement you may not need the service worker or content script at all. – wOxxOm Aug 01 '23 at 08:23
  • @wOxxOm 1、acturally I wrote as the doc: "content_scripts": { "matches": [""], "js": ["scripts/content.js"] } and met the error so I ask gpt. 2、I know that. I just paste part of my func – vacuity Aug 01 '23 at 08:56

0 Answers0