YouTube 脚本手机版

下载视频,移除广告,自动跳过广告

目前為 2022-09-17 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         YouTube 脚本手机版
// @namespace    http://tampermonkey.net/
// @version      0.4.2
// @description  下载视频,移除广告,自动跳过广告
// @author       sl00p
// @match        https://m.youtube.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';
    setInterval(function() {
        let ads = ["item GoogleActiveViewElement", "companion-ad-container"];
        let bCompacts = document.getElementsByClassName("button-renderer compact ");
        let isInject = false;
        // Remove ads
        for(let idx = 0; idx < ads.length; ++idx) {
            let nodes = document.getElementsByClassName(ads[idx]);
            if(nodes && nodes.length > 0) {
                nodes[0].remove();
            }
        }
        // Skip ads
        let videos = document.getElementsByTagName('video');
        let adsButtom = ['ytp-ad-skip-button-container', 'ytp-ad-preview-container', 'ytp-ad-text', 'ytp-ad-timed-pie-countdown-container'];
        for(let idx = 0; idx < adsButtom.length; ++idx) {
            let buttom = document.getElementsByClassName(adsButtom[idx]);
            if(videos && videos.length > 0 && buttom && buttom.length > 0) {
                if (videos[0].currentTime && videos[0].duration) {
                    videos[0].currentTime = videos[0].duration;
                }
            }
        }
        // Test if injected already
        for(let idx = 0; idx < bCompacts.length; ++idx) {
            if(bCompacts[idx].innerText.trim() === "下载") {
                isInject = true;
            }
        }
        // Directly return if match shorts
        if(window.location.href.indexOf('shorts') !== -1) {
            return 0;
        }
        if(!isInject && bCompacts.length >= 5) {
            let bReport = bCompacts[4];
            if(bReport && bReport !== undefined) {
                let bParent = bReport.parentElement;
                let bDown = bReport.cloneNode(true);
                let quality = {
                    "data": {
                        "quality": 720,
                        "previousQuality": 360,
                    },
                    "expiration": new Date().getTime(),
                    "creation": new Date().getTime() + 432000
                };
                bDown.getElementsByClassName("button-renderer-text")[0].innerText = "下载";
                bDown.getElementsByTagName("svg")[0].outerHTML =
                    `<svg t="1613792186988" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="1434"
                    width="200" height="200"><path d="M514 114.4c-220 0-398.8 178.9-398.8 398.8S294 912 514 912s398.8-178.9 398.8-398.8S734
                    114.4 514 114.4z m0 754.9c-196.4 0-356.1-159.8-356.1-356.1S317.6 157.1 514 157.1c196.4 0 356.1 159.8 356.1 356.1S710.4
                    869.3 514 869.3z" p-id="1435"></path><path d="M540.8 614.7c-1.7 1.7-3.5 3.2-5.5 4.5V252.1h-42.7v367.2c-1.9-1.3-3.8-2.8-5.5-4.5L368.9
                    496.5l-30.2 30.2L457 644.9c15.7 15.7 36.4 23.6 57 23.6 20.7 0 41.3-7.9 57-23.6l118.3-118.3-30.2-30.2-118.3 118.3zM358.6 697.4h310.7v42.7H358.6z"
                    p-id="1436"></path></svg>`;
                bDown.onclick = function() {
                    window.open("https://yt5s.com/en50?q=" + window.location.href);
                };
                bParent.appendChild(bDown);
                window.sessionStorage.setItem("yt-html5-player-modules::subtitlesModuleData::module-enabled", true);
                window.sessionStorage.setItem("yt-player-quality", JSON.stringify(quality));
            }
        }
    }, 500);
})();