Javascript 请求JSON的边缘扩展不';行不通

Javascript 请求JSON的边缘扩展不';行不通,javascript,jquery,json,xmlhttprequest,microsoft-edge,Javascript,Jquery,Json,Xmlhttprequest,Microsoft Edge,我正在进行边缘扩展,需要从外部网站请求JSON。下面的示例在stackoverflow.com上运行良好,但在我需要它的streamcommunity.com/id/上失败 以下是我拥有的文件: jQuery(来自本地文件) manifest.json: { "author": "me", "name": "Get JSON", "description": "get json", "version": "0.1", "manifest_version": 2, "con

我正在进行边缘扩展,需要从外部网站请求JSON。下面的示例在stackoverflow.com上运行良好,但在我需要它的streamcommunity.com/id/上失败

以下是我拥有的文件:

jQuery(来自本地文件)

manifest.json:

{
  "author": "me",
  "name": "Get JSON",
  "description": "get json",
  "version": "0.1",
  "manifest_version": 2,
  "content_scripts": [
    {
      "matches": [
        "*://steamcommunity.com/id/*",
        "*://stackoverflow.com/*"
      ],
      "js": ["jquery.min.js", "myscript.js"]
    }
  ]
}

myscript.js:

var url = 'https://raw.githubusercontent.com/bahamas10/css-color-names/master/css-color-names.json';

$.getJSON(url, function(data) {
  console.log(data);
});

正如我所说,这个示例在Stackoverflow上运行良好,但在Steam上失败。
这是我在Steam网站上收到的错误:

CSP14312: Resource violated directive 'connect-src 'self' http://steamcommunity.com https://steamcommunity.com https://api.steampowered.com/ http://localhost:27060 http://store.steampowered.com/ https://store.steampowered.com/' in Content-Security-Policy: https://raw.githubusercontent.com/bahamas10/css-color-names/master/css-color-names.json. Resource will be blocked.

编辑:我还应该补充一点,当在GoogleChrome中使用Chrome扩展时,这个确切的例子可以很好地工作。


非常感谢您的帮助。

与Chrome和Firefox不同,Microsoft Edge不会忽略网站CSP扩展规则

请参阅以了解一个未解决的问题

我知道的唯一可行的解决方法(我自己也进行了测试)是通过使用
browser.webRequest.onHeadersReceived
动态更改CSP响应头,幸运的是,这在Edge中得到了很好的支持

注意:这段代码是根据这篇伟大的文章改编的,这篇文章也更详细地解释了这个问题:

代码为每个CSP头和指令将扩展添加到允许的URL中,它应在后台页面中执行,该页面应设置为
持久

确保将
webRequest
webRequestBlocking
权限添加到您的
manifest.json

const cspHeaderNames = [
  'content-security-policy',
  'content-security-policy-report-only',
  'x-webkit-csp'
];

// @see https://developer.mozilla.org/en-US/docs/Web/Security/CSP/CSP_policy_directives
const cspSources = [
  'connect-src',
  'default-src',
  'font-src',
  'frame-src',
  'img-src',
  'media-src',
  'object-src',
  'script-src',
  'style-src',
  'child-src',
  'form-action',
];

const url = browser.extension.getURL("").slice(0, -1); // (remove trailing "/")

