Native Promise for YouTube

YouTube replaced Promise with polyfill in some old browser.

目前为 2023-06-22 提交的版本。查看 最新版本

// ==UserScript==
// @name         Native Promise for YouTube
// @namespace    http://tampermonkey.net/
// @version      0.1.0
// @license      MIT
// @description  YouTube replaced Promise with polyfill in some old browser.
// @author       CY Fung
// @match        https://www.youtube.com/*
// @match        https://www.youtube-nocookie.com/embed/*
// @match        https://music.youtube.com/*
// @run-at       document-start
// @grant        none
// @unwrap
// @allFrames
// @inject-into page
// ==/UserScript==

(function () {
    'use strict';

    const truePromise = (async () => { })().constructor;
    if (!truePromise) return;
    let cid = setInterval(() => {
        if (window.Promise !== truePromise) {
            window.Promise = truePromise;
            handler();
        }
    }, 1);

    const isMobile = location.hostname === 'm.youtube.com';

    const ets = isMobile ? ['initialdata', 'state-change', 'state-navigateend'] : ['yt-page-data-fetched', 'yt-navigate-finish', 'spfdone'];
    const mt = isMobile ? window : document;
    const handler = function () {
        if (!handler) return;
        handler = null;
        window.Promise = truePromise;
        clearInterval(cid);
        for (const et of ets) {
            mt.removeEventListener(et, handler, false);
        }
    }

    for (const et of ets) {
        mt.addEventListener(et, handler, false);
    }


    function onReady() {
        const f = handler;
        if (!f) return;
        handler = null;
        f();
    }



    if (document.readyState != 'loading') {
        onReady();
    } else {
        window.addEventListener("DOMContentLoaded", onReady, false);
    }


    // Your code here...
})();