浏览器扩展 service-worker中如何访问http请求

发布时间 2023-07-08 11:16:55作者: jqynr

参考 :https://stackoverflow.com/questions/71443428/how-can-i-make-an-http-request-in-the-service-worker-chrome-extension

fetch文档:https://developer.mozilla.org/en-US/docs/Web/API/fetch

因为service-worker没有没有运行到普通的环境,普通的node库也用不了,所以需要使用环境方法fetch

fetch('http://example.com/movies.json')
  .then((response) => response.json())
  .then((data) => console.log(data));

manifest中加入(好像也不是太需要)

  "host_permissions": [
    "http://example.com/"
  ],