This code provides a function vncIframe
that generates an HTML snippet embedding a VNC viewer iframe, allowing you to easily integrate VNC viewing into your web application. It takes optional connection parameters and dynamically constructs the iframe's source URL with encoded options.
The vncIframe
function generates an HTML snippet embedding a VNC viewer iframe, dynamically constructing the iframe's source URL based on provided connection parameters and encoding them for URL safety.
npm run import -- "connect to VNC through the web browser"
function urlEncode(obj) {
return Object.keys(obj).map(k => encodeURIComponent(k) + '=' + encodeURIComponent(obj[k])).join('&');
}
var vncIframe = (options = {
password: 'secret',
host: 'localhost',
port: 6080,
autoconnect: true,
resize: 'scale',
view_only: true,
reconnect: true
}) => {
console.log(`//localhost:6080/vnc.html?${urlEncode(options)}`);
return `
<div style="display:block; width:100%; padding-bottom:69%;position:relative;">
<iframe id="vnc"
style="position:absolute;top:0;right:0;bottom:0;left:0;width:100%;height:100%;border:0;"
src="//localhost:6080/vnc.html?${urlEncode(options)}"></iframe></div>
`;
}
module.exports = vncIframe;
if(typeof $ !== 'undefined') {
$.mime({'text/html': vncIframe()});
}
// Function to URL encode an object
const urlEncode = (obj) => {
// Use Object.entries() and map() to create an array of encoded key-value pairs
return Object.entries(obj).map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`).join('&');
}
// Function to generate a VNC iframe
const vncIframe = ({
password ='secret', // Default password
host = 'localhost', // Default host
port = 6080, // Default port
autoconnect = true, // Default autoconnect
resize ='scale', // Default resize behavior
view_only = true, // Default view-only mode
reconnect = true, // Default reconnect behavior
}) => {
// Log the URL for debugging purposes
console.log(`//localhost:6080/vnc.html?${urlEncode({...options })}`);
// Return the iframe HTML
return `
`;
}
// Export the vncIframe function
module.exports = vncIframe;
// If $ is defined, render the iframe as HTML
if (typeof $!== 'undefined') {
$.mime({ 'text/html': vncIframe({ /* Example options */ password: 'example', host: 'example.com' }) });
}
This code defines a function vncIframe
that generates an HTML snippet embedding a VNC viewer iframe.
Here's a breakdown:
urlEncode
Function:
encodeURIComponent
to create URL-safe query parameters.&
to form a URL query string.vncIframe
Function:
options
object with VNC connection parameters (host, port, password, etc.).//localhost:6080/vnc.html
) with the encoded options appended as query parameters.src
attribute points to the constructed VNC viewer URL.Module Export:
vncIframe
function as the module's main export.Conditional Execution:
$
exists (likely a framework or library).$.mime
to register the vncIframe
function as a MIME type handler for HTML content.