executeScript是未定义的,或者不是ManifestV3扩展中的函数 [英] executeScript is undefined or not a function in a ManifestV3 extension
问题描述
为什么Chrome的此功能无法正常工作?我再说一遍 https://developer.chrome.com/docs/extensions/mv3/content_scripts/#programmatic
Why this function from chrome is not working? I'am repeat example https://developer.chrome.com/docs/extensions/mv3/content_scripts/#programmatic
我为chrome开发了扩展程序,并将消息从"popup.js"发送到服务工作者"background.js",并在"executeScript"中出错.
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.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
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;
}
});
推荐答案
ManifestV3中的 executeScript
方法已更改,现在位于 chrome.scripting
API中: https://developer.chrome.com/docs/extensions/reference/scripting/
The executeScript
method in ManifestV3 has changed and is now in chrome.scripting
API:
https://developer.chrome.com/docs/extensions/reference/scripting/
在manifest.json中添加以下行:
Add this line in manifest.json:
"permissions": ["scripting"]
background.js
background.js
chrome.scripting.executeScript({
target: {tabId: id, allFrames: true},
files: ['content_scripts/cscript.js'],
});
这篇关于executeScript是未定义的,或者不是ManifestV3扩展中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!