|
const sha = '66ed4d0fe4143366c56f12a12737aba1030bbe20' |
|
|
|
const body = document.querySelector('body'), |
|
footer = document.querySelector('footer p:nth-child(2)'), |
|
search = document.querySelector('input#search'), |
|
section = document.querySelector('section[class^="layout"]') |
|
|
|
/** |
|
* When the window is no longer active, after an ms delay, execute cb |
|
* @param {integer} ms |
|
* @param {function} cb |
|
*/ |
|
const onblur = (ms, cb) => { |
|
let wait |
|
|
|
window.onblur = () => (wait = setTimeout(cb, ms)) |
|
window.onfocus = () => { |
|
body.style.opacity = 1 |
|
clearTimeout(wait) |
|
} |
|
} |
|
|
|
const Cookie = { |
|
get: name => { |
|
let nameEQ = name + '=', |
|
ca = document.cookie.split(';') |
|
|
|
for (let i = 0; i < ca.length; i++) { |
|
let c = ca[i] |
|
|
|
while (c.charAt(0) === ' ') c = c.substring(1, c.length) |
|
if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length) |
|
} |
|
|
|
return null |
|
}, |
|
set: (name, value, days) => { |
|
let expires = '' |
|
|
|
if (days) { |
|
let date = new Date() |
|
|
|
date.setTime(date.getTime() + days * 24 * 60 * 60 * 1000) |
|
expires = '; expires=' + date.toGMTString() |
|
} |
|
|
|
document.cookie = name + '=' + value + expires + '; path=/' |
|
}, |
|
del: name => this.set(name, '', -1), |
|
toggle: name => { |
|
let val = this.get(name) |
|
|
|
if (val == '1') val = '0' |
|
else val = '1' |
|
|
|
this.set(name, val) |
|
|
|
return val |
|
}, |
|
} |
|
|
|
document.querySelector('h1.red-line span').textContent += '+' |
|
footer.innerHTML += |
|
' | <a href="https://gist.github.com/Glaived/bf80a268fdbc5ed8ffd71e7786c8cfa6">Abyss Explorer+</a>' |
|
|
|
search.addEventListener('click', () => search.select()) |
|
|
|
window.addEventListener('focus', () => search.select()) |
|
|
|
document.addEventListener('keydown', e => { |
|
let event = e || window.event, |
|
key = event.key |
|
|
|
if (!event.ctrlKey) { |
|
search.focus() |
|
|
|
if ( |
|
['+', ',', 'Enter'].indexOf(key) > -1 && |
|
search.value.substring( |
|
event.target.selectionStart - 2, |
|
event.target.selectionStart |
|
) !== ', ' |
|
) { |
|
event.preventDefault() |
|
|
|
search.value = |
|
search.value.substring(0, event.target.selectionStart) + |
|
', ' + |
|
search.value.substring(event.target.selectionEnd) |
|
|
|
search.dispatchEvent(new Event('input')) |
|
} else if (['Backspace', 'Delete'].indexOf(key) > -1) { |
|
let before = search.value.substring( |
|
event.target.selectionStart - 2, |
|
event.target.selectionStart |
|
), |
|
inSplit = search.value.substring( |
|
event.target.selectionStart - 1, |
|
event.target.selectionStart + 1 |
|
), |
|
after = search.value.substring( |
|
event.target.selectionStart, |
|
event.target.selectionStart + 2 |
|
) |
|
|
|
if ([before, inSplit, after].indexOf(', ') > -1) event.preventDefault() |
|
|
|
if (before === ', ') { |
|
search.value = |
|
search.value.substring(0, event.target.selectionStart - 2) + |
|
search.value.substring(event.target.selectionEnd) |
|
} else if (inSplit === ', ') { |
|
search.value = |
|
search.value.substring(0, event.target.selectionStart - 1) + |
|
search.value.substring(event.target.selectionStart + 1) |
|
} else if (after === ', ') { |
|
search.value = |
|
search.value.substring(0, event.target.selectionStart) + |
|
search.value.substring(event.target.selectionStart + 2) |
|
} |
|
|
|
if ([before, inSplit, after].indexOf(', ') > -1) |
|
search.dispatchEvent(new Event('input')) |
|
} |
|
} |
|
}) |
|
|
|
document.addEventListener('click', e => { |
|
if (e.target && e.target.className === 'button aeplus') { |
|
Cookie.set('aeplus', e.target.dataset.sha, 31) |
|
|
|
document.querySelector('.update-available.orange-line').remove() |
|
} |
|
}) |
|
|
|
// Clear searchbar if idle after a few seconds |
|
onblur(30 * 1000, () => { |
|
window.scrollTo(0, 0) |
|
|
|
body.style.opacity = 0.3 |
|
|
|
if (search.value !== '') { |
|
section.style.opacity = 0 |
|
|
|
setTimeout(() => { |
|
search.value = '' |
|
search.focus() |
|
|
|
search.dispatchEvent(new Event('input')) |
|
|
|
section.style.opacity = 1 |
|
}, 1200) |
|
} |
|
}) |
|
|
|
fetch( |
|
'https://api.github.com/repos/AcelisWeaven/AbyssExplorer/branches/gh-pages', |
|
{ |
|
method: 'GET', |
|
redirect: 'follow', |
|
} |
|
) |
|
.then(response => response.json()) |
|
.then(data => { |
|
if ( |
|
sha !== data.commit.sha && |
|
(Cookie.get('aeplus') === null || |
|
Cookie.get('aeplus') !== data.commit.sha) |
|
) |
|
document.querySelector('body').insertAdjacentHTML( |
|
'afterbegin', |
|
`<div class="update-available orange-line"> |
|
<a href="https://gist.github.com/Glaived/bf80a268fdbc5ed8ffd71e7786c8cfa6">Abyss Explorer+</a> may no longer be up to date compare to Abyss Explorer |
|
<span class="button aeplus" data-sha="${data.commit.sha}">Stop displaying this alert for one month</span> |
|
</div>` |
|
) |
|
}) |
|
.catch(error => console.error(error)) |