(function() { const wsProtocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; const wsHost = window.location.hostname; const wsPort = window.location.port; const urlParams = new URLSearchParams(window.location.search); const password = urlParams.get('password'); const wsUrlBase = wsPort ? `${wsProtocol}//${wsHost}:${wsPort}/ws` : `${wsProtocol}//${wsHost}/ws`; const WS_URL = password ? `${wsUrlBase}?password=${encodeURIComponent(password)}` : wsUrlBase; let videoPlayer = null; let inputHandler = null; function init() { videoPlayer = new VideoPlayer('video-canvas', WS_URL); videoPlayer.init(); inputHandler = new InputHandler(videoPlayer.getCanvas(), { wsUrl: WS_URL }); inputHandler.init(); console.log('xc-remote initialized'); } function destroy() { if (inputHandler) { inputHandler.destroy(); inputHandler = null; } if (videoPlayer) { videoPlayer.destroy(); videoPlayer = null; } } window.addEventListener('beforeunload', destroy); if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); } else { init(); } })();