ThisAV 发表于 2021-11-25 14:49:00

图像加载遇到“内容安全策略”有解决办法吗?

https://github.com/Owyn/HandyImage/issues/451

如上面的链接,通过ajax操作获取到图像地址,
https://i.piccy.info/i9/ac44f4a35e09a0d62fc0365dd33e7828/1635318743/16734/1446202/2021_10_27_10_07_37.jpg
然后添加img标签把图像加载到网页上,

但是会遇到“内容安全策略”的报错。

油猴脚本有什么处理办法吗?
PS:测试可以随便找一个图片替换掉头像src地址就可以在控制台看到
because it violates the following Content Security Policy directive: "img-src data: 'self' data: github.githubassets.com identicons.github.com collector.githubapp.com github-cloud.s3.amazonaws.com secured-user-images.githubusercontent.com/ *.githubusercontent.com".

新注册账户,币就这么点了

李恒道 发表于 2021-11-25 14:49:01

// ==UserScript==
// @name         New Userscript
// @namespace    http://tampermonkey.net/
// @version      0.1
// @descriptiontry to take over the world!
// @author       You
// @match      https://github.com/Owyn/HandyImage/issues/451
// @icon         https://www.google.com/s2/favicons?domain=github.com
// @grant      unsafeWindow
// @grant      GM_xmlhttpRequest
// ==/UserScript==
function arrayBufferToBase64(buffer) {

    var binary = '';
    var bytes = new Uint8Array(buffer);
    var len = bytes.byteLength;
    for (var i = 0; i < len; i++) {

      binary += String.fromCharCode(bytes);
    }
    return window.btoa(binary);
}
GM_xmlhttpRequest({
    url:'https://i.piccy.info/i9/ac44f4a35e09a0d62fc0365dd33e7828/1635318743/16734/1446202/2021_10_27_10_07_37.jpg',
    responseType:'arraybuffer',
    method :"GET",
    headers: {
    },
    onload:function(xhr){
      let url= arrayBufferToBase64(xhr.response);
      var img=document.createElement('img')
      img.src='data:image/jpeg;base64,'+url;
      document.body.append(img)

    }
});

李恒道 发表于 2021-11-25 15:37:29

用gm_xhr读到图片再画到页面上?

李恒道 发表于 2021-11-25 15:38:35

不过直接src设置图片路径,然后再读也没问题吧

ThisAV 发表于 2021-11-25 15:42:56

李恒道 发表于 2021-11-25 15:38
不过直接src设置图片路径,然后再读也没问题吧

不行,通过审查工具直接修改src属性,不是内容安全策略所述的网站内容都无法显示

也包括blob协议获取的内容

来碰碰运气大佬了有没有解法

李恒道 发表于 2021-11-25 15:45:54

ThisAV 发表于 2021-11-25 15:42
不行,通过审查工具直接修改src属性,不是内容安全策略所述的网站内容都无法显示

也包括blob协议获取的 ...

var img=document.createElement('img')
img.src='https://i.piccy.info/i9/ac44f4a35e09a0d62fc0365dd33e7828/1635318743/16734/1446202/2021_10_27_10_07_37.jpg'
document.body.append(img)

李恒道 发表于 2021-11-25 15:46:17

![图片.png](data/attachment/forum/202111/25/154615uz7hoz2xcwoow0wh.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "图片.png")

李恒道 发表于 2021-11-25 15:47:22

印象里script和img都是没有跨域限制的吧

ThisAV 发表于 2021-11-25 15:56:38

李恒道 发表于 2021-11-25 15:47
印象里script和img都是没有跨域限制的吧



我说的是在 github 上加载这个图像

李恒道 发表于 2021-11-25 16:11:02

这个意思吗

![图片.png](data/attachment/forum/202111/25/161100f9m1y65dak16za6k.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/300 "图片.png")
页: [1] 2
查看完整版本: 图像加载遇到“内容安全策略”有解决办法吗?