-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpaste-tool.js
160 lines (137 loc) · 4.4 KB
/
paste-tool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Using this script:
// * Right click your grey gutters in the KVM console, then click 'Inspect`.
// * Go to the 'Console' tab.
// * Paste in the below function.
// * DO NOT use the keyboard to paste - use mouse. Note that mouse only works
// when right clicking if the text area is over the grey gutters.
// * Click paste.
//
// Note that there currently isn't a stop button once the pasting starts, you
// may abort pasting by closing the KVM tab.
(function() {
var input = '';
var delay = '100';
var index = -1;
var mainDivId = '00novnccustompastetool0947-maindiv';
var textId = '00novnccustompastetool0947-textinput';
var numId = '00novnccustompastetool0947-numinput';
// var textAreaDivInstance;
function updateUserInput(event) {
event.preventDefault();
// As it turns out, simulating text input is a huge amount of work.
// Completely disabling all input for now.
//
// switch (event.key) {
// case 'Alt':
// case 'Backspace':
// case 'Control':
// case 'Enter':
// case 'Shift':
// return;
// }
//
// if (!textAreaDivInstance) {
// textAreaDivInstance = document.getElementById(textId);
// }
// textAreaDivInstance.value += event.key;
}
function paste(event) {
event.preventDefault();
// Prep user input.
var userInput = document.getElementById(textId);
input = userInput.value;
// Prep delay.
var userNumInput = document.getElementById(numId);
delay = Number(userNumInput.value);
if (!delay) {
alert('Cannot proceed: delay number is invalid.');
return close();
}
// Close modal.
close(event);
// Wait a bit before starting, allows us to see text pasted if at top of
// screen.
setTimeout(tick, 500);
}
function close(event) {
event.preventDefault();
var mainDiv = document.getElementById(mainDivId);
document.body.removeChild(mainDiv);
}
function showGui() {
var div = document.createElement('div');
div.id = mainDivId;
div.style.position = 'fixed';
div.style.zIndex = '9999999999';
div.style.backgroundColor = 'rgb(221 221 221 / 90%)';
div.style.top = '0';
div.style.left = '0';
div.style.right = '0';
div.style.padding = '8px';
div.style.height = '168px';
div.innerHTML = '<b>Paste your text</b><br>' +
'Note: Use your mouse for copying and pasting because the KVM console ' +
'snatches all input and this is the only sane work-around.' +
'<br>';
var textInput = document.createElement('textarea');
textInput.id = textId;
textInput.onkeydown = updateUserInput;
textInput.style.display = 'block';
textInput.style.width = '100%';
textInput.style.marginBottom = '8px';
var inputDesc = document.createElement('label');
inputDesc.innerHTML = 'Per-key delay in ms: ';
inputDesc.title = 'Sending keys too fast can cause issues (such as ' +
'skipped keys); the delay helps alleviate this.';
var numInput = document.createElement('input');
numInput.title = inputDesc.title;
numInput.setAttribute('type', 'number');
numInput.setAttribute('min', '1');
numInput.setAttribute('max', '100000');
numInput.value = delay;
numInput.id = numId;
var ok = document.createElement('button');
ok.innerText = 'Paste';
ok.onclick = paste;
ok.style.margin = '8px';
var cancel = document.createElement('button');
cancel.innerText = 'Cancel';
cancel.onclick = close;
div.append(textInput);
div.append(inputDesc);
div.append(numInput);
div.append(document.createElement('br'));
div.append(cancel);
div.append(ok);
document.body.append(div);
}
function encodeAndSendKey(character) {
// This part taken from: https://gist.github.com/byjg/a6378edb420a1c654c5f27bb494ca1c8
var code = character.charCodeAt();
if (code === '\r'.charCodeAt()) {
return;
}
if (code === '\n'.charCodeAt()) {
rfb.sendKey(XK_Return, 1);
rfb.sendKey(XK_Return, 0);
return;
}
var needs_shift = character.match(/[A-Z!@#$%^&*()_+{}:\"<>?~|]/);
if (needs_shift) {
rfb.sendKey(XK_Shift_L, 1);
}
rfb.sendKey(code, 1);
rfb.sendKey(code, 0);
if (needs_shift) {
rfb.sendKey(XK_Shift_L, 0);
}
}
function tick() {
if (++index >= input.length) {
return;
}
encodeAndSendKey(input[index]);
setTimeout(tick, delay);
}
showGui();
})();