我试图使一个V3 Chrome扩展工作,但我不知道如何改变这个V2代码的工作。
manifest.json
{
"name": "Extension name",
"version": "1.0",
"manifest_version": 3,
"description": "Display API Info.",
"background": {
"service_worker": "background.js"
},
"icons": {
"16": "./icons/icon16.png",
"48": "./icons/icon48.png",
"128": "./icons/icon128.png"
},
"action": {}
}
background.js
chrome.browserAction.setBadgeBackgroundColor({ color: "green" });
const setStuff = () => {
chrome.browserAction.setBadgeText({ text: `...` });
}
const callApi = () => {
setStuff();
setTimeout(() => fetch('https://api.com/api')
.then(response => response.json())
.then(data => {
chrome.browserAction.setBadgeText({ text: `${data.info}` });
}).catch(error => console.log(error)), 5000)
}
callApi()
setInterval(function () {
callApi()
}, 300000);
在本地测试时获取此错误
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'setBadgeBackgroundColor')
发布于 2022-03-19 19:03:21
更改清单v2的代码:
chrome.browserAction.setBadgeBackgroundColor({ color: "green" });
const setStuff = () => {
chrome.browserAction.setBadgeText({ text: `...` });
}
对于Chrome扩展清单v3的代码:
chrome.action.setBadgeBackgroundColor({ color: "green" });
const setStuff = () => {
chrome.action.setBadgeText({ text: `...` });
}
https://stackoverflow.com/questions/71522471
复制相似问题