Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight 7 day old date and time from current date and time #2245

Open
Wasay21 opened this issue Nov 14, 2024 · 1 comment
Open

Highlight 7 day old date and time from current date and time #2245

Wasay21 opened this issue Nov 14, 2024 · 1 comment

Comments

@Wasay21
Copy link

Wasay21 commented Nov 14, 2024

Issue - Urgent help
I have written a tamper monkey script to highlight date and time which are 7 days old from current date and time , the date and time format should be 6/28/2023, 10:31:39 PM , it should run in all tabs and webpages unable to highlight with my script

Please review my script for better understanding

v// ==UserScript==
// @name Date Highlighter
// @namespace http://tampermonkey.net/
// @Version 1.0
// @description Highlights dates older than 7 days
// @author Your name
// @match :///*
// @grant none
// @run-at document-idle
// ==/UserScript==

(function() {
'use strict';

// Configuration
const HIGHLIGHT_COLOR = '#ffeb3b'; // Yellow highlight
const DATE_PATTERNS = [
    // MM/DD/YYYY, HH:MM:SS AM/PM
    /(\d{1,2}\/\d{1,2}\/\d{4},?\s*\d{1,2}:\d{2}:\d{2}\s*[AP]M)/i,
    // MM/DD/YYYY
    /(\d{1,2}\/\d{1,2}\/\d{4})/,
    // YYYY-MM-DD
    /(\d{4}-\d{2}-\d{2})/
];

function isDateOlderThanDays(dateStr, days) {
    try {
        let date;
        if (dateStr.includes(',')) {
            // For format like "6/28/2023, 10:31:39 PM"
            date = new Date(dateStr);
        } else if (dateStr.includes('-')) {
            // For ISO format YYYY-MM-DD
            const [year, month, day] = dateStr.split('-');
            date = new Date(year, month - 1, day);
        } else {
            // For format like "11/14/2023"
            const [month, day, year] = dateStr.split('/');
            date = new Date(year, month - 1, day);
        }

        const now = new Date();
        const diffTime = now - date;
        const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
        console.log(`Date: ${dateStr}, Diff Days: ${diffDays}`); // Debug log
        return diffDays > days;
    } catch (e) {
        console.error('Date parsing error:', e, dateStr);
        return false;
    }
}

function createHighlightedSpan(text) {
    const span = document.createElement('span');
    span.style.backgroundColor = HIGHLIGHT_COLOR;
    span.style.padding = '2px';
    span.style.borderRadius = '3px';
    span.textContent = text;
    return span;
}

function processNode(node) {
    if (node.nodeType !== Node.TEXT_NODE || !node.textContent.trim()) {
        return;
    }

    const text = node.textContent;
    let hasMatch = false;
    let fragment = document.createDocumentFragment();
    let lastIndex = 0;

    DATE_PATTERNS.forEach(pattern => {
        pattern.lastIndex = 0; // Reset regex
        let match;

        while ((match = pattern.exec(text)) !== null) {
            const dateStr = match[0];
            if (isDateOlderThanDays(dateStr, 7)) {
                hasMatch = true;
                // Add text before the match
                if (match.index > lastIndex) {
                    fragment.appendChild(document.createTextNode(text.slice(lastIndex, match.index)));
                }
                // Add highlighted date
                fragment.appendChild(createHighlightedSpan(dateStr));
                lastIndex = pattern.lastIndex;
            }
        }
    });

    if (hasMatch) {
        // Add any remaining text
        if (lastIndex < text.length) {
            fragment.appendChild(document.createTextNode(text.slice(lastIndex)));
        }
        node.parentNode.replaceChild(fragment, node);
    }
}

function walkDom(node) {
    if (node.nodeType === Node.ELEMENT_NODE) {
        if (node.tagName === 'SCRIPT' || node.tagName === 'STYLE' || node.tagName === 'NOSCRIPT') {
            return;
        }
        Array.from(node.childNodes).forEach(walkDom);
    } else if (node.nodeType === Node.TEXT_NODE) {
        processNode(node);
    }
}

// Initial processing
console.log('Starting date highlighting...'); // Debug log
walkDom(document.body);

// Handle dynamic content
const observer = new MutationObserver((mutations) => {
    mutations.forEach(mutation => {
        mutation.addedNodes.forEach(node => {
            if (node.nodeType === Node.ELEMENT_NODE) {
                walkDom(node);
            }
        });
    });
});

// Start observing
observer.observe(document.body, {
    childList: true,
    subtree: true
});

// Add CSS for better visibility
const style = document.createElement('style');
style.textContent = `
    .date-highlight {
        background-color: ${HIGHLIGHT_COLOR} !important;
        padding: 2px !important;
        border-radius: 3px !important;
        display: inline-block !important;
    }
`;
document.head.appendChild(style);

// Debug info
console.log('Date highlighter script loaded and running');

})();

@Wasay21
Copy link
Author

Wasay21 commented Nov 14, 2024

Need help please anyone with experience can check and reply

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant