LoginSignup
6

More than 5 years have passed since last update.

【メモ】Google Chromeの拡張機能でHTMLを読み込む

Last updated at Posted at 2018-06-05

Google Chromeの拡張機能で、HTMLファイルを使用する方法の私的メモ。

manifest.json
{
    "web_accessible_resources": ["読み込みたいHTMLファイル.html"]
}
  • HTMLをリソースとして宣言する
async function injectHtml(resource, dom) {
    const res = await fetch(chrome.runtime.getURL(resource), { method: "GET" })
    const html = await res.text()

    dom.innerHTML = html
}

const target = document.getElementById("HTMLを挿入するDOM")
injectHtml("HTMLのファイル名", target)
  • web_accessible_resourcesで宣言したファイルは、chrome.runtime.getURL()からURL経由で利用できる。
  • Chrome専用なのでビルドツールなしでasyncawaitが使えて楽しい。

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
6