summaryrefslogtreecommitdiff
path: root/demo/asset/script/shared.js
diff options
context:
space:
mode:
Diffstat (limited to 'demo/asset/script/shared.js')
-rw-r--r--demo/asset/script/shared.js48
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();
+}