From 6e9a3222ce566f6430caad541c2695255cb06d7c Mon Sep 17 00:00:00 2001 From: Devaev Maxim Date: Sun, 9 Sep 2018 20:13:03 +0300 Subject: refactoring --- kvmd/web/js/hid.js | 96 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 49 insertions(+), 47 deletions(-) (limited to 'kvmd') diff --git a/kvmd/web/js/hid.js b/kvmd/web/js/hid.js index 520c4552..ca535157 100644 --- a/kvmd/web/js/hid.js +++ b/kvmd/web/js/hid.js @@ -43,7 +43,7 @@ function Hid() { if (window.navigator.clipboard && window.navigator.clipboard.readText) { __chars_to_codes = __buildCharsToCodes(); - tools.setOnClick($("pak-button"), __pasteAsKeys); + tools.setOnClick($("pak-button"), __pasteAsKeysFromClipboard); } else { $("pak-button").title = $("pak-led").title = "Your browser does not support the Clipboard API.\nUse Google Chrome or Chromium."; } @@ -131,53 +131,55 @@ function Hid() { return chars_to_codes; }; - var __pasteAsKeys = function() { - window.navigator.clipboard.readText().then(function(clipboard_text) { - clipboard_text = clipboard_text.replace(/[^\x00-\x7F]/g, ""); // eslint-disable-line no-control-regex - if (clipboard_text) { - var clipboard_codes = []; - var codes_count = 0; - [...clipboard_text].forEach(function(ch) { - var codes = __chars_to_codes[ch]; - if (codes) { - codes_count += codes.length; - clipboard_codes.push(codes); - } - }); + var __pasteAsKeysFromClipboard = function() { + window.navigator.clipboard.readText().then(__pasteAsKeys); + }; - var confirm_msg = ( - "You are going to automatically type " + codes_count - + " characters from the system clipboard." - + " It will take " + (__codes_delay * codes_count * 2 / 1000) + " seconds.
" - + "
Are you sure you want to continue?
" - ); - - ui.confirm(confirm_msg).then(function(ok) { - if (ok) { - $("pak-button").disabled = true; - $("pak-led").className = "led-pak-typing"; - $("pak-led").title = "Autotyping..."; - - tools.debug("Paste-as-keys:", clipboard_text); - - var index = 0; - var iterate = function() { - __emitShortcut(clipboard_codes[index]).then(function() { - ++index; - if (index < clipboard_codes.length && __ws) { - iterate(); - } else { - $("pak-button").disabled = false; - $("pak-led").className = "led-off"; - $("pak-led").title = ""; - } - }); - }; - iterate(); - } - }); - } - }); + var __pasteAsKeys = function(text) { + text = text.replace(/[^\x00-\x7F]/g, ""); // eslint-disable-line no-control-regex + if (text) { + var clipboard_codes = []; + var codes_count = 0; + [...text].forEach(function(ch) { + var codes = __chars_to_codes[ch]; + if (codes) { + codes_count += codes.length; + clipboard_codes.push(codes); + } + }); + + var confirm_msg = ( + "You are going to automatically type " + codes_count + + " characters from the system clipboard." + + " It will take " + (__codes_delay * codes_count * 2 / 1000) + " seconds.
" + + "
Are you sure you want to continue?
" + ); + + ui.confirm(confirm_msg).then(function(ok) { + if (ok) { + $("pak-button").disabled = true; + $("pak-led").className = "led-pak-typing"; + $("pak-led").title = "Autotyping..."; + + tools.debug("Paste-as-keys:", text); + + var index = 0; + var iterate = function() { + __emitShortcut(clipboard_codes[index]).then(function() { + ++index; + if (index < clipboard_codes.length && __ws) { + iterate(); + } else { + $("pak-button").disabled = false; + $("pak-led").className = "led-off"; + $("pak-led").title = ""; + } + }); + }; + iterate(); + } + }); + } }; __init__(); -- cgit v1.2.3