@@ -0,0 +1,67 @@
|
||||
(function () {
|
||||
var buttons = document.querySelectorAll("[data-share-page]");
|
||||
if (!buttons.length) return;
|
||||
|
||||
var RESET_MS = 2000;
|
||||
|
||||
function fallbackCopy(text) {
|
||||
var ta = document.createElement("textarea");
|
||||
ta.value = text;
|
||||
ta.setAttribute("readonly", "");
|
||||
ta.style.cssText = "position:fixed;top:0;left:0;width:1px;height:1px;padding:0;border:0;opacity:0;";
|
||||
document.body.appendChild(ta);
|
||||
ta.focus();
|
||||
ta.select();
|
||||
ta.setSelectionRange(0, ta.value.length);
|
||||
var ok = false;
|
||||
try {
|
||||
ok = document.execCommand("copy");
|
||||
} catch (_) {
|
||||
ok = false;
|
||||
}
|
||||
document.body.removeChild(ta);
|
||||
return ok;
|
||||
}
|
||||
|
||||
function copyText(text) {
|
||||
if (navigator.clipboard && navigator.clipboard.writeText) {
|
||||
return navigator.clipboard.writeText(text).catch(function () {
|
||||
fallbackCopy(text);
|
||||
});
|
||||
}
|
||||
fallbackCopy(text);
|
||||
return Promise.resolve();
|
||||
}
|
||||
|
||||
function showCopied(btn, label, original) {
|
||||
if (label) {
|
||||
label.textContent = "链接已复制";
|
||||
label.classList.add("is-copied");
|
||||
}
|
||||
btn.classList.add("is-share-copied");
|
||||
btn.setAttribute("aria-label", "链接已复制");
|
||||
if (typeof window.showToast === "function") {
|
||||
window.showToast("链接已复制", "success");
|
||||
}
|
||||
if (btn._shareResetTimer) window.clearTimeout(btn._shareResetTimer);
|
||||
btn._shareResetTimer = window.setTimeout(function () {
|
||||
if (label) {
|
||||
label.textContent = original;
|
||||
label.classList.remove("is-copied");
|
||||
}
|
||||
btn.classList.remove("is-share-copied");
|
||||
btn.setAttribute("aria-label", original || "分享");
|
||||
btn._shareResetTimer = 0;
|
||||
}, RESET_MS);
|
||||
}
|
||||
|
||||
Array.prototype.forEach.call(buttons, function (btn) {
|
||||
var label = btn.querySelector("[data-share-page-label]");
|
||||
var original = (label && label.textContent) || btn.getAttribute("aria-label") || "分享";
|
||||
btn.addEventListener("click", function (event) {
|
||||
event.preventDefault();
|
||||
showCopied(btn, label, original);
|
||||
copyText(window.location.href);
|
||||
});
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user