Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ZeroDeth/36efa5989035b3a2be4b9489bbe1aedb to your computer and use it in GitHub Desktop.
Save ZeroDeth/36efa5989035b3a2be4b9489bbe1aedb to your computer and use it in GitHub Desktop.
Copy/Paste for noVNC Proxmox
// ==UserScript==
// @name noVNC Paste for Proxmox (Mac UK Friendly)
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Pastes text into a noVNC window for Proxmox – Mac British keyboard safe version
// @author Sherif & ChatGPT
// @match https://*
// @include /^.*novnc.*/
// @require http://code.jquery.com/jquery-3.3.1.min.js
// @grant none
// ==/UserScript==
const delay = 50; // slight delay to make sure keys land correctly
(function () {
'use strict';
window.sendString = function(text) {
const el = document.getElementById("canvas-id");
if (!el) {
console.error("Canvas element not found.");
return;
}
let i = 0;
function typeNext() {
if (i >= text.length) return;
const char = text[i];
const isUpper = char === char.toUpperCase() && char.toLowerCase() !== char;
if (isUpper) {
el.dispatchEvent(new KeyboardEvent('keydown', { key: 'Shift', code: 'ShiftLeft', shiftKey: true }));
}
// Use keypress to deliver the actual character
el.dispatchEvent(new KeyboardEvent('keydown', { key: char, code: char, shiftKey: isUpper }));
el.dispatchEvent(new KeyboardEvent('keypress', { key: char, code: char, shiftKey: isUpper }));
el.dispatchEvent(new KeyboardEvent('keyup', { key: char, code: char, shiftKey: isUpper }));
if (isUpper) {
el.dispatchEvent(new KeyboardEvent('keyup', { key: 'Shift', code: 'ShiftLeft' }));
}
i++;
setTimeout(typeNext, delay);
}
typeNext();
};
$(document).ready(function () {
setTimeout(() => {
console.log("noVNC Paste (Mac UK layout) initialised");
$("canvas").attr("id", "canvas-id");
$("canvas").on("mousedown", (e) => {
if (e.button === 2) { // Right-click
navigator.clipboard.readText().then(text => {
window.sendString(text);
});
}
});
}, 1000);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment