gpt4 book ai didi

javascript - executeScript 未定义或不是 ManifestV3 扩展中的函数

转载 作者:行者123 更新时间:2023-12-05 00:25:32 24 4
gpt4 key购买 nike

为什么 Chrome 的这个功能不起作用?我正在尝试这个例子:
https://developer.chrome.com/docs/extensions/mv3/content_scripts/#programmatic .
我正在为 chrome 开发一个扩展并从 发送一条消息popup.js 致服务人员背景.js 并在 executeScript 中出现错误.
popup.js

chrome.runtime.sendMessage({ from: "newScript"}); 
list .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"
]
}
背景.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;
}
});

最佳答案

executeScript ManifestV3 中的方法已更改,现在位于 chrome.scripting接口(interface):
https://developer.chrome.com/docs/extensions/reference/scripting/
在 manifest.json 中添加这一行:

"permissions": ["scripting"] 
背景.js
chrome.scripting.executeScript({
target: {tabId: id, allFrames: true},
files: ['content_scripts/cscript.js'],
});

关于javascript - executeScript 未定义或不是 ManifestV3 扩展中的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66549560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com