Skip to content

Instantly share code, notes, and snippets.

@amunchet
Last active May 2, 2024 18:49
Show Gist options
  • Star 69 You must be signed in to star a gist
  • Fork 22 You must be signed in to fork a gist
  • Save amunchet/4cfaf0274f3d238946f9f8f94fa9ee02 to your computer and use it in GitHub Desktop.
Save amunchet/4cfaf0274f3d238946f9f8f94fa9ee02 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.2a
// @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 delay = 1
;(function () {
'use strict'
window.sendString = function(text) {
var el = document.getElementById("canvas-id")
text.split("").forEach(x=>{
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)
}, delay)
})
}
$(document).ready(function() {
setTimeout(()=>{
console.log("Starting up noVNC Copy/Paste (for Proxmox)")
$("canvas").attr("id", "canvas-id")
$("canvas").on("mousedown", (e)=>{
if(e.button == 2){ // Right Click
navigator.clipboard.readText().then(text =>{
window.sendString(text)
})
}
})
}, 1000);
})
})()
@oreganmike
Copy link

Absolute legend. Thanks.

@flightless22
Copy link

flightless22 commented Jan 2, 2024

For anyone interested, I removed the clipboard dependency by changing:

                if(e.button == 2){ // Right Click
                    navigator.clipboard.readText().then(text =>{
                        window.sendString(text);
                    })
                }

Into:

                if(e.button == 2){ // Right Click
                    let text = prompt("Enter text to paste:");
                    if (text != null) window.sendString(text);
                }

That way, you can paste the text into the native JS prompt.

I got errors with the original script. it needs to check if clipboard access is present and than use prompt or other method as work around. otherwise now it's perfect. almost. I also encountered the same issue with text getting cut off at the end with really long text. I'm using mint / firefox / violentmonkey

@dan3805
Copy link

dan3805 commented Mar 23, 2024

Wow! Thank you so much! This script is really nice!

@CrazyWolf13
Copy link

(I actually took German in Jr. High, so it's not that odd to me :)....not as weird as like the Japanese layout).

Couple of thoughts:

  • The Alt Gr needs to have the shift style applied (like in the above example)
  • You'll probably need to just swap Z and Y in the javascript. This can be done like this (probably can be done better):
  x = x.split('').map(function(c) {
    if (c === 'z') {
      return 'y';
    } else if (c === 'y') {
      return 'z';
    } else {
      return c;
    }
  }).join('');
  • What keys do the Umlauts (U, O, and A) appear as? You will probably need to do some kind of swap for them as well.

This script's basic operating principle is to take a string and then pretend to type it into Proxmox via Javascript from the browser. I don't have a deep understanding into how the layouts for each browser work - there may be a way to adjust the internal key mappings that I'm not aware of. KeyboardEvent is the crux of the idea - maybe there is some documentation on how that works for international layouts.

There may also be a way to temporarily switch your keyboard layout to US-en for the duration of the paste, then switch it immediately back. I think the internationalization issues all stem from trying to type letters into an international layout and emulating those keycodes.

Hi
Any updates on this?
I'd really love such a feature, as with the current code I can barely even use it.

Edit:
Swiss Keyboard Layout is even better:
image

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