window.addEventListener('load', () => { let loadFlag = false const openSearch = () => { const bodyStyle = document.body.style bodyStyle.width = '100%' bodyStyle.overflow = 'hidden' btf.animateIn(document.getElementById('search-mask'), 'to_show 0.5s') btf.animateIn(document.querySelector('#local-search .search-dialog'), 'titleScale 0.5s') setTimeout(() => { document.querySelector('#local-search-input input').focus() }, 100) if (!loadFlag) { search(GLOBAL_CONFIG.localSearch.path) loadFlag = true } // shortcut: ESC document.addEventListener('keydown', function f (event) { if (event.code === 'Escape') { closeSearch() document.removeEventListener('keydown', f) } }) } const closeSearch = () => { const bodyStyle = document.body.style bodyStyle.width = '' bodyStyle.overflow = '' btf.animateOut(document.querySelector('#local-search .search-dialog'), 'search_close .5s') btf.animateOut(document.getElementById('search-mask'), 'to_hide 0.5s') } // click function const searchClickFn = () => { document.querySelector('#search-button > .search').addEventListener('click', openSearch) document.getElementById('search-mask').addEventListener('click', closeSearch) document.querySelector('#local-search .search-close-button').addEventListener('click', closeSearch) } searchClickFn() // pjax window.addEventListener('pjax:complete', function () { getComputedStyle(document.querySelector('#local-search .search-dialog')).display === 'block' && closeSearch() searchClickFn() }) async function search (path) { let datas = [] const typeF = path.split('.')[1] const response = await fetch(GLOBAL_CONFIG.root + path) if (typeF === 'json') { datas = await response.json() } else if (typeF === 'xml') { const res = await response.text() const t = await new window.DOMParser().parseFromString(res, 'text/xml') const a = await t datas = [...a.querySelectorAll('entry')].map(function (item) { return { title: item.querySelector('title').textContent, content: item.querySelector('content').textContent, url: item.querySelector('url').textContent } }) } if (response.ok) { const $loadDataItem = document.getElementById('loading-database') $loadDataItem.nextElementSibling.style.display = 'block' $loadDataItem.remove() } const $input = document.querySelector('#local-search-input input') const $resultContent = document.getElementById('local-search-results') const $loadingStatus = document.getElementById('loading-status') $input.addEventListener('input', function () { const keywords = this.value.trim().toLowerCase().split(/[\s]+/) if (keywords[0] !== '') $loadingStatus.innerHTML = '' $resultContent.innerHTML = '' let str = '
' + matchContent + '...
' } } str += '