|
|
|
const socketProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; |
|
const socketUrl = `${socketProtocol}//${window.location.host}`; |
|
const socket = new WebSocket(socketUrl); |
|
|
|
socket.onmessage = (event) => { |
|
term.write(event.data); |
|
|
|
} |
|
|
|
var term = new window.Terminal({ |
|
cursorBlink: true |
|
}); |
|
term.open(document.getElementById('terminal')); |
|
|
|
function init() { |
|
if (term._initialized) { |
|
return; |
|
} |
|
|
|
term._initialized = true; |
|
|
|
term.prompt = () => { |
|
runCommand('\n'); |
|
}; |
|
setTimeout(() => { |
|
term.prompt(); |
|
}, 300); |
|
|
|
term.onKey(keyObj => { |
|
runCommand(keyObj.key); |
|
}); |
|
|
|
term.attachCustomKeyEventHandler((e) => { |
|
if ((e.ctrlKey || e.metaKey) && e.key === 'v') { |
|
navigator.clipboard.readText().then(text => { |
|
runCommand(text); |
|
}); |
|
return false; |
|
} |
|
return true; |
|
}); |
|
} |
|
|
|
function runCommand(command) { |
|
socket.send(command); |
|
|
|
} |
|
|
|
init(); |