Format Date

mail@pastecode.io avatar
unknown
javascript
2 years ago
1.7 kB
1
Indexable
Never
// ==UserScript==
// @name         Youtube Data Formatter
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        http://*.youtube.com/*
// @match        https://*.youtube.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=youtube.com
// @grant        none
// ==/UserScript==

(() => {
        'use strict'

        let result = '';
        let isResultAppended = false;
        let intervalId;

        const youtubeDate = '#info > span:nth-child(3)';
        const standardDate = '#description-inner > tp-yt-paper-tooltip > #tooltip';

        const pattern = /[A-Za-z]{3}\s\d{1,2},\s\d{4}/g;

        function updateDate() {

            const elementYoutubeDate = document.querySelector(youtubeDate);
            const textYoutubeDate = elementYoutubeDate.textContent;

            const elementStandardDate = document.querySelector(standardDate);
            const textStandardDate = elementStandardDate.textContent;

            if (elementYoutubeDate && elementStandardDate) {

                result = `${textYoutubeDate} - ${textStandardDate.match(pattern)[0]}`;

                if (textYoutubeDate !== result && !isResultAppended) {

                    elementYoutubeDate.textContent = result;
                    isResultAppended = true;
                    console.log("Date was updated successfully");
                    clearInterval(intervalId);
                } else {
                    isResultAppended = false;
                }
            }
        }

        intervalId = setInterval(updateDate, 3000);
    }
)();