Skip to content

Commit 9173456

Browse files
committed
Update index.js
添加在移动设备上主动调用剪贴板权限以获得用户授权
1 parent 8786660 commit 9173456

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

  • rustedwarfareapicode/.vitepress/theme

rustedwarfareapicode/.vitepress/theme/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ function wait(timeout) {
66
return new Promise((resolve) => setTimeout(resolve, timeout));
77
}
88

9+
async function requestClipboardPermission() {
10+
try {
11+
await navigator.permissions.query({ name: "clipboard-write" });
12+
} catch (error) {
13+
console.error(error);
14+
}
15+
}
16+
917
async function init() {
1018
try {
1119
await wait(1000);
@@ -19,13 +27,17 @@ async function init() {
1927
for (let i = 0; i < copyBtnList.length; i++) {
2028
const btn = copyBtnList[i];
2129
btn.addEventListener("touchend", async () => {
30+
// 请求剪贴板权限
31+
await requestClipboardPermission();
32+
2233
alert("touchend");
2334
const parentDiv = btn.parentNode;
2435
const codeElem = parentDiv.querySelector("code");
2536
const spans = [...codeElem.querySelectorAll("span")];
2637
const textToCopy = [
2738
...new Set(spans.map((span) => span.textContent)),
2839
].join("");
40+
console.log(textToCopy);
2941

3042
try {
3143
await navigator.clipboard.writeText(textToCopy);

0 commit comments

Comments
 (0)