From ba521b527ca31e2d9db5340f514d1a39e245ff73 Mon Sep 17 00:00:00 2001 From: ayunami2000 Date: Sun, 16 Jan 2022 20:37:27 -0500 Subject: [PATCH] add clipboard shart --- javascript/index.html | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/javascript/index.html b/javascript/index.html index b9f8d7e..0dca1aa 100644 --- a/javascript/index.html +++ b/javascript/index.html @@ -7,6 +7,41 @@ window.addEventListener("load", function(){ window.minecraftOpts = [ "game_frame","assets.epk", "CgAACQAHc2VydmVycwoAAAACCAACaXAAJHdzczovL2cuZWFncy51cy9lYWdsZXJjcmFmdC9jcmVhdGl2ZQgABG5hbWUAFGVhZ2xlcmNyYWZ0IGNyZWF0aXZlAQALaGlkZUFkZHJlc3MACAAKZm9yY2VkTU9URAAhdGhpcyBpcyBtZWFudCB0byBiZSBhIGRlbW8gc2VydmVyAAgAAmlwACR3c3M6Ly9nLmVhZ3MudXMvZWFnbGVyY3JhZnQvc3Vydml2YWwIAARuYW1lABRlYWdsZXJjcmFmdCBzdXJ2aXZhbAEAC2hpZGVBZGRyZXNzAAgACmZvcmNlZE1PVEQAI3NlZSBnaXRodWIgZm9yIHByaXZhdGUgc2VydmVyIGd1aWRlAAA=" ]; main(); }); +//modified from https://gist.github.com/GlauberF/d8278ce3aa592389e6e3d4e758e6a0c2 +function simulateKey (key, type) { + var keyCode = key.charCodeAt(0); + var evtName = (typeof(type) === "string") ? "key" + type : "keydown"; + + var event = document.createEvent("HTMLEvents"); + event.initEvent(evtName, true, false); + event.keyCode = event.which = keyCode; + event.key = key; + event.shiftKey = false; + event.ctrlKey = false; + event.metaKey = false; + + document.dispatchEvent(event); +} +if(window.navigator.clipboard&&window.isSecureContext){ + window.addEventListener("keydown",function(e){ + if((e.ctrlKey||e.metaKey)&&e.keyCode==86&&!e.altKey){ + window.navigator.clipboard.readText().then(clipdata=>{ + if(clipdata==null||clipdata=="")return; + simulateKey("\u0011","up"); + simulateKey("\u0011","press"); + simulateKey("\b","down"); + simulateKey("\b","up"); + simulateKey("\b","press"); + var clipchars=clipdata.split(""); + for(var clipchar of clipchars){ + simulateKey(clipchar,"down"); + simulateKey(clipchar,"up"); + simulateKey(clipchar,"press"); + } + }); + } + }); +}