【问题标题】:Chrome Extension Cross-Origin Requests in Background Script Blocked后台脚本中的 Chrome 扩展跨域请求被阻止
【发布时间】:2020-06-05 19:36:41
【问题描述】:

我尝试从我的后台脚本中获取数据,如推荐的 here,但它总是被阻止。我错过了什么吗?

Background.js:

    chrome.storage.sync.get(['sigurl'], function(result) {
      console.log(result.sigurl);
      fetch(result.sigurl)
      .then((response) => {
        return response.text();
      })
      .then((html) => {
        console.log(html);
        chrome.storage.sync.set({'sig': html}, function() {});
      })
      .catch(function(err) {
            console.log('Failed to fetch page: ', err);
        });
    });

控制台:

Access to fetch at 'http://example.com/test.html' from origin 'chrome-extension://pbflkjmmkpgddamdcihlbggdccjmjmbk' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

使用 no-cors 模式也不起作用。

【问题讨论】:

  • http://example.com/ 添加到permissions in manifest.json
  • @wOxxOm 后台脚本不应该被CORS阻止吗?
  • @avalanche1,请参阅我在第一条评论中提供的链接。

标签: google-chrome-extension


【解决方案1】:

我的问题的解决方案是添加

"permissions": [
    "https://*/"
  ]

到我的 manifest.json

感谢 wOxxOm 为我指明了正确的方向。

【讨论】:

  • 我想知道这是否会产生Extension can: Read and change all your data on the websites you visit警告? @wOxxOm
  • 我刚刚意识到你应该只在权限中添加你想访问的网站以限制恶意代码,如 WOxxOm 给出的链接所示
猜你喜欢
  • 2014-03-04
  • 2012-04-22
  • 2021-06-26
  • 2014-10-13
  • 1970-01-01
  • 2019-11-04
  • 2017-02-06
相关资源
最近更新 更多