ACT.YouTube.MO.转到嵌入页按钮

一键转到嵌入式页面以在页面内全屏播放器上使用字幕自动翻译菜单,供手机用户使用。

当前为 2022-04-18 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name               ACT.YouTube.MO.Embed-button
// @name:zh-CN         ACT.YouTube.MO.转到嵌入页按钮
// @description        Go to Embed page to uses Subtitles/CC auto-translate menu on in-page fullscreen player, for mobile users.
// @description:zh-CN  一键转到嵌入式页面以在页面内全屏播放器上使用字幕自动翻译菜单,供手机用户使用。
// @author             ACTCD
// @version            20220419.1
// @license            GPL-3.0-or-later
// @namespace          ACTCD/Userscripts
// @supportURL         https://github.com/ACTCD/Userscripts#contact
// @homepageURL        https://github.com/ACTCD/Userscripts
// @match              *://m.youtube.com/*
// @match              *://www.youtube-nocookie.com/embed/*
// @grant              none
// @run-at             document-start
// ==/UserScript==

(function () {
    'use strict';

    const button = document.createElement("button");
    button.id = "ACT_Embed";
    button.innerText = "Embed";
    button.style.setProperty('color', 'white');
    button.style.setProperty('background-color', 'transparent');
    button.style.setProperty('border', '2px solid');
    button.style.setProperty('border-radius', '10px');
    button.style.setProperty('padding', '1px 5px');
    button.style.setProperty('font-size', '1.5em');
    button.style.setProperty('font-weight', '500');
    button.style.setProperty('text-shadow', 'black 1px 1px 2px');
    button.addEventListener('click', event => {
        let vid = new URL(location).searchParams.get('v');
        if (!vid) return;
        let url = new URL('https://www.youtube-nocookie.com/embed/?autoplay=1&cc_lang_pref=zh&cc_load_policy=1&hl=zh&modestbranding=1&tlang=zh-Hans');
        url.pathname = '/embed/' + vid;
        location.href = url;
        event.preventDefault();
        event.stopImmediatePropagation();
    }, true);

    function insert_button() {
        if (location.hostname != 'm.youtube.com') return;
        if (location.pathname != '/watch') return;
        if (button.parentNode) return;
        document.querySelector('ytm-home-logo','.mobile-topbar-header-endpoint')?.after(button);
        console.log('insert_button');
    }

    function tweak() {
        insert_button()
        document.querySelector('.ytp-fullscreen-button')?.remove();
    }

    new MutationObserver(tweak).observe(document, { subtree: true, childList: true });

    function web_app() {
        const meta = document.createElement('meta');
        meta.name = 'apple-mobile-web-app-capable';
        meta.setAttribute('content', 'yes');
        document.head.appendChild(meta);
    }

    function WindowLoaded() {
        console.log('WindowLoaded');
        tweak();
        location.pathname.startsWith('/embed/') && web_app();
    }

    if (document.readyState === 'complete') {
        WindowLoaded();
    } else {
        window.addEventListener('load', WindowLoaded);
    }

})();