browser.webRequest.onHeadersReceived.addListener(details => {
  details.responseHeaders.forEach(header => {
    const isCspHeader = cspHeaderNames.indexOf(header.name.toLowerCase()) >= 0;
    if (isCspHeader) {
      let newValue = header.value;
      cspSources.forEach(source => {
        newValue = newValue.replace(source, source + ' ' + url);
      });
      header.value = newValue;
    }
  });
  return {
    responseHeaders: details.responseHeaders
  };
}, {
  urls: ['<all_urls>'],
  types: ['main_frame', "sub_frame"],
}, ["blocking", "responseHeaders"]);
const csp头节点=[
“内容安全策略”,
“仅限内容安全策略报告”,
“x-webkit-csp”
];
//@见https://developer.mozilla.org/en-US/docs/Web/Security/CSP/CSP_policy_directives
常量CSP源=[
“连接src”,
“默认src”,
“字体src”,
“框架src”,
“img src”,
“媒体src”,
'对象src',
“脚本src”,
“样式src”,
“child src”,
“形式行动”,
];
常量url=browser.extension.getURL(“”).slice(0,-1);//(删除尾随“/”)
browser.webRequest.onHeadersReceived.addListener(详细信息=>{
details.responseHeaders.forEach(header=>{
const iscsheader=cspHeaderNames.indexOf(header.name.toLowerCase())>=0;
if(iscsheader){
让newValue=header.value;
cspSources.forEach(source=>{
newValue=newValue.replace(source,source+''+url);
});
header.value=newValue;
}
});
返回{
负责人:详细信息。负责人
};
}, {
网址:[''],
类型:[“主框架”,“子框架”],
}","阻拦","负责人",;

与Chrome和Firefox不同,Microsoft Edge不会忽略网站CSP扩展规则

请参阅以了解一个未解决的问题

我知道的唯一可行的解决方法(我自己也进行了测试)是通过使用
browser.webRequest.onHeadersReceived
动态更改CSP响应头,幸运的是,这在Edge中得到了很好的支持

注意:这段代码是根据这篇伟大的文章改编的,这篇文章也更详细地解释了这个问题:

代码为每个CSP头和指令将扩展添加到允许的URL中,它应在后台页面中执行,该页面应设置为
持久

确保将
webRequest
webRequestBlocking
权限添加到您的
manifest.json

const cspHeaderNames = [
  'content-security-policy',
  'content-security-policy-report-only',
  'x-webkit-csp'
];

// @see https://developer.mozilla.org/en-US/docs/Web/Security/CSP/CSP_policy_directives
const cspSources = [
  'connect-src',
  'default-src',
  'font-src',
  'frame-src',
  'img-src',
  'media-src',
  'object-src',
  'script-src',
  'style-src',
  'child-src',
  'form-action',
];

const url = browser.extension.getURL("").slice(0, -1); // (remove trailing "/")

browser.webRequest.onHeadersReceived.addListener(details => {
  details.responseHeaders.forEach(header => {
    const isCspHeader = cspHeaderNames.indexOf(header.name.toLowerCase()) >= 0;
    if (isCspHeader) {
      let newValue = header.value;
      cspSources.forEach(source => {
        newValue = newValue.replace(source, source + ' ' + url);
      });
      header.value = newValue;
    }
  });
  return {
    responseHeaders: details.responseHeaders
  };
}, {
  urls: ['<all_urls>'],
  types: ['main_frame', "sub_frame"],
}, ["blocking", "responseHeaders"]);
const csp头节点=[
“内容安全策略”,
“仅限内容安全策略报告”,
“x-webkit-csp”
];
//@见https://developer.mozilla.org/en-US/docs/Web/Security/CSP/CSP_policy_directives
常量CSP源=[
“连接src”,
“默认src”,
“字体src”,
“框架src”,
“img src”,
“媒体src”,
'对象src',
“脚本src”,
“样式src”,
“child src”,
“形式行动”,
];
常量url=browser.extension.getURL(“”).slice(0,-1);//(删除尾随“/”)
browser.webRequest.onHeadersReceived.addListener(详细信息=>{
details.responseHeaders.forEach(header=>{
const iscsheader=cspHeaderNames.indexOf(header.name.toLowerCase())>=0;
if(iscsheader){
让newValue=header.value;
cspSources.forEach(source=>{
newValue=newValue.replace(source,source+''+url);
});
header.value=newValue;
}
});
返回{
负责人:详细信息。负责人
};
}, {
网址:[''],
类型:[“主框架”,“子框架”],
}","阻拦","负责人",;