Skip to content

Instantly share code, notes, and snippets.

@sscotth
Forked from amunchet/noVNCCopyPasteProxmox.user.js
Last active October 7, 2023 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sscotth/71895aca82af4c7a18a97b04259f19d9 to your computer and use it in GitHub Desktop.
Save sscotth/71895aca82af4c7a18a97b04259f19d9 to your computer and use it in GitHub Desktop.
Copy/Paste for noVNC Proxmox
// ==UserScript==
// @name noVNC Paste for Proxmox
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Pastes text into a noVNC window (for use with Proxmox specifically)
// @author Chester Enright
// @match https://*
// @include /^.*novnc.*/
// @require http://code.jquery.com/jquery-3.3.1.min.js
// @grant none
// ==/UserScript==
const startDelay = 2000
const keyDelay = 50
;(function () {
'use strict'
window.sendString = function(text) {
var el = document.getElementById("canvas-id")
text.split("").forEach((x,i)=>{
setTimeout(()=>{
var 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)
}, keyDelay * (i + 1))
})
}
$(document).ready(function() {
setTimeout(()=>{
console.log("Starting up noVNC Copy/Paste (for Proxmox)")
$("canvas").attr("id", "canvas-id")
$("canvas").on("mousedown", (e) => {
console.log(e.button, e.detail)
if(e.button == 0 && e.detail === 4){ // Quad Click
setTimeout(() => {
navigator.clipboard.readText().then(text =>{
console.log('sendString', text)
window.sendString(text)
})
}, startDelay)
}
})
}, 1000);
})
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment