فهرست منبع

Use navigator.clipboard.writeText

Simon Legner 9 ماه پیش
والد
کامیت
ba294a12de
2فایلهای تغییر یافته به همراه3 افزوده شده و 22 حذف شده
  1. 0 18
      assets/javascripts/lib/util.js
  2. 3 4
      assets/javascripts/views/content/entry_page.js

+ 0 - 18
assets/javascripts/lib/util.js

@@ -526,21 +526,3 @@ $.highlight = function (el, options) {
   el.classList.add(options.className);
   setTimeout(() => el.classList.remove(options.className), options.delay);
 };
-
-$.copyToClipboard = function (string) {
-  let result;
-  const textarea = document.createElement("textarea");
-  textarea.style.position = "fixed";
-  textarea.style.opacity = 0;
-  textarea.value = string;
-  document.body.appendChild(textarea);
-  try {
-    textarea.select();
-    result = !!document.execCommand("copy");
-  } catch (error) {
-    result = false;
-  } finally {
-    document.body.removeChild(textarea);
-  }
-  return result;
-};

+ 3 - 4
assets/javascripts/views/content/entry_page.js

@@ -217,10 +217,9 @@ app.views.EntryPage = class EntryPage extends app.View {
       this.load();
     } else if (target.classList.contains("_pre-clip")) {
       $.stopEvent(event);
-      target.classList.add(
-        $.copyToClipboard(target.parentNode.textContent)
-          ? "_pre-clip-success"
-          : "_pre-clip-error",
+      navigator.clipboard.writeText(target.parentNode.textContent).then(
+        () => target.classList.add("_pre-clip-success"),
+        () => target.classList.add("_pre-clip-error"),
       );
       setTimeout(() => (target.className = "_pre-clip"), 2000);
     }