gpt4 book ai didi

javascript - Chrome 扩展 V3 无法读取未定义的 setBadgeBackgroundColor 的属性

转载 作者:行者123 更新时间:2023-12-02 15:56:03 25 4
gpt4 key购买 nike

我正在尝试让 V3 Chrome 扩展正常工作,但我不确定如何更改这个有效的 V2 代码。

list .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": {}
}

背景.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')

最佳答案

更改 list v2 中的代码:

chrome.browserAction.setBadgeBackgroundColor({ color: "green" });

const setStuff = () => {
chrome.browserAction.setBadgeText({ text: `...` });
}

Chrome 扩展 list v3 的这段代码:

chrome.action.setBadgeBackgroundColor({ color: "green" });

const setStuff = () => {
chrome.action.setBadgeText({ text: `...` });
}

关于javascript - Chrome 扩展 V3 无法读取未定义的 setBadgeBackgroundColor 的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71522471/

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