1
0
mirror of https://gitee.com/bookshelfplus/bookshelfplus synced 2025-09-02 23:23:28 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee

复制到剪切板功能兼容旧版浏览器

This commit is contained in:
2022-04-20 13:42:59 +08:00
parent e493458406
commit 7d8ff462ee
2 changed files with 40 additions and 9 deletions

View File

@@ -1,11 +1,42 @@
function copyToClipboard(content) {
var aux = document.createElement('input');
aux.setAttribute('value', content);
aux.style.display = "none";
document.body.appendChild(aux);
aux.select();
document.execCommand('copy');
document.body.removeChild(aux);
// firefox 下必须为用户同步调用,不可以使用异步调用隔开
// refer: http://www.caotama.com/43769.html
function oldMethod() {
var aux = document.createElement('input');
aux.setAttribute('value', content);
// 在 firefox for windows 93.0 版本下测试,添加这一行会无法复制到剪切板
// aux.style.display = "none";
document.body.appendChild(aux);
aux.select();
aux.focus();
document.execCommand('copy');
document.body.removeChild(aux);
console.log("isSuccess", isSuccess);
}
if (navigator.clipboard) {
navigator.permissions.query({ name: "clipboard-write" }).then(result => {
if (result.state == "granted") {
navigator.clipboard.writeText(content)
.then(function () {
// 复制成功
console.log("API复制成功");
}, function () {
// 使用API复制失败
console.log("API复制失败");
oldMethod();
})
} else {
console.log("支持API但没有权限使用旧方法复制");
oldMethod();
}
});
} else {
console.log("不支持API使用旧方法复制");
oldMethod();
}
}
function showTip(e, text) {
var $i = $("<span>").text(text);
@@ -25,7 +56,7 @@ function showTip(e, text) {
"top": y - 60,
"left": x,
"opacity": "0"
}, 600, function() {
}, 600, function () {
$i.remove();
});
e.stopPropagation();

View File

@@ -100,7 +100,7 @@ router.get('/dashboard/:group/:page/:subpage?', function (req, res) {
},
"category-manage": {
title: "分类管理",
baseTemplate: "form",
baseTemplate: "table",
pageTemplate: "CategoryManage",
},
"book-manage": {