0%

How to load background script in manifest V3 chrome extension?

chrome extension manifest V3 無法載入 background.js

  • 問題:chrome extension 官方的 getstarted,在 Manifest V3 跑不起來
  • 文件:Getting started - Chrome Developers
    • Step-by-step instructions on how to create a Chrome Extension.
    • Updated on Wednesday, November 18, 2020
  • 遇到的錯誤:
    1
    2
    background.js not working
    chrome.storage.sync.set undefined

處理方式

  1. 確認 Chrome 更新到 88

    Manifest V3 is available beginning with Chrome 88

  2. 處理無法載入 background.js 的情況

  • 原本的範例:

    1
    2
    3
    4
    "background": {
    "scripts": ["background.js"],
    "persistent": false
    },
  • 改寫成:

    1
    2
    3
    "background": {
    "service_worker": "background.js"
    },

    Replace background.page or background.scripts with background.service_worker in manifest.json. Note that the service_worker field takes a string, not an array of strings.

    Remove background.persistent from manifest.json.

驗證

  • 出現下面框起來的這行代表有成功

參考資料