Greasy Fork

Greasy Fork is available in English.

【自用】YoutubeTools

1.恢复页面布局,2.增加按钮,点击后复制一条命令行到剪切板(配合IDM使用)【受YouTube™ Multi Downloader启发,需配合Local YouTube Downloader使用】,3.增加按钮,一键重绘下载png格式的cover

目前为 2021-09-03 提交的版本,查看 最新版本

// ==UserScript==
// @icon        https://github.com/favicon.ico
// @name        【自用】YoutubeTools
// @namespace   Violentmonkey Scripts
// @match *://*.youtube.com/*
// @grant       none
// @version     2021.09.03
// @author      heckles
// @description 1.恢复页面布局,2.增加按钮,点击后复制一条命令行到剪切板(配合IDM使用)【受YouTube™ Multi Downloader启发,需配合Local YouTube Downloader使用】,3.增加按钮,一键重绘下载png格式的cover
// @Homepage URL http://greasyfork.icu/zh-CN/scripts/431488-%E8%87%AA%E7%94%A8-youtubetools
// ==/UserScript==


//1.设定加载条件
if (document.getElementById("browser-app") || document.getElementById("masthead")) { //这两个元素,有一个是true,就往下执行
    var sx = setInterval(function () { //间隔执行
        if (window.location.href.indexOf("watch?v=") < 0) { //如果网址不匹配
          return false; //就不执行         【这里只能匹配域名,然后筛,直接用watch的网址,从首页点进去会不触发】
        }
        if (document.getElementById("meta-contents") && document.getElementById("punisher") === null) { //网址匹配的话,punisher没有被添加
          StartJS(); //就执行函数,添加punisher
          console.log(">>>>>>>>>>>               YoutubeTools      已加载          <<<<<<<<<<<");
          clearInterval(sx);
        }      
    }, 1000); //间隔时间1000毫秒
    //return;
}

//2.条件触发后加载
function StartJS() {
    //2.1新增按钮的样式
    const btncss = `
  color: #F97D00;
  font-weight: bold;
  /*text-transform: uppercase;*/
  padding: 0px 3px;
  background-color: transparent;
  border-color: transparent;
`
    //2.2开始添加按钮
    var buttonDiv = document.createElement("span");
    buttonDiv.id = "punisher";
    buttonDiv.style.width = "100%";
    buttonDiv.style.marginTop = "3px";
    buttonDiv.style.padding = "1px 0";
    var addButtonV = document.createElement("button");
    var addButtonA = document.createElement("button");
    var aCover = document.createElement("a");
    addButtonV.appendChild(document.createTextNode("Vcode"));
    addButtonA.appendChild(document.createTextNode("Acode"));
    aCover.appendChild(document.createTextNode("Cover"));
    addButtonV.style.cssText = btncss;
    addButtonA.style.cssText = btncss;
    aCover.style.cssText = btncss;
    buttonDiv.appendChild(addButtonV);
    buttonDiv.appendChild(addButtonA);
    buttonDiv.appendChild(aCover);
    var targetElement = document.querySelectorAll("[id='info-text']"); //youtube故意的,很多元素id重复,这里够绝,直接全选中,然后按class筛,再加
    if (targetElement) {
        for (var i = 0; i < targetElement.length; i++) {
            if (targetElement[i].className.indexOf("style-scope ytd-video-primary-info-renderer") > -1) {
                targetElement[i].appendChild(buttonDiv);
            }
        }
    }

//3.创建一个input,但是不显示(通过移位),作为复制的中介
    var nMInput = document.createElement('input');
    nMInput.style.cssText = "position:absolute; top:-200px;"; //火狐实测隐藏的话不能选,oInput.style.display='none';
    document.body.appendChild(nMInput);
//4.生成文件名
    var nM_V = '"' + document.querySelector("#container h1 yt-formatted-string").innerText + ' - DASH_V' + '"' + '.mp4';
    var nM_A = '"' + document.querySelector("#container h1 yt-formatted-string").innerText + ' - DASH_A' + '"' + '.m4a';
//5.生成封面图的地址和名称
    var src_J = document.querySelector("#container div.ytp-cued-thumbnail-overlay-image").style.cssText.slice(23, -3);
    var nM_J = document.querySelector("#container h1 yt-formatted-string").innerText + src_J.split("default")[1];
    aCover.target = "_blank";
//6.IDM下载命令行所需
    const ds1 = `"D:\\Programs\\Internet Download Manager"\\idman.exe /n /d "`
    const ds2 = `" /f `
    //7.1视频   
    addButtonV.onclick = function () {//按钮加event //shadowroot的mode必须是open,否则没有ShadowDOM
        var xroot = document.getElementById("hahahazijijiade");
        //console.log(xroot.shadowRoot.children[0]);
        var linkss = xroot.shadowRoot.children[0].children[2].children[1].children[1];
        var ku = linkss.querySelectorAll("a");
        if (ku) {
            for (var i = 0; i < ku.length; i++) {
                if (linkss.children[i].innerText.indexOf("1080p") > -1 && linkss.children[i].innerText.indexOf("video/mp4") > -1 && linkss.children[i].innerText.indexOf("avc1.6") > -1) { //用=0就不行,用>-1就行...,如果没有,就是-1
                    nMInput.value = ds1 + linkss.children[i].href + ds2 + nM_V;
                    console.log(nMInput.value);
                }
            }
        }
        nMInput.select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令,火狐里面这个command只能是用户触发,不能自动
    };
    //7.2音频
    addButtonA.onclick = function () {//按钮加event //shadowroot的mode必须是open,否则没有ShadowDOM
        var xroot = document.getElementById("hahahazijijiade");
        var linkss = xroot.shadowRoot.children[0].children[2].children[1].children[1];
        var ku = linkss.querySelectorAll("a");
        if (ku) {
            for (var i = 0; i < ku.length; i++) {
                if (linkss.children[i].innerText.indexOf("audio/mp4") > -1) { //用=0就不行,用>-1就行...,如果没有,就是-1
                    nMInput.value = ds1 + linkss.children[i].href + ds2 + nM_A;
                    console.log(nMInput.value);
                }
            }
        }
        nMInput.select(); // 选择对象
        document.execCommand("Copy"); // 执行浏览器复制命令,火狐里面这个command只能是用户触发,不能自动
    };
//7.3封面
    //网上找的点击下载图片的,原理是canvas重绘
    aCover.onclick = function(){
      function dIamge(xhref, name) {      
        var image = new Image();
        image.setAttribute('crossOrigin', 'anonymous'); // 解决跨域 Canvas 污染问题
        image.src = xhref;
        image.onload = function () {           
            var canvas = document.createElement('canvas');
            canvas.width = image.width;
            canvas.height = image.height;
            var context = canvas.getContext('2d');
            context.drawImage(image, 0, 0, image.width, image.height);
            var url = canvas.toDataURL('image/png');
            var a = document.createElement('a'); // 生成一个a元素
            var event = new MouseEvent('click'); // 创建一个单击事件
            a.download = name || '下载图片名称'; // 将a的download属性设置为我们想要下载的图片名称,若name不存在则使用‘下载图片名称’作为默认名称
            a.href = url; // 将生成的URL设置为a.href属性
            a.dispatchEvent(event); // 触发a的单击事件
            aCover.style.color = "#444";
            aCover.innerHTML = window.location.href.split("?v=")[1];
        }
      };
      dIamge(src_J, nM_J);
    };
}