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

View File

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

View File

@ -303,7 +303,7 @@ export default class AnimeNotifier {
let message = "" let message = ""
// Prevent closing tab on new thread page // 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?" 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 // Calculate offsets
let tipStyle = window.getComputedStyle(element, ":before") 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 tipWidth = parseInt(tipStyle.width) + parseInt(tipStyle.paddingLeft) * 2
let tipStartX = rect.left + rect.width / 2 - tipWidth / 2 - contentRect.left let tipStartX = rect.left + rect.width / 2 - tipWidth / 2 - contentRect.left
let tipEndX = tipStartX + tipWidth let tipEndX = tipStartX + tipWidth
@ -423,7 +429,7 @@ export default class AnimeNotifier {
} }
element.addEventListener("drop", async e => { element.addEventListener("drop", async e => {
let toElement = e.toElement let toElement: Element | null = e.toElement
// Find tab element // Find tab element
while(toElement && !toElement.classList.contains("tab")) { while(toElement && !toElement.classList.contains("tab")) {
@ -682,7 +688,14 @@ export default class AnimeNotifier {
setSelectBoxValue() { setSelectBoxValue() {
for(let element of document.getElementsByTagName("select")) { 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")) { for(let element of findAll("utc-date-absolute")) {
displayTime(element, now) displayTime(element)
} }
} }