navigator.clipboard.write 复制图片到钉钉、微信、Word 等
navigator.clipboard.writeText('Linr Text to be copied')
location ^~ / {
root html;
index index.html index.htm;
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET,POST';
add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
}
convertToPng = imgBlob => {
const imageUrl = window.URL.createObjectURL(imgBlob);
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
const imageEl = createImage({ src: imageUrl });
imageEl.onload = e => {
canvas.width = e.target.width;
canvas.height = e.target.height;
ctx.drawImage(e.target, 0, 0, e.target.width, e.target.height);
canvas.toBlob(copyToClipboard, "image/png", 1);
};
};
createImage = options => {
options = options || {};
const img = Image ? new Image() : document.createElement("img");
if (options.src) {
img.src = options.src;
}
return img;
};
// 文本
createTextBlob = text => {
return new Blob([text], { type: "text/plain" });
};
copyToClipboard = pngBlob => {
console.log(pngBlob);
// const textBlob = createTextBlob("大家好222");
try {
navigator.clipboard.write([
new ClipboardItem({
[pngBlob.type]: pngBlob,
// [textBlob.type]: textBlob
})
]);
console.log("Image copied");
} catch (error) {
console.error(error);
}
};
// 复制图片
copyImg(
"https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1089874897,1268118658&fm=26&gp=0.jpg"
);