Skip to content

Instantly share code, notes, and snippets.

@amunchet
Last active October 10, 2022 18:50
Show Gist options
  • Save amunchet/39f872f702ab1d394e1089b100f7e04b to your computer and use it in GitHub Desktop.
Save amunchet/39f872f702ab1d394e1089b100f7e04b to your computer and use it in GitHub Desktop.
noVNC Paste Tampermonkey script.
// ==UserScript==
// @name noVNC Paste
// @namespace http://tampermonkey.net/
// @version 0.2a
// @description Pastes text into a noVNC window
// @author You
// @match */console*
// @require http://code.jquery.com/jquery-3.3.1.min.js
// @grant none
// ==/UserScript==
;(function() {
'use strict'
var XK_Shift_L = 0xffe1
// Find and create the copy and paste location
window.paste = function() {
var text = prompt('Please enter text to paste:', '')
sendString(text)
}
window.sendString = function(str) {
f(str.split(''))
function f(t) {
var character = t.shift()
var i = []
var code = character.charCodeAt()
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)
}
if (t.length > 0) {
setTimeout(function() {
f(t)
}, 10)
}
}
}
$(document).ready(function() {
var ctrlDown = false,
ctrlKey = 17,
cmdKey = 91,
vKey = 86,
cKey = 67
$(document)
.keydown(function(e) {
if (e.keyCode == ctrlKey || e.keyCode == cmdKey) ctrlDown = true
})
.keyup(function(e) {
if (e.keyCode == ctrlKey || e.keyCode == cmdKey)
ctrlDown = false
})
$('.no-copy-paste').keydown(function(e) {
if (ctrlDown && (e.keyCode == vKey || e.keyCode == cKey))
return false
})
// Document Ctrl + C/V
$(document).keydown(function(e) {
if (ctrlDown && e.keyCode == vKey)
console.log('Document catch Ctrl+V')
paste()
ctrlDown = false
})
})
$('#noVNC_screen').prepend(
"<div style='padding:20px;'><button onclick='javascript:paste();'>Click to Paste Text</button></div>"
)
})()
@amunchet
Copy link
Author

This is just a small tampermonkey script to add a button at the top of a noVNC session to paste in text to the terminal.

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