25

Why is this function from Chrome not working? I'm trying this example: https://developer.chrome.com/docs/extensions/mv3/content_scripts/#programmatic.

I'm developing an extension for chrome and sending a message from popup.js to service worker background.js and get error in executeScript.

popup.js

chrome.runtime.sendMessage({ from: "newScript"}); 

manifest.json

{
    "manifest_version": 3,
    "name": "TennisBet",
    "version": "1.0",
    "description": "Extrension for bet on tennis.",
    "action": {
        "default_icon": {
            "256": "images/tennis256.png",
            "128": "images/tennis128.png",
            "64": "images/tennis64.png",
            "32": "images/tennis32.png",
            "24": "images/tennis24.png",
            "16": "images/tennis16.png"
        },
        "default_popup": "popup/popup.html"
    },
    "background": {
        "service_worker": "background-wrapper.js"
    },
    "host_permissions": ["*://*/*"],
    "permissions": [
        "tabs",
        "alarms",
        "activeTab",
        "declarativeContent",
        "storage"
    ]
}

background.js

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    switch(request.from){
        case "error":
            console.log(request.message);
            break;
        case "checkTabs":
            tabsWorker();      
            break;
        case "newScript":
            chrome.scripting.executeScript({ // Error in event handler: TypeError: Cannot read property 'executeScript' of undefined
                file: "content_scripts/cscript.js"
            });
            break;
        default:
            console.log("Message listener status active");
            break;
    }
});
patridge
  • 26,385
  • 18
  • 89
  • 135
Cyber User
  • 863
  • 1
  • 7
  • 13
  • The link you gave says `chrome.scripting` not `chrome.tabs`. – wOxxOm Mar 09 '21 at 15:28
  • @wOxxOm The thing is, I've tried and "chrome.scripting.executeScript" and get error: "Error in event handler: TypeError: Unable to read executeScript property of undefined value" Now i editing this post – Cyber User Mar 09 '21 at 15:40
  • 5
    Well, it's a separate API so it needs `"scripting"` in `"permissions"` in manifest.json.You can report the bug in this documentation, there's a link at the bottom. – wOxxOm Mar 09 '21 at 15:47

1 Answers1

58

The executeScript method in ManifestV3 has changed and is now in chrome.scripting API: https://developer.chrome.com/docs/extensions/reference/scripting/

Add this line in manifest.json:

"permissions": ["scripting"] 

background.js

chrome.scripting.executeScript({
    target: {tabId: id, allFrames: true},
    files: ['content_scripts/cscript.js'],
});
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Cyber User
  • 863
  • 1
  • 7
  • 13
  • In case it didn't jump out at you, notice that `tabId` is now part of the passed object and not an argument. Also, the passed object is [`ScriptInjection`](https://developer.chrome.com/docs/extensions/reference/scripting/#type-ScriptInjection) rather than [`InjectDetails`](https://developer.chrome.com/docs/extensions/reference/extensionTypes/#type-InjectDetails). I haven't compared the objects to see if there are other differences, but it's worth keeping your eyes open. – StormyKnight Nov 04 '22 at 16:08
  • And also, if you're using manifestV3, you need to set `"background": { "service_worker": "workers/background.js", <--- your background worker file path "type": "module" },` on `manifest.json` like that. – Seiwon Park Jun 12 '23 at 09:53