我尝试使用DataTransfer.mozSetDataAt()
jsfiddle 中的方法 script它抛出
SecurityError: The operation is insecure.
这是为什么呢?这个方法只能用于扩展吗(不允许在常规网页中使用)?
使用的浏览器:Firefox 55.0.2
最佳答案
这里似乎抛出的是您对私有(private)'application/x-moz-file'
数据类型的使用。
如果您使用真正的 MIME 类型 ('image/png'
),那么它不会抛出异常,您的文件将被添加到 dataTransfer 对象中:
const button = document.querySelector('button');
const image = document.querySelector('img');
let file = null;
button.addEventListener('click', () => {
document.execCommand('copy')
})
document.addEventListener('copy', (event) => {
event.clipboardData.mozSetDataAt('image/png', file, 0);
console.log(event.clipboardData.files)
})
// to avoid the big dataURI in post
fetch("https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Mozilla_Firefox_3.5_logo_256.png/145px-Mozilla_Firefox_3.5_logo_256.png")
.then(resp => resp.blob())
.then(blob => file = new File([blob], 'firefox.png'));
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Mozilla_Firefox_3.5_logo_256.png/145px-Mozilla_Firefox_3.5_logo_256.png" width="20" height="20"/>
<button>copy</button>
关于javascript - DataTransfer.mozSetDataAt() 抛出 SecurityError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45968560/