Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

javascript - executeScript is undefined or not a function in a ManifestV3 extension

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

I develop extension for chrome and send 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;
    }
});
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'],
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...