Last active
September 10, 2023 02:28
-
-
Save lundeen-bryan/ece730c091c15b027f0f0cc53ac1f4c7 to your computer and use it in GitHub Desktop.
Show all dates in current webpage in a popup
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
/** | |
* Date Extractor from HTML | |
* | |
* Version: 1.0.0 | |
* | |
* Purpose: | |
* This script is designed to extract dates present in an HTML document. | |
* It uses multiple regular expressions to detect a variety of date formats. | |
* After extracting all the unique dates, it presents them in a new popup window, | |
* sorted in descending order from newest to oldest. If no dates are found, | |
* a message is displayed in the popup indicating the absence of detectable dates. | |
* | |
* Creator: lundeen-bryan | |
* Creation Date: 2023-09-09 | |
*/ | |
(function () { | |
// Function to format a Date object into DD-MMM-YYYY format | |
function formatDate(d) { | |
const monthNames = [ | |
"Jan", | |
"Feb", | |
"Mar", | |
"Apr", | |
"May", | |
"Jun", | |
"Jul", | |
"Aug", | |
"Sep", | |
"Oct", | |
"Nov", | |
"Dec", | |
]; | |
return ( | |
("0" + d.getDate()).slice(-2) + | |
"-" + | |
monthNames[d.getMonth()] + | |
"-" + | |
d.getFullYear() | |
); | |
} | |
// Array to store unique date tags found in the document | |
var tags = []; | |
// Function to extract dates from the document based on given regex pattern | |
function addMoreDates(reg) { | |
var addTags = document.documentElement.innerHTML.match(reg); | |
if (addTags) { | |
addTags.forEach(function (newTag) { | |
let parsedDate = Date.parse(newTag); | |
if (!isNaN(parsedDate)) { | |
let formattedDate = formatDate(new Date(parsedDate)); | |
// Add the date to the tags list if it's not already there | |
if (tags.indexOf(formattedDate) === -1) { | |
tags.push(formattedDate); | |
} | |
} | |
}); | |
} | |
} | |
// Array of Regex patterns to detect various date formats in the document | |
const regexPatterns = [ | |
// ... (other regex patterns listed) ... | |
]; | |
// For each regex pattern, attempt to extract dates from the document | |
regexPatterns.forEach(addMoreDates); | |
// Sort the dates in descending order (newest to oldest) | |
tags.sort(function (a, b) { | |
return Date.parse(b) - Date.parse(a); | |
}); | |
// If no dates were found, add a default message | |
if (tags.length === 0) { | |
tags = ["No dates detected in page."]; | |
} | |
// Construct the popup content HTML | |
var popupContent = "<html><head><title>Date List</title></head><body>"; | |
popupContent += "<h2>Dates in the HTML:</h2>"; | |
popupContent += "<ul>"; | |
tags.forEach(function (tag) { | |
popupContent += "<li>" + tag + "</li>"; | |
}); | |
popupContent += "</ul></body></html>"; | |
// Open a new popup window and write the list of dates into it | |
var popupWindow = window.open("", "DateListPopup", "width=400,height=400,scrollbars=1"); | |
popupWindow.document.write(popupContent); | |
popupWindow.document.close(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment