From faed3b4c900c6e2820b4905c8179c0d225b7471c Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Thu, 16 Nov 2023 05:29:06 -0800 Subject: [PATCH] async/await --- asan.js | 97 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 60 insertions(+), 37 deletions(-) diff --git a/asan.js b/asan.js index afedbe9..7e04144 100644 --- a/asan.js +++ b/asan.js @@ -42,11 +42,11 @@ THE SOFTWARE. // It then fetches the HTML from the 'src' path. // It then includes the HTML it fetched from the 'src' path here into the 'dst'. // It then sets the title of the page to the inner-text for the first

from the 'src'. -export function include(element) { +export async function include(element) { log("begin") if (undefined === element) { - log("element is undefined") + logerror("element is undefined") log("end") return } @@ -54,45 +54,25 @@ export function include(element) { let dst = window.location.pathname let src = "/src" + dst - log("dst-path: ", dst) - log("src-path: ", src) + log("dst-path:", JSON.stringify(dst)) + log("src-path:", JSON.stringify(src)) - fetch(src). - then( response => { - if (!response.ok) { - element.innerHTML = '

Not Found

' - throw new Error('network response was not ok') - } + const response = await fetch(src) + if (!response.ok) { + element.innerHTML = '

Not Found

' + logerror("src file not found") + log("end") + throw new Error('network response was not ok') + } + log("src file found") - log("src found") - return response.text() - }). - then( text => { - element.innerHTML = text + const text = await response.text() - return text - }). - then( () => { + // include the src file + setInnerHTML(element, text) - // Set the title of the page to the value of the first

(if there is an

). - - const h1s = element.getElementsByTagName("h1") - if (undefined === h1s || element.length <= 0) { - return - } - const h1 = h1s[0] - - const titles = document.getElementsByTagName("title") - if (undefined === titles || titles.length <= 0) { - return - } - const title = titles[0] - - const titleText = h1.innerText - console.log("[dst] title-text: ", titleText) - title.innerText = titleText - - }); + // Set the title of the page to the value of the first

(if there is an

). + setTitle(element) log("end") } @@ -101,3 +81,46 @@ function log(...args) { args.unshift("[asan][include]"); console.log(...args); } + +function logerror(...args) { + args.unshift("[asan][include] ERROR:"); + console.error(...args); +} + +function setInnerHTML(element, text) { + if (undefined === element) { + logerror("element is undefined") + return + } + + element.innerHTML = text + log("src file included") +} + +function setTitle(element) { + if (undefined === element) { + logerror("element is undefined") + return + } + + const h1s = element.getElementsByTagName("h1") + if (undefined === h1s || element.length <= 0) { + logerror("

s is undefined") + return + } + const h1 = h1s[0] + log("src file has at least 1

element — will set the same value of to the same value of this first <h1>") + + const titleText = h1.innerText + log("first <h1>'s value:", JSON.stringify(titleText)) + + const titles = document.getElementsByTagName("title") + if (undefined === titles || titles.length <= 0) { + logerror("<title>s is undefined") + return + } + const title = titles[0] + + title.innerText = titleText + log("value of <title> set to value of first <h1> in included src file") +}