diff options
| author | netop://ウィビ <paul@webb.page> | 2026-04-11 15:16:43 -0700 |
|---|---|---|
| committer | netop://ウィビ <paul@webb.page> | 2026-04-11 15:16:43 -0700 |
| commit | 6be86b5f05a7c14e9de03d63132e7beb9789965c (patch) | |
| tree | dae243510721368d64f4b5c60e7f66e4ad048f77 /demo/asset/script | |
Diffstat (limited to 'demo/asset/script')
| -rw-r--r-- | demo/asset/script/shared.js | 48 |
1 files changed, 48 insertions, 0 deletions
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(); +} |
