From 4517ec7864d650d26188b5b66d2c8323f70f2bda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=A4=E5=BF=83?= <3277200+sentsim@users.noreply.github.com> Date: Fri, 1 Sep 2023 23:44:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20`lay.clipboard.writeText()?= =?UTF-8?q?`=20=E6=96=B9=E6=B3=95=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=86=99?= =?UTF-8?q?=E5=85=A5=E5=89=AA=E5=88=87=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/modules/lay.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/modules/lay.js b/src/modules/lay.js index dadec147..99963200 100644 --- a/src/modules/lay.js +++ b/src/modules/lay.js @@ -309,6 +309,37 @@ return matched; }; + // 剪切板 + lay.clipboard = { + // 写入文本 + writeText: function(options) { + var text = String(options.text); + + try { + navigator.clipboard.writeText(text).then( + options.done + ).catch(options.error); + } catch(e) { + var elem = document.createElement('textarea'); + + elem.value = text; + elem.style.position = 'absolute'; + elem.style.opacity = '0'; + document.body.appendChild(elem); + elem.select(); + + try { + document.execCommand('copy'); + typeof options.done === 'function' && options.done(); + } catch(err) { + typeof options.error === 'function' && options.error(err); + } + + elem.remove(); + } + } + }; + /* * lay 元素操作