/ app / ui / script.js
script.js
  1  const DEFAULT_PAGE = 'agregore://welcome'
  2  
  3  const webview = $('#view')
  4  // Kyran: Using variable name "top" causes issues for some reason? I would assume it's because of another one of the UI scripts but it doesn't seem like that's the case.
  5  const nav = $('#top')
  6  const search = $('#search')
  7  const find = $('#find')
  8  const actions = $('#actions')
  9  
 10  const currentWindow = window.getCurrentWindow()
 11  
 12  const pageTitle = $('title')
 13  
 14  const searchParams = new URL(window.location.href).searchParams
 15  
 16  const toNavigate = searchParams.has('url') ? searchParams.get('url') : DEFAULT_PAGE
 17  
 18  const rawFrame = searchParams.get('rawFrame') === 'true'
 19  const noNav = searchParams.get('noNav') === 'true'
 20  
 21  if (rawFrame) nav.classList.toggle('hidden', true)
 22  
 23  window.addEventListener('load', () => {
 24    if (noNav) return
 25    console.log('toNavigate', toNavigate)
 26    currentWindow.loadURL(toNavigate)
 27    webview.emitResize()
 28  })
 29  
 30  search.addEventListener('back', () => {
 31    currentWindow.goBack()
 32  })
 33  
 34  search.addEventListener('forward', () => {
 35    currentWindow.goForward()
 36  })
 37  
 38  search.addEventListener('navigate', ({ detail }) => {
 39    const { url } = detail
 40  
 41    navigateTo(url)
 42  })
 43  
 44  search.addEventListener('unfocus', async () => {
 45    await currentWindow.focus()
 46    search.src = await currentWindow.getURL()
 47  })
 48  
 49  search.addEventListener('search', async ({ detail }) => {
 50    const { query, searchID } = detail
 51  
 52    const results = await currentWindow.searchHistory(query, searchID)
 53  
 54    search.setSearchResults(results, query, searchID)
 55  })
 56  
 57  webview.addEventListener('focus', () => {
 58    currentWindow.focus()
 59  })
 60  
 61  webview.addEventListener('resize', ({ detail: rect }) => {
 62    currentWindow.setBounds(rect)
 63  })
 64  
 65  currentWindow.on('navigating', (url) => {
 66    search.src = url
 67  })
 68  
 69  currentWindow.on('history-buttons-change', updateButtons)
 70  
 71  currentWindow.on('page-title-updated', (title) => {
 72    pageTitle.innerText = title + ' - Agregore Browser'
 73  })
 74  currentWindow.on('enter-html-full-screen', () => {
 75    if (!rawFrame) nav.classList.toggle('hidden', true)
 76  })
 77  currentWindow.on('leave-html-full-screen', () => {
 78    if (!rawFrame) nav.classList.toggle('hidden', false)
 79  })
 80  currentWindow.on('update-target-url', async (url) => {
 81    search.showTarget(url)
 82  })
 83  currentWindow.on('browser-actions-changed', () => {
 84    actions.renderLatest()
 85  })
 86  
 87  find.addEventListener('next', ({ detail }) => {
 88    const { value, findNext } = detail
 89  
 90    currentWindow.findInPage(value, { findNext })
 91  })
 92  
 93  find.addEventListener('previous', ({ detail }) => {
 94    const { value, findNext } = detail
 95  
 96    currentWindow.findInPage(value, { forward: false, findNext })
 97  })
 98  
 99  find.addEventListener('hide', () => {
100    currentWindow.stopFindInPage('clearSelection')
101  })
102  
103  function updateButtons ({ canGoBack, canGoForward }) {
104    search.setAttribute('back', canGoBack ? 'visible' : 'hidden')
105    search.setAttribute('forward', canGoForward ? 'visible' : 'hidden')
106  }
107  
108  function $ (query) {
109    return document.querySelector(query)
110  }
111  
112  async function navigateTo (url) {
113    const currentURL = await currentWindow.getURL()
114    if (currentURL === url) {
115      console.log('Reloading')
116      currentWindow.reload()
117    } else {
118      currentWindow.loadURL(url)
119      currentWindow.focus()
120    }
121  }