From 6ec4017638054fb68286ea37a653ff9c02f8ccf8 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Fri, 19 Apr 2019 23:34:49 +0900 Subject: [PATCH] Add null checks and make use of insertAdjacentHTML --- scripts/Actions/InfiniteScroller.ts | 23 +++++++++++------------ scripts/Actions/Notifications.ts | 8 ++++++++ scripts/AnimeNotifier.ts | 21 +++++++++++++++++---- 3 files changed, 36 insertions(+), 16 deletions(-) diff --git a/scripts/Actions/InfiniteScroller.ts b/scripts/Actions/InfiniteScroller.ts index ca2c176c..bc4d961c 100644 --- a/scripts/Actions/InfiniteScroller.ts +++ b/scripts/Actions/InfiniteScroller.ts @@ -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) { diff --git a/scripts/Actions/Notifications.ts b/scripts/Actions/Notifications.ts index 4e1fb076..c4efe8a2 100644 --- a/scripts/Actions/Notifications.ts +++ b/scripts/Actions/Notifications.ts @@ -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() diff --git a/scripts/AnimeNotifier.ts b/scripts/AnimeNotifier.ts index eed08b26..46bf03f9 100644 --- a/scripts/AnimeNotifier.ts +++ b/scripts/AnimeNotifier.ts @@ -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) } }