Skip to content

Commit

Permalink
add content override to query params
Browse files Browse the repository at this point in the history
  • Loading branch information
zeroXbrock committed Jun 25, 2024
1 parent d9c8f81 commit 2c572ef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
15 changes: 12 additions & 3 deletions js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ const DEFAULT_CONTENT_SRC = 'https://raw.githubusercontent.com/zeroXbrock/banner

/** Register scripts to run when DOM content is loaded.
* @param {{
* contentUrl: string,
* contentUrl?: string,
* marqueeId: string,
* _period?: number,
* _content?: string,
* }} params - hardcoded configuration parameters.
*/
const init = (params) => {
Expand All @@ -19,6 +20,7 @@ const init = (params) => {
...params,
contentUrl,
_period: queryParams['_period'] || params._period,
_content: queryParams['_content'] || params._content,
}
document.addEventListener('DOMContentLoaded', () => {
doInit(args)
Expand All @@ -38,8 +40,15 @@ async function doInit(params) {
marqueeId,
contentUrl,
_period,
_content,
} = params
console.log("fetching content...")
await fetchContent(marqueeId, contentUrl)
let content = _content
if (!_content) {
console.log("fetching content from URL...")
content = await fetchContent(contentUrl)
}

const bannerElement = document.getElementById(marqueeId);
bannerElement.innerHTML = ` ${content} `;
scaleAnimationDuration(marqueeId, _period)
}
8 changes: 3 additions & 5 deletions js/fetch-banner-content.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/** Fetch content from `contentSrc` and inject it into element with id `marqueeId`.
* @param {string} marqueeId - The id of the element to inject the content into.
* @param {string} contentSrc - The URL to fetch the content from.
*/
const fetchContent = async (marqueeId, contentSrc) => {
const fetchContent = async (contentSrc) => {
try {
const response = await fetch(contentSrc, {cache: "no-cache"});
if (!response.ok) {
throw new Error('Network response was not ok');
}
const content = await response.text();
const bannerElement = document.getElementById(marqueeId);
bannerElement.innerHTML = ` ${content} `;
return await response.text();
} catch (error) {
console.error('Error fetching content:', error);
return null
}
}

0 comments on commit 2c572ef

Please sign in to comment.