Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// This will open up a prompt for text to send to a console session on digital ocean
// Useful for long passwords
(function () {
var t = prompt("Enter text to be sent to console, (This wont send the enter keystroke)").split("");
function f() {
var character = t.shift();
var i=[];
var code = character.charCodeAt();
var needs_shift = "!@#$%^&*()_+{}:\"<>?~|".indexOf(character) !== -1
var shift = XK_Shift_L; // To help with minification
function key(keycode, down) {
i=i.concat(RFB.messages.keyEvent(keycode, down));
}
if (needs_shift) {
key(shift,1);
}
key(code,1);
key(code,0);
if (needs_shift) {
key(shift,0);
}
rfb._sock.send(i);
if (t.length > 0) {
setTimeout(f, 10);
}
}
f();
})();
// Minified version:
!function(){function t(){function n(t,e){s=s.concat(RFB.messages.keyEvent(t,e))}var o=e.shift(),s=[],i=o.charCodeAt(),c=-1!=='!@#$%^&*()_+{}:"<>?~|'.indexOf(o),r=XK_Shift_L;c&&n(r,1),n(i,1),n(i,0),c&&n(r,0),rfb._sock.send(s),e.length>0&&setTimeout(t,10)}var e=prompt("Enter text to be sent to console, (This wont send the enter keystroke)").split("");t()}();
// This is an alternate version that will open a box on ctrl+shift+v or command+shift+v
// this version might work better on browsers that block prompt from console
(function(){
var shift_down = false;
var ctrl_down = false;
var super_down = false;
var v_down = false;
$(document).keydown(function(e) {
if (e.which == 17) { ctrl_down = true; }
else if (e.which == 16) { shift_down = true; }
else if (e.which == 91) { super_down = true; }
else if (e.which == 86) { v_down = true; }
if (v_down && shift_down && (super_down || ctrl_down) ) {
ctrl_down = false;
shift_down = false;
super_down = false;
var t = prompt("Enter text to be sent to console, (This wont send the enter keystroke)").split("");
function f() {
var character = t.shift();
var i=[];
var code = character.charCodeAt();
var needs_shift = "!@#$%^&*()_+{}:\"<>?~|".indexOf(character) !== -1
var shift = XK_Shift_L; // To help with minification
function key(keycode, down) {
i=i.concat(RFB.messages.keyEvent(keycode, down));
}
if (needs_shift) {
key(shift,1);
}
key(code,1);
key(code,0);
if (needs_shift) {
key(shift,0);
}
rfb._sock.send(i);
if (t.length > 0) {
setTimeout(f, 10);
}
}
setTimeout(f, 10);
}
});
$(document).keyup(function(e) {
if (e.which == 17) { ctrl_down = false; }
else if (e.which == 16) { shift_down = false; }
else if (e.which == 91) { super_down = false; }
else if (e.which == 86) { v_down = false; }
});
})();
// Minified version:
(function(){var d=!1,b=!1,c=!1,f=!1;$(document).keydown(function(a){17==a.which?b=!0:16==a.which?d=!0:91==a.which?c=!0:86==a.which&&(f=!0);if(f&&d&&(c||b)){c=d=b=!1;var g=prompt("Enter text to be sent to console, (This wont send the enter keystroke)").split(""),h=function(){function a(b,c){d=d.concat(RFB.messages.keyEvent(b,c))}var e=g.shift(),d=[],b=e.charCodeAt(),e=-1!=='!@#$%^&*()_+{}:"<>?~|'.indexOf(e),c=XK_Shift_L;e&&a(c,1);a(b,1);a(b,0);e&&a(c,0);rfb._sock.send(d);0<g.length&&setTimeout(h,10)};
setTimeout(h,10)}});$(document).keyup(function(a){17==a.which?b=!1:16==a.which?d=!1:91==a.which?c=!1:86==a.which&&(f=!1)})})();
@croepha
Copy link
Author

croepha commented Dec 4, 2015

Strange, Thanks, I'll check it out

@kevin1
Copy link

kevin1 commented Dec 5, 2015

I think this is because that character requires the "shift" key -- for example, ! becomes 1

@croepha
Copy link
Author

croepha commented Dec 7, 2015

kevin1, your right.

I updated the code to handle those characters

@warthurton
Copy link

warthurton commented Feb 22, 2017

Works great! Thank you!

@adl1995
Copy link

adl1995 commented Mar 11, 2017

Worked like a charm. Is there any way to make this work with a text editor like vi?

@oldo
Copy link

oldo commented Apr 26, 2017

Wow, that's very nice!

@adl1995 it does work - just be sure to be in insert mode.

@watzon
Copy link

watzon commented May 27, 2017

