Update index.js

添加在移动设备上主动调用剪贴板权限以获得用户授权
This commit is contained in:
mason369 2023-04-13 12:39:25 +08:00
parent 8786660ceb
commit 91734565f1

View File

@ -6,6 +6,14 @@ function wait(timeout) {
return new Promise((resolve) => setTimeout(resolve, timeout));
}
async function requestClipboardPermission() {
try {
await navigator.permissions.query({ name: "clipboard-write" });
} catch (error) {
console.error(error);
}
}
async function init() {
try {
await wait(1000);
@ -19,6 +27,9 @@ async function init() {
for (let i = 0; i < copyBtnList.length; i++) {
const btn = copyBtnList[i];
btn.addEventListener("touchend", async () => {
// 请求剪贴板权限
await requestClipboardPermission();
alert("touchend");
const parentDiv = btn.parentNode;
const codeElem = parentDiv.querySelector("code");
@ -26,6 +37,7 @@ async function init() {
const textToCopy = [
...new Set(spans.map((span) => span.textContent)),
].join("");
console.log(textToCopy);
try {
await navigator.clipboard.writeText(textToCopy);