Native Promise for YouTube

YouTube replaced Promise with polyfill in some old browser.

目前為 2023-06-22 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Native Promise for YouTube
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @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';

    try {
        const truePromise = (async () => { })().constructor;

        window.PromiseRejectionEvent = window.PromiseRejectionEvent || (() => {
            throw 'PromiseRejectionEvent is not supported';
        });


        if (truePromise !== Promise) window.Promise = truePromise;
    } catch (e) { }

    /*
        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;
        let 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...
})();