Add null checks and make use of insertAdjacentHTML

This commit is contained in:
Eduard Urbach 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()

View File

@ -303,7 +303,7 @@ export default class AnimeNotifier {
let message = ""
// Prevent closing tab on new thread page
if(this.app.currentPath === "/new/thread" && document.activeElement.tagName === "TEXTAREA" && (document.activeElement as HTMLTextAreaElement).value.length > 20) {
if(this.app.currentPath === "/new/thread" && document.activeElement && document.activeElement.tagName === "TEXTAREA" && (document.activeElement as HTMLTextAreaElement).value.length > 20) {
message = "You have unsaved changes on the current page. Are you sure you want to leave?"
}
@ -351,6 +351,12 @@ export default class AnimeNotifier {
// Calculate offsets
let tipStyle = window.getComputedStyle(element, ":before")
if(!tipStyle.width || !tipStyle.paddingLeft) {
console.error("Tooltip with incorrect computed style:", element)
return
}
let tipWidth = parseInt(tipStyle.width) + parseInt(tipStyle.paddingLeft) * 2
let tipStartX = rect.left + rect.width / 2 - tipWidth / 2 - contentRect.left
let tipEndX = tipStartX + tipWidth
@ -423,7 +429,7 @@ export default class AnimeNotifier {
}
element.addEventListener("drop", async e => {
let toElement = e.toElement
let toElement: Element | null = e.toElement
// Find tab element
while(toElement && !toElement.classList.contains("tab")) {
@ -682,7 +688,14 @@ export default class AnimeNotifier {
setSelectBoxValue() {
for(let element of document.getElementsByTagName("select")) {
element.value = element.getAttribute("value")
let attributeValue = element.getAttribute("value")
if(!attributeValue) {
console.error("Select box without a value:", element)
continue
}
element.value = attributeValue
}
}
@ -698,7 +711,7 @@ export default class AnimeNotifier {
}
for(let element of findAll("utc-date-absolute")) {
displayTime(element, now)
displayTime(element)
}
}