Last active
May 3, 2021 17:53
-
-
Save alexiglesias93/04aa371683703e4fd7be7bd7bec53905 to your computer and use it in GitHub Desktop.
Display the last published date of a Webflow site in an HTMLElement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Display the last published date of a Webflow site in an HTMLElement | |
* @constant targetSelector You should place the selector of the element that will display the publish date. | |
* If you want to modify the outputted Date format, you can pass an additional options object to the Date.toDateString() method. | |
*/ | |
document.addEventListener('DOMContentLoaded', () => { | |
const targetSelector = 'YOUR_ELEMENT_SELECTOR_HERE'; | |
const publishDate = extractPublishDate(); | |
if (!publishDate) return; | |
const target = document.querySelector(targetSelector); | |
if (target) target.textContent = publishDate.toDateString(); | |
}); | |
/** | |
* Extracts the publish date of a Webflow site | |
* @returns A Date object, if found | |
*/ | |
const extractPublishDate = (): Date | undefined | void => { | |
const publishDatePrefix = 'Last Published:'; | |
for (const node of document.childNodes) { | |
if (node.nodeType === Node.COMMENT_NODE && node.textContent?.includes(publishDatePrefix)) { | |
const publishDateValue = node.textContent.trim().split(publishDatePrefix)[1]; | |
if (publishDateValue) return new Date(publishDateValue); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment