Tampermonkey script for YouTube not loading when opening videos linked from YouTube website

31 Views Asked by At

I wrote a Tampermonkey script to replace the "___ days/months/years ago" field on YouTube video descriptions with the actual date, the way things used to be. The script works great when opening a YouTube URL directly via https://www.youtube.com/watch?v=, but it won't execute when clicking a video link on the YouTube website, such as from search results or the homepage.

// ==UserScript==
// @name         YouTube Date Changer
// @namespace    http://tampermonkey.net/
// @version      2024-01-13
// @description  Replaces the "___ days/months/years ago" with the actual upload date in the video description
// @match        https://www.youtube.com/watch?*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=tampermonkey.net
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';
    function run () {
        var longdate = document.querySelector("div#info-strings yt-formatted-string");
        if (longdate) {
            var str = longdate.innerHTML;
            var shortdate = document.getElementsByClassName("style-scope yt-formatted-string bold")[2];
            shortdate.innerHTML = str;
        } else {
            setTimeout(run,250);
        }
    }
    run();
})();

This appears to happen because YouTube internally loads videos from its website instead of making a new request for the video URL. (Note the progress bar that shoots across the top of the window after clicking a video link on YouTube.) I don't think Tampermonkey ever sees the @match in my script.

I can work around this behavior by opening video links in a new window, but that doesn't really solve the problem. I see two solutions:

  1. Force YouTube (through additional userscripts?) to open all videos links as if they were new requests; or

  2. Use a different method of triggering the Tampermonkey script by detecting when the video loads, not when the URL changes.

I tried researching these two ideas, but I haven't had any luck. I'd prefer #2 but I'm not sure where to start.

0

There are 0 best solutions below