From 6be86b5f05a7c14e9de03d63132e7beb9789965c Mon Sep 17 00:00:00 2001 From: "netop://ウィビ" Date: Sat, 11 Apr 2026 15:16:43 -0700 Subject: initial commit --- demo/asset/script/shared.js | 48 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 demo/asset/script/shared.js (limited to 'demo/asset/script/shared.js') diff --git a/demo/asset/script/shared.js b/demo/asset/script/shared.js new file mode 100644 index 0000000..73ea5c2 --- /dev/null +++ b/demo/asset/script/shared.js @@ -0,0 +1,48 @@ +function copyTextToClipboard(text, { target = document.body } = {}) { + if (typeof text !== "string") + throw new TypeError(`Expected parameter \`text\` to be a \`string\`, got \`${typeof text}\`.`); + + const element = document.createElement("textarea"); + const previouslyFocusedElement = document.activeElement; + let isSuccess = false; + + element.value = text; + element.setAttribute("readonly", ""); + element.style.contain = "strict"; + element.style.position = "absolute"; + element.style.left = "-9999px"; + element.style.fontSize = "12pt"; + + const selection = document.getSelection(); + const originalRange = selection.rangeCount > 0 && selection.getRangeAt(0); + + target.append(element); + + element.select(); + element.selectionStart = 0; + element.selectionEnd = text.length; + + try { + isSuccess = document.execCommand("copy"); + } catch(_) { + // + } + + element.remove(); + + if (originalRange) { + selection.removeAllRanges(); + selection.addRange(originalRange); + } + + if (previouslyFocusedElement) + previouslyFocusedElement.focus(); + return isSuccess; + + /// via https://github.com/sindresorhus/copy-text-to-clipboard +} + +function playAudio() { + const audio = document.getElementById("speak"); + audio.play(); +} -- cgit v1.2.3