I'm sure this isn't perfect, but I encountered an error while trying to use this to paste into vim. What happened is that vim would reverse the capitalization of each letter, so "hEllO" would become HeLLo". My update fixes that problem.

(function () {        
        var t = prompt("Enter text to be sent to console, (This wont send the enter keystroke)").split("");
        var o = [];
        
        for (let letter of t) {
          if (letter.match(/[a-z]/)) {
            letter = letter.toUpperCase();
          } else if (letter.match(/[A-Z]/)) {
            letter = letter.toLowerCase();
          }
          o.push(letter);
          console.log(letter);
        }
        
        function f() {
                
                var character = o.shift();
                var i=[];
                var code = character.charCodeAt();
                var needs_shift = "!@#$%^&*()_+{}:\"<>?~|".indexOf(character) !== -1
                var shift = XK_Shift_L; // To help with minification
                function key(keycode, down) {
                        i=i.concat(RFB.messages.keyEvent(keycode, down));
                }
                
                if (needs_shift) {
                        key(shift,1);
                }
                key(code,1);
                key(code,0);
                if (needs_shift) {
                        key(shift,0);
                }
                rfb._sock.send(i);
                
                if (o.length > 0) {
                        setTimeout(f, 10);
                }
        }
        f();
})();

@mayureshhedaoo
Copy link

mayureshhedaoo commented Jul 4, 2017

Its working.....Thanks for this help!!!!

@mreis1
Copy link

mreis1 commented Jul 8, 2017

is anyone having issues this warning in chrome while running the script?

A window.prompt() dialog generated by this page was suppressed because this page is not the active tab of the front window. Please make sure your dialogs are triggered by user interactions to avoid this situation https://www.chromestatus.com/feature/5637107137642496

@croepha
Copy link
Author

croepha commented Jul 9, 2017

@mreis1: I added another version that might work better for chrome's blocking of window.prompt

@giovannibenussi
Copy link

giovannibenussi commented Aug 25, 2017

@mreis1: I had the same error and the solution is to open the console in a chrome tab instead of the pop up (you can copy the url and paste it in a new tab)

@byjg
Copy link

byjg commented Oct 12, 2017

Great script!! It save my life!! I made some changes on the script to work on another host provide. The new version is here:
https://gist.github.com/byjg/a6378edb420a1c654c5f27bb494ca1c8

@andrehrferreira
Copy link

andrehrferreira commented Oct 20, 2017

@RowdyCoder
Copy link

RowdyCoder commented Oct 29, 2017

So Awesome. Thanks for this!

@Treeless
Copy link

Treeless commented Nov 3, 2017

THANK YOU SOOOOOOO MUCH <3 I started typing out my ssh key...

Copy link

ghost commented Nov 27, 2017

Wow. <3 I love you so much right now.

@north27
Copy link

north27 commented Dec 4, 2017

On the latest version of Chrome (at the time of writing) I get the following error:
Package is invalid. Details: 'Could not load background page 'background.html'.'.

@wfilho
Copy link

wfilho commented Jan 12, 2018

Thanks for sharing !

I cannot add chrome extension. It says that cannot add background.html.

@vgrch
Copy link

vgrch commented Feb 1, 2018

Respect! thanks for sharing.

@loeffel-io
Copy link

loeffel-io commented Jun 11, 2018

haha nice! 👍

@jjaaccoobb
Copy link

jjaaccoobb commented Jul 3, 2018

thank you so much

@vaxoiva
Copy link

vaxoiva commented Jul 20, 2018

hi

how about |

@SmartWeb25
Copy link

SmartWeb25 commented Jul 24, 2018

it changes ssh key letters to strange format.
for example = changed to +, date number to _@!*)&@$

@RIAEvangelist
Copy link

RIAEvangelist commented Dec 5, 2018

Doesnt work anymore

@ssivak0909
Copy link

ssivak0909 commented Mar 14, 2020

kevin1, your right.

I updated the code to handle those characters

where i have to update this code

@shadyshrif
Copy link

shadyshrif commented Apr 19, 2020

It isn't working!

@kainjinez
Copy link

kainjinez commented Oct 22, 2020

Not working anymore.

@elmehalawi
Copy link

elmehalawi commented Nov 30, 2020

Thank you for this, it works perfectly. I also ran into the same issue where the console kind of trips over itself and everything is capitalized. When I set the timeout to 100 instead of 10 (on line 27), I don't run into this issue, at least as frequently. It's a bit annoying to wait for the text to enter slowly, but I won't need to do it often so idc.

@scsskid
Copy link

scsskid commented Apr 21, 2021

thanks all! definitely works for me, I also had to increase the timeout, as elmehalawi mentioned, I used Chrome

@jaydenireland
Copy link

jaydenireland commented Jul 19, 2021

If characters are sent in the wrong order, watzon's script and increasing the timeout. This worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment