Duh*_*uis 5 google-chrome-extension
我在扩展程序中面临CSP问题...
我使用内容脚本来更改网站上的图像.我的内容脚本是将自己的图像添加到网站,所以我有以下警告:
[Report Only] Refused to load image from 'chrome://extension/xxx/...'
because of Content-Security-Policy.
The page at https://plus.google.com/u/0/hot displayed insecure content
from chrome://extension/xxx/....
Run Code Online (Sandbox Code Playgroud)
所以我在清单中添加了以下行:
"content_security_policy": "default-src *"
Run Code Online (Sandbox Code Playgroud)
并且警告消失了......
现在,我需要修改图像,为此,我将它们写入画布,获取dataURL并将其转换为WebkitBlobBuilder,以避免由于img标记上的src更改导致的内存泄漏(使用blob,我可以撤消它一次它已被使用,它释放了记忆......)
部分代码:
//Code to create a blob from dataURI
base.dataURItoBlob = function(dataURI, callback) {
var byteString;
if (dataURI.split(',')[0].indexOf('base64') >= 0)
byteString = atob(dataURI.split(',')[1]);
else
byteString = unescape(dataURI.split(',')[1]);
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
var bb = new WebKitBlobBuilder();
bb.append(ab);
return bb.getBlob(mimeString);
};
//Code to display the blob on an image :
//Write image on a canvas :
base.ctx.putImageData(cData, img.leftPos, img.topPos);
//Get a blob
var blobData = base.dataURItoBlob(base.canvas.toDataURL("image/png"));
//Create an URL from the blob
var urlfile = window.webkitURL.createObjectURL(dataBlob);
//set it on the img tag
img.attr("src", urlfile);
//Revoke the blob once loaded
img.load(function() {
window.webkitURL.revokeObjectURL(urlfile);
});
Run Code Online (Sandbox Code Playgroud)
这段代码效果很好....由于我的img标签上的src更改,没有更多的内存泄漏.
但是我有这个警告:
[Report Only] Refused to load image from 'blob:https%3A%2F
%2Fplus.google.com/52ac1648-64d6-4fce-bb35-537d939d5007' because of
Content-Security-Policy.
The page at https://plus.google.com/u/0/hot displayed insecure content
from blob:https%3A%2F%2Fplus.google.com/52ac1648-64d6-4fce-
bb35-537d939d5007.
Run Code Online (Sandbox Code Playgroud)
为什么内容策略的default-src不适用于blob?
谢谢 !
根据 CSP 规范:
4.1.3 数据:不允许使用 URI
所以你必须在清单中将其列入白名单
"content_security_policy": "script-src 'self'; object-src 'self'; img-src data:
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1157 次 |
| 最近记录: |