最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome extension V3 Cannot read properties of undefined setBadgeBackgroundColor - Stack Overflow

programmeradmin3浏览0评论

I'm trying to make a V3 Chrome Extension work, but I'm not sure how to change this V2 code that works.

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('')
        .then(response => response.json())
        .then(data => {
            chrome.browserAction.setBadgeText({ text: `${data.info}` });
        }).catch(error => console.log(error)), 5000)

}

callApi()

setInterval(function () {
    callApi()
}, 300000);

Getting this error when testing it locally

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'setBadgeBackgroundColor')

I'm trying to make a V3 Chrome Extension work, but I'm not sure how to change this V2 code that works.

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./api')
        .then(response => response.json())
        .then(data => {
            chrome.browserAction.setBadgeText({ text: `${data.info}` });
        }).catch(error => console.log(error)), 5000)

}

callApi()

setInterval(function () {
    callApi()
}, 300000);

Getting this error when testing it locally

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'setBadgeBackgroundColor')
Share Improve this question asked Mar 18, 2022 at 4:25 CiprianCiprian 3,22610 gold badges66 silver badges101 bronze badges 2
  • 2 There's no browserAction in MV3, only action, see the migration guide. – woxxom Commented Mar 18, 2022 at 4:27
  • 1 @wOxxOm can you please add an answer? – Ciprian Commented Mar 22, 2022 at 15:00
Add a ment  | 

1 Answer 1

Reset to default 11

Change your code that is manifest v2:

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

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

To this code for Chrome extension manifest v3:

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

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

评论列表(0)

  1. 暂无评论