YouTube 脚本手机版

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

目前为 2020-12-23 提交的版本。查看 最新版本

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

(function() {
    'use strict';
    let postForm = function() {
        // Remove origin node
        let post = document.getElementById("download-post");
        if(post && post !== undefined) {
            post.remove();
        }
        // Create form for post herf
        let form = function() {
            let form = document.createElement("form");
            form.id = "download-post";
            form.name = "post";
            form.method = "post";
            form.target = "_blank";
            form.action = "https://www.findyoutube.net/result";
            return form;
        }();
        document.body.appendChild(form);
        // Add url input
        form.appendChild(function() {
            let inputUrl = document.createElement("input");
            inputUrl.type = "hidden";
            inputUrl.name = "url";
            inputUrl.value = window.location.href;
            return inputUrl;
        }());
        // Add proxy input
        form.appendChild(function() {
            let inputProxy = document.createElement("input");
            inputProxy.type = "hidden";
            inputProxy.name = "proxy";
            inputProxy.value = "Random";
            return inputProxy;
        }());
        form.submit();
    }
    setInterval(function() {
        let ads = ["item GoogleActiveViewElement", "companion-ad-container"];
        let skipAds = ["ytp-ad-skip-button ytp-button"];
        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
        for(let idx = 0; idx < skipAds.length; ++idx) {
            let nodes = document.getElementsByClassName(skipAds[idx]);
            if(nodes && nodes.length > 0) {
                let videos = document.getElementsByTagName('video');
                if(videos && videos.length > 0) {
                    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;
            }
        }
        if(!isInject && bCompacts.length >= 5) {
            let bReport = bCompacts[4];
            if(bReport && bReport !== undefined) {
                let bParent = bReport.parentElement;
                let bDown = bReport.cloneNode(true);
                bDown.getElementsByClassName("button-renderer-text")[0].innerText = "下载";
                bDown.getElementsByTagName("svg")[0].outerHTML = `<svg t="1567309127254" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2090" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M640 469.333333h170.666667l-298.666667 298.666667-298.666667-298.666667h170.666667V85.333333h256v384zM213.333333 853.333333h597.333334v85.333334H213.333333v-85.333334z" p-id="2091"></path></svg>`;
                bDown.onclick = postForm;
                bParent.appendChild(bDown);
            }
        }
    }, 500);
})();