提取API,Chrome扩展程序,内容脚本,CORS权限

时间:2018-12-10 20:38:40

标签: google-chrome-extension fetch-api

不知道是否有人注意到了这一点。

在内容脚本中将XMLHttpRequest设置为图像文件:

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        console.log(xhttp.getAllResponseHeaders());
    }
};
xhttp.open("HEAD", "https://www.gstatic.com/webp/gallery3/1.sm.png", true);
xhttp.send();

您将获得以下信息:

  

日期:2018年11月27日星期二15:42:53 GMT x-content-type-options:nosniff   上次修改时间:2016年4月21日,星期四GMT服务器:sffe年龄:1136854   不同:原始内容类型:image / png状态:200缓存控件:   public,max-age = 31536000 accept-ranges:bytes alt-svc:quic =“:443”;   ma = 2592000; v =“ 44,43,39,35”内容长度:48061 x-xss-protection:1;   模式=阻止过期:2019年11月27日星期三格林尼治标准时间

在内容脚本中对同一张图片发出提取请求:

fetch("https://www.gstatic.com/webp/gallery3/1.sm.png", {method: "HEAD"})
    .then(res => { 
        var headers = res.headers;
        headers.forEach(function(value, name) { console.log(`${name} : ${value}`);
     });

您将获得以下信息:

  

缓存控制:公共,最大年龄= 31536000内容类型:image / png   过期:周二,2019年12月10日20:12:24 GMT上次修改时间:周四,2016年4月21日   03:17:22 GMT

这是由于W3规范here所致,这向我证实,获取并不完全受益于Chrome扩展程序的跨域清单豁免。但是,如果您使用Fetch进行get请求,它将成功。

有人知道这是否是故意的吗?

0 个答案:

没有答案