Add null checks and make use of insertAdjacentHTML

This commit is contained in:
2019-04-19 23:34:49 +09:00
parent d16197340d
commit 6ec4017638
3 changed files with 36 additions and 16 deletions

View File

@ -8,10 +8,15 @@ export async function loadMore(arn: AnimeNotifier, button: HTMLButtonElement) {
return
}
const target = document.getElementById("load-more-target")
if(!target) {
return
}
arn.loading(true)
button.disabled = true
let target = document.getElementById("load-more-target")
let index = button.dataset.index
try {
@ -26,25 +31,19 @@ export async function loadMore(arn: AnimeNotifier, button: HTMLButtonElement) {
let newIndex = response.headers.get("X-LoadMore-Index")
// End of data?
if(newIndex === "-1") {
if(!newIndex || newIndex === "-1") {
button.disabled = true
button.classList.add("hidden")
} else {
button.dataset.index = newIndex
}
let body = await response.text()
let tmp = document.createElement(target.tagName)
tmp.innerHTML = body
let children = [...tmp.childNodes]
// Get the HTML response
let html = await response.text()
// Add the HTML to the existing target
Diff.mutations.queue(() => {
for(let child of children) {
target.appendChild(child)
}
target.insertAdjacentHTML("beforeend", html)
arn.app.emit("DOMContentLoaded")
})
} catch(err) {

View File

@ -2,6 +2,10 @@ import AnimeNotifier from "../AnimeNotifier"
// Enable notifications
export async function enableNotifications(arn: AnimeNotifier, button: HTMLElement) {
if(!arn.user || !arn.user.dataset.id) {
return
}
arn.statusMessage.showInfo("Enabling instant notifications...")
await arn.pushManager.subscribe(arn.user.dataset.id)
arn.updatePushUI()
@ -10,6 +14,10 @@ export async function enableNotifications(arn: AnimeNotifier, button: HTMLElemen
// Disable notifications
export async function disableNotifications(arn: AnimeNotifier, button: HTMLElement) {
if(!arn.user || !arn.user.dataset.id) {
return
}
arn.statusMessage.showInfo("Disabling instant notifications...")
await arn.pushManager.unsubscribe(arn.user.dataset.id)
arn.updatePushUI()