Skip to content

Instantly share code, notes, and snippets.

@PJB3005
Forked from amunchet/noVNCCopyPasteProxmox.user.js
Last active April 21, 2023 12:09
Show Gist options
  • Save PJB3005/fa1b3278b7d06712ca5de512717c3ddb to your computer and use it in GitHub Desktop.
Save PJB3005/fa1b3278b7d06712ca5de512717c3ddb to your computer and use it in GitHub Desktop.
noVNC Paste as keyboard
// ==UserScript==
// @name noVNC Paste as keyboard
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Pastes text into a noVNC window by typing key binds. Useful if you need to dump an SSH public key into a system over only VNC or something. Paste the text to be pasted into NoVNC's clipboard window, then activate the script paste button
// @author Chester Enright, Pieter-Jan Briers
// @match https://console.cp4staging.be/*
// @match https://console.level27.eu/*
// @grant GM_registerMenuCommand
// @grant GM_log
// ==/UserScript==
const delay = 1;
GM_log("noVNC Paste as keyboard");
GM_registerMenuCommand("Paste", function(event) {
let text = document.getElementById("noVNC_clipboard_text").value.trim();
let el = document.querySelector("canvas");
setTimeout(() => {
for (let i = 0; i < text.length; i++) {
let x = text[i];
let needs_shift = x.match(/[A-Z!@#$%^&*()_+{}:\"<>?~|]/);
let evt;
if (needs_shift) {
evt = new KeyboardEvent("keydown", {keyCode: 16});
el.dispatchEvent(evt);
evt = new KeyboardEvent("keydown", {key: x, shiftKey: true});
el.dispatchEvent(evt);
evt = new KeyboardEvent("keyup", {keyCode: 16});
el.dispatchEvent(evt);
} else {
evt = new KeyboardEvent("keydown", {key: x});
}
el.dispatchEvent(evt)
}
}, delay)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment