File size: 2,061 Bytes
2d8be8f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!doctype html>
<html>
  <h1>Send a message to a window using its label:</h1>
  <form id="send-message-form">
    <input id="send-message" placeholder="message" />
    <input id="send-label" placeholder="Secondary" />
    <button type="submit">Send</button>
  </form>

  <br />

  <h1>Create new window</h1>
  <form id="new-window-form">
    <input id="new-label" placeholder="newLabel" />
    <input id="new-title" placeholder="New window" />
    <button type="submit">Create</button>
  </form>

  <br />

  <h1>Messages received from other windows:</h1>
  <pre id="messages-view"></pre>

  <body>
    <script>
      const { WebviewWindow } = window.__TAURI__.webviewWindow
      const { getCurrentWebviewWindow } = window.__TAURI__.webviewWindow
      const { emitTo } = window.__TAURI__.event

      const sendMessageForm = document.querySelector('#send-message-form')
      const sendMessageEl = document.querySelector('#send-message')
      const sendLabelEl = document.querySelector('#send-label')
      sendMessageForm.addEventListener('submit', (e) => {
        e.preventDefault()
        console.log(sendLabelEl.value)
        console.log(sendMessageEl.value)

        emitTo(sendLabelEl.value, 'message', sendMessageEl.value)
      })

      const newWindowForm = document.querySelector('#new-window-form')
      const newLabelEl = document.querySelector('#new-label')
      const newTitleEl = document.querySelector('#new-title')
      newWindowForm.addEventListener('submit', (e) => {
        e.preventDefault()

        new WebviewWindow(newLabelEl.value, {
          title: newTitleEl.value
        })
      })

      const currentWindow = getCurrentWebviewWindow()
      const messagesView = document.querySelector('#messages-view')
      window.addEventListener('DOMContentLoaded', () => {
        currentWindow.listen('message', (event) => {
          const time = new Date().toLocaleTimeString()
          messagesView.textContent = `${messagesView.textContent}\n[${time}] ${event.payload}`
        })
      })
    </script>
  </body>
</html>