Keyboard Display Script

shows keyboard inputs on screen

As of 2019-07-09. See the latest version.

  1. // ==UserScript==
  2. // @name Keyboard Display Script
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.22
  5. // @description shows keyboard inputs on screen
  6. // @author Oki
  7. // @match https://*.jstris.jezevec10.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. /**************************
  12. Keyboard Display Script
  13. **************************/
  14.  
  15. (function() {
  16. window.addEventListener('load', function(){
  17.  
  18.  
  19. if(typeof Game != "undefined" || typeof Replayer != "undefined"){
  20.  
  21. if(typeof getParams != "function"){var getParams=a=>{var params=a.slice(a.indexOf("(")+1);params=params.substr(0,params.indexOf(")")).split(",");return params}}
  22. if(typeof trim != "function"){var trim=a=>{a=a.slice(0,-1);a=a.substr(a.indexOf("{")+1);return a}}
  23.  
  24.  
  25. var kbhold=document.createElement("div");
  26. kbhold.id="keyboardHolder";
  27. kbhold.style.position="absolute"
  28. kbhold.style.left = (myCanvas.getBoundingClientRect().right + 300) + "px";
  29. kbhold.style.top = (myCanvas.getBoundingClientRect().top + 100) + "px";
  30.  
  31.  
  32. if(typeof Replayer != "undefined") {
  33. Replayer["pressKey"] = function(num,type){
  34. //type: 0=release 1=down 2=press
  35. document.getElementsByClassName("kbkey")[num].style.backgroundColor = type?"lightgoldenrodyellow":""
  36. if(type==2){
  37. setTimeout(x=>{document.getElementsByClassName("kbkey")[num].style.backgroundColor = ""},100)
  38. }
  39.  
  40. }
  41. kbhold.style.top = (myCanvas.getBoundingClientRect().top + 200) + "px";
  42. }
  43.  
  44. document.body.appendChild(kbhold);
  45.  
  46. var f='<R>M{text-align:center;position: absolute;font-size:15px`{]-Q:QIspacing:0;Nred`td|th|Uwp8o_000000;]:inherit`UT_34ff34`Ujy2k_f8a102`UO_f8ff00;}</RYbo"Yps"KPJV"Ptr^Tq180~TqSD~TqHDZ[CCWZXtr^OqHL~OqCWZ[&lt;[v[&gt;X/JK>~</td^|{padding:10px 5pxIR:solidIwidth:2px`q kbkey">`;}M.tg _{]-N#^PtdV-]border[~jy2kqZ~wp8o">YPdiv id="kX</tdP/trPV class="tgU.tg-Ttc3eRstyleQcollapseP><Op39mNcolor:M#kbo KP/divJtableI;]-';var g=0;var i=0;for(i in g='IJKMNOPQRTUVXYZ[]^_`q|~')var e=f.split(g[i]),f=e.join(e.pop())
  47. keyboardHolder.innerHTML = f
  48.  
  49.  
  50. if(typeof Game != "undefined") {
  51.  
  52. document['addEventListener']('keydown', press);
  53. document['addEventListener']('keyup', press);
  54. function press(e) {
  55. if(~Game['set2ings'].indexOf(e.keyCode)){
  56. var corresponding = [6,8,1,2,3,5,4,0][Game['set2ings'].indexOf(e.keyCode)]
  57. document.getElementsByClassName("kbkey")[corresponding].style.backgroundColor = ["lightgoldenrodyellow",""][+(e.type=="keyup")]
  58. }
  59. }
  60.  
  61.  
  62. var set2ings = Game['prototype']['readyGo'].toString()
  63. set2ings = "Game['set2ings']=this.Settings.controls;" + trim(set2ings)
  64. Game['prototype']['readyGo'] = new Function(set2ings);
  65.  
  66. var updateTextBarFunc = Game['prototype']['updateTextBar'].toString()
  67. updateTextBarFunc = trim(updateTextBarFunc) + ";kps.innerHTML='KPS: '+(this.getKPP()*this.placedBlocks/this.clock).toFixed(2)"
  68. Game['prototype']['updateTextBar'] = new Function(updateTextBarFunc);
  69. } else {
  70.  
  71.  
  72.  
  73. var website = "jstris.jezevec10.com"
  74. var url = window.location.href
  75. var parts = url.split("/")
  76.  
  77. if(parts[3]=="replay" && parts[2].endsWith(website)){
  78.  
  79. fetch("https://"+parts[2]+"/replay/data?id="+ (parts.length==6?(parts[5]+"&live=1"):(parts[4])))
  80. .then(function(response) {
  81. return response.json();
  82. })
  83. .then(function(jsonResponse) {
  84. var das = jsonResponse.c.das
  85. var playT = Replayer['prototype']['playUntilTime'].toString()
  86. var playTparams = getParams(playT);
  87.  
  88.  
  89. var insert1 = `
  90. kps.innerHTML="KPS: "+(this.getKPP()*this.placedBlocks/(this.clock/1000)).toFixed(2)
  91. this["delayedActions"] = []
  92. for (var i = 0; i < this["actions"].length; i++) {
  93. var action = JSON.parse(JSON.stringify(this["actions"][i]));
  94. if(action.a == 2 || action.a == 3){
  95. action.t = (action.t-`+das+`)>0 ? (action.t-`+das+`) : 0
  96. }
  97. this["delayedActions"].push(action)
  98. }
  99.  
  100. this["delayedActions"].sort(function(a, b) {
  101. return a.t - b.t;
  102. });
  103.  
  104. var oldVals = [this["timer"],this["ptr"]]
  105.  
  106. while (`+playTparams[0]+` >= this['delayedActions'][this['ptr']]['t']) {
  107. if (this['ptr']) {
  108. this['timer'] += (this['delayedActions'][this['ptr']]['t'] - this['delayedActions'][this['ptr'] - 1]['t']) / 1000
  109. };
  110. if(this['delayedActions'][this['ptr']]["a"] == 2){
  111. Replayer["pressKey"](6,1)
  112. }
  113. if(this['delayedActions'][this['ptr']]["a"] == 3){
  114. Replayer["pressKey"](8,1)
  115. }
  116.  
  117. this['ptr']++;
  118. if (this['delayedActions']['length'] === this['ptr']) {
  119. this['reachedEnd'] = true;
  120. break
  121. }
  122. };
  123.  
  124. this["timer"] = oldVals[0]
  125. this["ptr"] = oldVals[1]`
  126.  
  127. var insert2 = `
  128. var highlight = [[6,2],[8,2],[6,0],[8,0],[3,2],[5,2],[0,2],[2,2],[1,2],,[4,2]][this['actions'][this['ptr']]["a"]]
  129. if(highlight){
  130. Replayer["pressKey"](...highlight)
  131. };
  132. `
  133.  
  134. playT = playT.replace(";",insert1+";")
  135. playT = playT.replace("1000};","1000};"+insert2)
  136. Replayer['prototype']['playUntilTime'] = new Function(...playTparams, trim(playT));
  137.  
  138.  
  139.  
  140. });
  141. }
  142.  
  143.  
  144. }
  145.  
  146. }
  147.  
  148.  
  149. });
  150. })();