当前位置:网站首页>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!!!");
边栏推荐
- Linux server development, MySQL transaction principle analysis
- 数据库实时同步利器——CDC(变化数据捕获技术)
- 芯片 設計資料下載
- Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
- Myabtis_ Plus
- Recursive method to verify whether a tree is a binary search tree (BST)
- 漏洞复现-Fastjson 反序列化
- Implementation of replacement function of shell script
- Padavan manually installs PHP
- Content of string
猜你喜欢
Réplication de vulnérabilité - désrialisation fastjson
藏书馆App基于Rainbond实现云原生DevOps的实践
Avatary的LiveDriver试用体验
UnityHub破解&Unity破解
[quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
漏洞複現-Fastjson 反序列化
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
Linux server development, MySQL index principle and optimization
Application of slip ring of shipborne radar antenna
Force buckle 145 Binary Tree Postorder Traversal
随机推荐
Basic use of CTF web shrink template injection nmap
[quick start of Digital IC Verification] 15. Basic syntax of SystemVerilog learning 2 (operators, type conversion, loops, task/function... Including practical exercises)
The element with setfieldsvalue set is obtained as undefined with GetFieldValue
Complex network modeling (III)
Uniapp mobile terminal forced update function
快解析内网穿透为文档加密行业保驾护航
LeetCode中等题之我的日程安排表 I
Force buckle 144 Preorder traversal of binary tree
Quickly use Jacobo code coverage statistics
game攻防世界逆向
Zsh shell adds automatic completion and syntax highlighting
互动送书-《Oracle DBA工作笔记》签名版
央视太暖心了,手把手教你写HR最喜欢的简历
Excel import function of jeesite form page
Content of string
Network learning (II) -- Introduction to socket
Who has docker to install MySQL locally?
通俗易懂单点登录SSO
青龙面板-今日头条
The legend about reading the configuration file under SRC