🐛 [修复] 原生系统剪贴板,只适用被授权安全的站点,http下不能使用的问题

This commit is contained in:
须弥 2021-12-26 16:34:44 +08:00
parent 96ed2c948d
commit b2705b41be

View File

@ -34,9 +34,40 @@ const toggle = function () {
const copy = function () {
const foundCodes = meta.value.getElementsByClassName('language-html')
if (document.hasFocus()) {
const text = foundCodes[0].textContent || "";
const foundCode = foundCodes[0];
let successful = false;
// 使http使
if (navigator.clipboard && document.hasFocus()) {
const text = foundCode.textContent || "";
navigator.clipboard.writeText(text);
successful = true;
} else if (window.getSelection()){
// 使document.execCommand
// div使div
var range = document.createRange();
let copyDiv;
if (show.value) {
range.selectNode(foundCode);
} else {
copyDiv = document.createElement('div');
copyDiv.innerHTML = foundCode.innerHTML;
copyDiv.style.position="fixed";
copyDiv.style.left="-9999px";
document.body.appendChild(copyDiv);
range.selectNode(copyDiv);
}
window.getSelection()?.addRange(range);
try {
successful = document.execCommand('copy');
} catch(err) {
successful = false;
console.error(err);
}
window.getSelection()?.removeAllRanges();
copyDiv?.remove();
}
if (successful) {
layer.msg("复制成功", { icon : 1, time: 1000}, ()=>{})
} else {
layer.msg("复制失败", { icon : 2, time: 1000}, ()=>{})