remote-login.js 1.1 KB

1234567891011121314151617181920212223
  1. const byId = id => document.getElementById(id)
  2. const form = byId('login-form')
  3. const preset = new URLSearchParams(location.search).get('server')
  4. if (preset) byId('server').value = preset
  5. form.addEventListener('submit', async event => {
  6. event.preventDefault()
  7. const error = byId('error'), status = byId('status'), submit = byId('submit')
  8. error.hidden = true; status.hidden = false; status.textContent = '正在登录…'; submit.disabled = true
  9. try {
  10. const result = await window.yaoyaoRemoteLogin.submit({
  11. serverURL: byId('server').value,
  12. username: byId('username').value,
  13. password: byId('password').value,
  14. })
  15. if (!result.ok) throw new Error(result.error || '登录失败')
  16. status.textContent = result.hostRegistered ? '登录成功,已注册本机为远程桌面主机…' : '登录成功…'
  17. } catch (cause) {
  18. status.hidden = true; error.hidden = false
  19. error.textContent = cause instanceof Error ? cause.message : '登录失败'
  20. submit.disabled = false
  21. }
  22. })
  23. byId('use-local').addEventListener('click', () => { void window.yaoyaoRemoteLogin.useLocal() })