当前位置:网站首页>JS copy picture to clipboard read clipboard
JS copy picture to clipboard read clipboard
2022-07-07 08:11:00 【qiphon3650】
JS Copy the image to the clipboard Read the clipboard
navigator.clipboard Copy pictures

Picture writing clipboard
function handleCopyImg() {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const img = new Image();
canvas.width = testImg.width;
canvas.height = testImg.height;
img.crossOrigin = "Anonymous";
img.src = testImg.src;
img.onload = () => {
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
ctx.drawImage(img, 0, 0); // take canvas To blob
canvas.toBlob(async blob => {
console.log(blob);
const data = [
new ClipboardItem({
[blob.type]: blob,
}),
]; // https://w3c.github.io/clipboard-apis/#dom-clipboard-write
await navigator.clipboard.write(data)
.then(
() => {
console.log("Copied to clipboard successfully!");
},
() => {
console.error("Unable to write to clipboard.");
}
);
});
}}
btn.addEventListener("click", handleCopyImg, false);
read Method returns a Promise, Get the copied content
async function foo() {
const items = await navigator.clipboard.read();
const imageBlob = await items[0].getType("image/png");
console.log("imageBlob==>", imageBlob);
}
// Read text
async function foo() {
await navigator.clipboard.readText().then((data) => {
console.log(data);
});
}
write Method , Write clipboard contents , Such as pictures or other documents
const data = [
new ClipboardItem({
"text/plain": new Blob(["hello clipboard"], {
type: "text/plain" })
})
];
navigator.clipboard.write(data).then(
() => {
console.log("Copied to clipboard successfully!");
},
() => {
console.error("Unable to write to clipboard. :-(");
}
);
// writeText Method to write text content
navigator.clipboard.writeText("hello!!!");
边栏推荐
- Register of assembly language by Wang Shuang
- eBPF Cilium实战(1) - 基于团队的网络隔离
- 云原生存储解决方案Rook-Ceph与Rainbond结合的实践
- Codeforce c.strange test and acwing
- Zsh shell adds automatic completion and syntax highlighting
- Vulnerability recurrence fastjson deserialization
- Call pytorch API to complete linear regression
- 互动送书-《Oracle DBA工作笔记》签名版
- Find the mode in the binary search tree (use medium order traversal as an ordered array)
- game攻防世界逆向
猜你喜欢

Record a stroke skin bone error of the skirt

Excel import function of jeesite form page

Force buckle 144 Preorder traversal of binary tree

Linux server development, redis source code storage principle and data model

船载雷达天线滑环的使用

Basic use of CTF web shrink template injection nmap

Qinglong panel - today's headlines

Avatary的LiveDriver试用体验

漏洞複現-Fastjson 反序列化

Hisense TV starts the developer mode
随机推荐
JSON data flattening pd json_ normalize
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
面试题(CAS)
柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
Leetcode medium question my schedule I
Quickly use Jacobo code coverage statistics
Relevant data of current limiting
Merging binary trees by recursion
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
漏洞复现-easy_tornado
Fast parsing intranet penetration escorts the document encryption industry
Myabtis_ Plus
LeetCode简单题之字符串中最大的 3 位相同数字
It took "7" years to build the robot framework into a micro service
Linux server development, MySQL cache strategy
芯片 設計資料下載
Lattice coloring - matrix fast power optimized shape pressure DP
央视太暖心了,手把手教你写HR最喜欢的简历
Summary of redis functions
云原生存储解决方案Rook-Ceph与Rainbond结合的实践