// ==UserScript== // @icon https://yt3.ggpht.com/yti/APfAmoHlInyEAGUifsNIIVF28iHTEz_Sfafu-PjCl0Ll=s88-c-k-c0x00ffffff-no-rj-mo // @name 【自用】YoutubeTools // @namespace Violentmonkey Scripts // @match *://*.youtube.com/* // @grant none // @version 2021.08.27 // @author heckles // @description 1.恢复页面布局,2.增加按钮,点击后复制一条命令行到剪切板(配合IDM使用)【受YouTube™ Multi Downloader启发,需配合Local YouTube Downloader使用】 // @downloadURL none // ==/UserScript== if (document.getElementById("browser-app") || document.getElementById("masthead")) { //这两个元素,有一个是true,就往下执行 setInterval(function () { //间隔执行 if (window.location.href.indexOf("watch?v=") < 0) { //如果网址不匹配 return false; //就不执行 } if (document.getElementById("meta-contents") && document.getElementById("punisher") === null) { //网址匹配的话,punisher没有被添加 StartJS(); //就执行函数,添加punisher } }, 1000); //间隔时间1000毫秒 return; } function StartJS() { const btncss = ` color: #fff; text-transform: uppercase; padding: 2px 6px; background-color: transparent; border-color: transparent; ` //开始添加按钮 var buttonDiv = document.createElement("span"); buttonDiv.id = "punisher"; buttonDiv.style.width = "100%"; buttonDiv.style.marginTop = "3px"; buttonDiv.style.padding = "10px 0"; var addButtonV = document.createElement("button"); var addButtonA = document.createElement("button"); addButtonV.appendChild(document.createTextNode("视频名")); addButtonA.appendChild(document.createTextNode("音频名")); addButtonV.style.cssText = btncss; addButtonA.style.cssText = btncss; buttonDiv.appendChild(addButtonV); buttonDiv.appendChild(addButtonA); 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); } } } //创建一个input,但是不显示(通过移位),作为复制的中介 var nMInput = document.createElement('input'); nMInput.style.cssText = "position:absolute; top:-200px;"; //火狐实测隐藏的话不能选,oInput.style.display='none'; document.body.appendChild(nMInput); //先生成文件名 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'; //IDM下载命令行所需 const ds1 = `"D:\\Programs\\Internet Download Manager"\\idman.exe /n /d "` const ds2 = `" /f ` //按钮加event //shadowroot的mode必须是open,否则没有ShadowDOM addButtonV.onclick = function () { 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: video/mp4") > -1) { //用=0就不行,用>-1就行...,如果没有,就是-1 nMInput.value = ds1 + linkss.children[i].href + ds2 + nM_V; console.log(nMInput.value); } } } nMInput.select(); // 选择对象 document.execCommand("Copy"); // 执行浏览器复制命令,火狐里面这个command只能是用户触发,不能自动 }; //按钮加event //shadowroot的mode必须是open,否则没有ShadowDOM addButtonA.onclick = function () { 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只能是用户触发,不能自动 }; }