1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Chrome拡張 manifestV3でディレクトリ内の画像にアクセスする

Posted at

やり方

manifest.jsonweb_accessible_resourcesで使用する画像を設定する。

manifest.json
{
    "web_accessible_resources": [{
        "resources": ["img/*"],
        "matches": ["<all_urls>"]
    }]
}

content.js (manifest.jsonにてcontent_scriptsに指定しているjsなりtsなり)でchrome.runtime.getURL("img/hoge.jpg");をして取得。

content.js
var image = document.createElement("img");
image.src = chrome.runtime.getURL("img/hoge.jpg");

おわり

軽く解説

web_aacessible_resourcesについて

webから拡張機能内のリソースにアクセスできるようにしたいときに設定する。
ここを設定していなければ、webからアクセスすることはできない。
v3から書き方もちょっと変わっているみたい。
詳しくは公式を見ると良さそう。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?