Greasy Fork

Greasy Fork is available in English.

抖音时间控制

自用抖音时间管理+其他优化(想要实现但未实现:下载视频:替换广告(已实现):替换超过5分钟的视频(已实现,但必须是先加载好的,刷太快可能就没有效果):5分钟后搜索视频上提醒10s后遮罩,一个小数只能刷2次(已实现):替换直播

目前为 2023-03-21 提交的版本。查看 最新版本

// ==UserScript==
// @name         抖音时间控制
// @description  自用抖音时间管理+其他优化(想要实现但未实现:下载视频:替换广告(已实现):替换超过5分钟的视频(已实现,但必须是先加载好的,刷太快可能就没有效果):5分钟后搜索视频上提醒10s后遮罩,一个小数只能刷2次(已实现):替换直播
// @namespace    http://tampermonkey.net/
// @version      2023.3.21
// @author       仰晨
// @match        *://*.douyin.com/*
// @icon         https://lf1-cdn-tos.bytegoofy.com/goofy/ies/douyin_web/public/favicon.ico
// @grant        none
// @license      AGPL License
// ==/UserScript==

//抖音最近默认进来是静音的,直接解除静音(muted)// 每秒检查 video 是否存在2023.3.14
const intervalId = setInterval(() => {
    const videoElements = document.getElementsByTagName("video");
    if (videoElements.length > 0 ) {
        videoElements[0].muted = false;
        clearInterval(intervalId); // 停止定时器
    }
}, 1000);


var min5 = false;
//监听 鼠标滚轮滚动2023.3.9
// 选取要观察的节点
var targetNode = document;
// 监听鼠标滚轮事件
targetNode.addEventListener('wheel', function(event) {
    if (event.deltaY > 0) {//如果是向下滚动就管2023.3.15
        if(min5){//alert('5分钟到啦');
            //如果打开了消息就不管2023.3.15
            if(document.getElementsByClassName("TNoRtROB").length > 0){
                console.log("打开了消息--不管");
            } else {
                // 关闭网页,如果关闭失败就跳转到指定主页
                window.opener = null; window.open('', '_self'); window.close();window.location.href = "https://www.jianfast.com/";
            }
        }
        //如果没打开了消息就管2023.3.21
        if(document.getElementsByClassName("TNoRtROB").length === 0){
            //前五分钟滚动判定代码(直播修改思路:video标签 带src的就是了
            setTimeout(function (){
                let videos = document.getElementsByTagName("video");


                //如果视频有src标签,说明不是一个正常视频,就点向下的会员  或时间时长超过5分钟2023.3.21
                if(videos[Math.floor(videos.length/2)].hasAttribute("src")||videos[Math.floor(videos.length/2)].duration>300){
                    // 向下点击的元素
                    const element = document.querySelector('.xgplayer-playswitch-next');
                    // 如果找到元素,则模拟点击事件
                    if (element){
                        element.click();
                        // 创建一个滚轮事件 分发事件到文档中2023.3.21
                        document.dispatchEvent(new WheelEvent('wheel', {deltaY: 100}));
                    }
                }

                let fuClass=document.getElementsByClassName("zK9etl_2 slider-video");//视频标签的父级标签
                if(fuClass.length>2){
                    let Advertising = fuClass[2].getElementsByClassName("RA5iG98_");//广告
                    let Shopping = fuClass[2].getElementsByClassName("Lnsyat3L"); //购物推荐视频 2023.3.12
                    let VideoDuration = fuClass[2].getElementsByClassName("time-duration"); //视频时长
                    // 设置每半秒检查一次的定时器2023.3.14
                    const timer = setInterval(() => {
                        if(VideoDuration.length>0){
                            const videoDuration = VideoDuration[0];
                            console.log('视频时长:'+videoDuration.innerText);
                            if (videoDuration.innerText > '00:00') {
                                // 加载完成后执行的代码
                                clearInterval(timer); // 停止计时器

                                if(Advertising.length>0){replacVideo("广告");
                                                        }else if(Shopping.length>0){replacVideo("购物");
                                                                                   }else if(VideoDuration[0].innerText>"05:00"){replacVideo("超长");} //document.getElementsByTagName("video")[2].duration//这样也可以获取视频长度的秒
                            } else {
                                console.log('视频还未加载完成...');
                            }
                        }else {//如果视频时长标签不存在也没必要
                            console.log('——————视频加载完成——————');
                            clearInterval(timer); // 停止计时器
                        }
                    }, 400); // 每半秒检查一次
                }
            },400)
        }
    }

});


//替换视频
function replacVideo(tip) {
    let videos = document.getElementsByTagName("video");
    let index=videos.length != 1 ? 2 : 0;
    if(index>1){
        let video = videos[index];
        // 判断是否存在src属性
        if (!video.hasAttribute("src")){
            video.src = '//www.douyin.com/aweme/v1/play/?video_id=v0200fg10000cg2650rc77u1uog8eaig&line=0&file_id=ea45ae18d426468187776aaacaed6f30&sign=6cdbe8be7c6a55eb9a95a38bc7194185&amp'; // 设置视频URL
            video.load(); // 加载视频

            //如果视频出现直播的话会没有这些标签.所以《视频标签》==《正常视频父类标签》是就是没有出现直播 就执行2023.3.9
            let fuClass=document.getElementsByClassName("zK9etl_2 slider-video");
            if(index+1==fuClass.length){
                let fu=fuClass[index];
                //隐藏弹幕(失败了:因为不会提前加载只会滚动到视频的时候才出现,点击下方的关闭样式又会影响到其他的,现在想法就到五分钟直接吧弹幕的样式none)2023.3.8 [class="I6WvmKj6 E8wnDxIb danmu"]
                //隐藏左边的点赞那栏2023.3.8
                fu.getElementsByClassName("L1TH4HdO positionBox")[0].style.display = "none";
                //隐藏名字和描述2023.3.8
                fu.getElementsByClassName("tSXOCvQc")[0].style.display = "none";
                //替换评论区
                replacComment(index,tip);
            }
        }
    }
}


//替换评论区2023.3.7
function replacComment(index,tip) {
    let Comment=document.getElementsByClassName("aDQw7OWI _628BWQES LookModalFrameFast")[index];
    Comment.innerHTML="ikun:"+tip;
    let styles=`font-size: 78px;
                color: white;
                justify-content: center; /* 水平居中 */
  		        align-items: center;     /* 垂直居中 */
                `
    Comment.style=styles;

    // 包涵集合的评论区得隐藏
    let set=Comment.getElementsByClassName("nNXxvS1t");
    if(set.length>0){
        set[0].style="display:none;";
    }
}


//次数控制(用newBing生成修改2023.3.11)
function countControl() {
    //"use strict"是一个字面量表达式,它表示代码应该在严格模式下执行。
    //'use strict';

    // 获取当前时间的小时数
    let hour = new Date().getHours();
    console.log("当前时间的小时数"+hour);

    // 获取本地存储中保存的上一次访问抖音的小时数和次数
    let lastHour = localStorage.getItem("lastHour");
    let count = localStorage.getItem("count");
    console.log("@@@@@@@@@@@@@@之前的小时数"+lastHour+" 次数"+count);

    // 如果本地存储中没有数据,则初始化为0
    if (lastHour === null || count === null) {
        lastHour = count = 0;
    }else {
        // 把字符串转换成数字
        lastHour = parseInt(lastHour);
        count = parseInt(count);
    }

    // 如果当前时间和上一次访问抖音的时间不在同一个小时内,则重置计数器为0
    if (hour !== lastHour) {
        count = 0;
    }

    // 增加计数器,并保存到本地存储中
    localStorage.setItem("lastHour", hour);
    localStorage.setItem("count", count+1);

    // 如果计数器大于等于2,则不给看了
    if (count >= 2) {
        // 替换body的内容
        let body = document.querySelector("body");
        body.innerHTML = "2次了";
        body.setAttribute("style", "font-size: 200px; color: red; font-family: monospace;left: 33%;top: 30%;position: fixed;");
        //关闭网页,如果失败就跳转到指定主页
        setTimeout(function (){window.opener = null; window.open('', '_self'); window.close();
                               window.location.href = "https://www.jianfast.com/";
                              },500)
    }
}



//进入的是刷视频页才执行↓↓↓
if (location.href === "https://www.douyin.com/"){
    //次数控制(用newBing生成修改2023.3.11)
    countControl();

    //加载完成时自行↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
    window.onload=function (){
        //1.5秒后自动打开评论区
        setTimeout(function (){document.getElementsByClassName("tzVl3l7w")[0].click();},1500)

        //加载完成时自行-↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
    }

    //chat生成  说明快捷键:2023.3.10
    {
        let shortcut = document.createElement('div');
        shortcut.classList.add('shortcut');

        let label = document.createTextNode('快捷键');
        shortcut.appendChild(label);

        let infoBox = document.createElement('div');
        infoBox.classList.add('shortcut-info');
        shortcut.appendChild(infoBox);

        let keys = ['W:上一个视频', 'S:下一个视频', 'A:后退进度条', 'D:前进进度条', 'X:打开/关闭评论', 'G:关注/取关作者', 'Z:点赞/取消小红心'];
        for (let i = 0; i < keys.length; i++) {
            let p = document.createElement('p');
            let text = document.createTextNode(keys[i]);
            p.appendChild(text);
            infoBox.appendChild(p);
        }
        document.getElementById('root').appendChild(shortcut);
    }


    var 延迟时间=5*60*1000;
    //开始计时===============================================================================================
    setTimeout(function (){
        //赋初始值
        var i = 1;
        var sj="00:00"
        var videoTime=document.getElementsByClassName('time-current');
        var index=0;//如果只刷一个视频就只有一个索引,开始第二个的时候就会出现三个索引【0,1,2】当前一般是【1】
        min5 = true;


        var div = document.createElement("div");
        div.innerText="5分钟到了-----------------------------------------------------";
        div.setAttribute('id','kun'); //设置标签属性
        var fu = document.getElementById("slidelist");//找到想成为的父标签
        var one=fu.firstChild;// 获取父标签的第一个子元素
        fu.insertBefore(div,one); // 在父标签的第一个子元素,前插入div标签

        //改一下样式
        {
            let style=`
	ul.JTui1eE0{
        transition: all 20s ;
		width: 100px;
		background-color: #5f6175a8;
		border-radius: 8px;
		margin: 0px 10px;
		box-shadow: 0 0px 9px 1px #fff;							 /*阴影     水平偏移 垂直偏移  模糊  阴影大小*/
	}
    #kun{
        width:100%;
        height: 100%;
        animation:ikun 5s infinite;
    }
    @keyframes ikun {
    	0% {background-color: red;}
    	50% {background-color: #06f;}
		100% {background-color: red;}
	}


    [class="xgplayer-playswitch recommend-out-switch-btn IvL3R0Q4"],  /*隐藏右边点击上下看视频按钮*/
    [class="I6WvmKj6 E8wnDxIb danmu"]                                 /*隐藏弹幕*/
    {
        display: none !important;
    }
    `;
            let ele=document.createElement("style");
            ele.innerHTML=style;
            document.getElementsByTagName('head')[0].appendChild(ele);
        }


        //判断退出代码↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
        for (let i=0; i<3600; i++)task(i);


        function task(i) {setTimeout( function () {
            try{
                if (videoTime.length===3){
                    index=1; //获取视频个数   要么是1个要么是3个 三个的时候 当前看的是第二个
                    // 到时间了 替换视频
                    replacVideo("够钟");
                }

                //判断这一秒的时间是否大过上一秒的时间  大过说明看完了 该润了
                if(sj>videoTime[index].innerText){
                    alert("够钟死心了");
                    window.opener = null; window.open('', '_self'); window.close();
                    //这种情况是偶尔才会出现,没有自动关闭,就让声音隐藏
                    setTimeout(function (){
                        document.getElementsByTagName("video")[0].muted = true;
                    },1000)
                }
                //刷新时间值
                sj=videoTime[index].innerText;
                document.getElementsByClassName("time")[index].innerHTML = "---------5分钟到了+"+i+"秒"+sj+">?"+videoTime[index].innerText;i++;
            }catch(err){
                console.log("离谱");
            }
        }, 1000 * i);}

    },延迟时间);//在5分钟后执行代码--------------------------------------------------------------------------------------------------------
}//进入的是刷视频页才执行↑↑↑↑

//个人主页生效----------------------------
else if(location.href.startsWith("https://www.douyin.com/user")){
    //添加一个到刷视频的按钮
    const parent = document.querySelector("#root");
    const newLink = document.createElement("a");
    newLink.setAttribute("href", "https://www.douyin.com/");
    newLink.setAttribute("id", "ikun");
    newLink.setAttribute("title", "到刷视频页面");
    parent.appendChild(newLink);
}

//样式-|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|-|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|-
{
    let css = "";
    /*整个抖音域名都生效*/
    if ((location.hostname === "douyin.com" || location.hostname.endsWith(".douyin.com"))) {
        css += `
            /*眼不见为净*/
            [class="n9PPTk22"],              /*左边栏底下的说明*/
            [class="DsdpRAYf fP8n2eAM"],    /*私信通知里面的下载抖音*/
            [class="DsdpRAYf n4H8QMkw"],   /*私信通知里面的下载抖音*/
			#douyin-sidebar		          /*右下角的意见反馈*/
			{
				display: none !important;
			}

            /*左边栏设置*/
            [data-e2e="douyin-navigation"],   /*抖音的左边栏内容*/
            .HQwsRJFy,		                 /*左上 抖音 logo */
			.N_HNXA04.KYtgzo9m{		        /*抖音大大的左边栏*/
				transition: all 0.5s ;	   /*过渡效果2023.1.31*/
				max-width: 5px;
			}
		 	

			/*移动到左边就弹出来*/
			.N_HNXA04.KYtgzo9m:hover .HQwsRJFy,
			.N_HNXA04.KYtgzo9m:hover [data-e2e="douyin-navigation"],
            [class="N_HNXA04 WcK6IrkT JVPLvXh3"]:hover [data-e2e="douyin-navigation"], /*搜索页面特别一点*/
			.N_HNXA04.KYtgzo9m:hover{
				max-width:172px;
				display: block;
			}

		`;
    }

    /*个人主页生效-----------------------------*/
    if (location.href.startsWith("https://www.douyin.com/user")) {
        css += `
            [class="VgThzTk9 vwAQ8Y04"],            /*观看历史-今天 漂浮*/
            [class="bbec3J8x m5e7YGwM"],            /*视频|影视综 观看历史漂浮*/
            .MFb3tP0s._bpGHYWu > :nth-child(-n+3),  /*前3个直接子元素  1、客户端 2、收藏网页 3、合作*/
            [class="feXqXaSk a3sjxRqS"],            /*收藏夹|视频|合集*/
			[class="iwzpXgQ3 lXuWkeYW dSNgkU25 K4TZD9ct wJgU15d1"],	/*滚动还显示搜索栏*/
			[class="gX8AKvzT gthle0Gj"],			/*滚动还显示作品栏*/
			[class="_7gdyuNUv"]					/*主页的下载电脑客户端*/
			{
		        display: none !important;           /*使用!important可以覆盖所有其他样式规则,但不建议过度使用*/
			}

			/*背景全黑----------------------------------*/
            [class="VgThzTk9"],                         /*观看历史-今天*/
            [class="bbec3J8x"],                         /*视频|影视综*/
            .feXqXaSk,                                  /*收藏夹|视频|合集*/
			[class="gX8AKvzT"],						    /*作品栏*/
			[class="UKp0VfC1"],						    /*下面的视频背景*/
			html[dark] ._LDrC7Wb.Smb5gBZJ:before{	    /*名片*/
				background-image: linear-gradient(to bottom, #000, #000);
			}


			/*使用!important可以覆盖所有其他样式规则,但不建议过度使用。例如:
			.N_HNXA04 {
		  	background-color: red !important;
			}
			但是,使用!important可能会导致样式难以维护和调试,因此应该尽量避免使用它。*/


            /*生成的到刷视频按钮的样式*/
            #ikun {
	               z-index: 503;
  	  	           position: absolute;
 	  	           top: 5px;
  	  	           left: 5px;
 	  	           width: 39px;
 	  	           height: 39px;
 	  	           border-radius: 50%;
 	  	           text-align: center;
 	  	           line-height: 30px;
 	  	           background-color: #4a4949;
 	           	   box-shadow: 0 0 5px #999;
 	           	   cursor:pointer;
		           background-image: url(https://www.douyin.com/favicon.ico);
		           background-repeat: no-repeat;
		           background-size: 100%;
	      }
		`;
    }

    /*刷视频  搜索视频 页面生效  */
    if (location.href === "https://www.douyin.com/" || location.href.startsWith("https://www.douyin.com/search/")) {
        css += `
            [class="XNmfpct4 fJOjbqAw"],            /*直播间名字 标题*/
            [class="NWMyy49m"],                     /*点击进入直播间*/
            [class="U6kxNmZU"],                     /*直播叫你买浮窗*/
            [class="QUUswvJ3"],                     /*客户端*/
			[class="JTui1eE0"]:nth-child(2),		/*收藏网页*/
			[class="JTui1eE0"]:nth-child(3) {		/*合作*/
		  		display: none !important;
			}
		`;
    }

        /*刷视频 页面生效  */
    if (location.href === "https://www.douyin.com/") {
        css += `
            /*评论区广告提交表单*/
            .htirveP0:before{
	                  display: flex;
		              min-width: 60px;
		              min-height: 680px;
		              color: #929faf;
		              content: "ikun:广告已被覆盖";
		              justify-content: center; /* 水平居中 */
  		              align-items: center; /* 垂直居中 */
		              font-size: 40px;
	          }


            /*说明快捷键---------样式--------------*/
            .shortcut {
		             z-index: 501;
  		             position: fixed;
  		             right: 15px;
  	             	 top: 90px;
  	             	 width: 40px;
  	             	 height: 40px;
	             	 opacity: 30%;
  	             	 border-radius: 50%;
  	             	 color: #fff;
  	             	 font-size: 10px;
  	             	 line-height: 40px;
  	             	 cursor: pointer;
		             box-shadow: 0 0px 6px 3px #7b849c87;		 /*阴影     水平偏移 垂直偏移  模糊  阴影大小*/
		             transition: all 0.5s ;			            /*过渡效果2023.1.31*/
	             }
                .shortcut:hover,
	            .shortcut:hover .shortcut-info {
	             	opacity: 80%;
               		display: block;
	             }
	             .shortcut-info {
               		display: none;
  	             	position: absolute;
  	             	top: 40px;
  	             	right: 0px;
  	             	width: 130px;
  	             	padding: 10px;
  	             	border-radius: 5px;
  	             	background-color: rgb(242, 242, 242);
  	             	font-size: 12px;
  	             	color: #6a5acd;
  	             	z-index: 999;
  	             	text-align: left; /* 文字靠左 */
  	             	line-height: 1.2; /* 设置行高 */
	             }
		`;
    }

    if (typeof GM_addStyle !== "undefined") {
        GM_addStyle(css);
    } else {
        let styleNode = document.createElement("style");
        styleNode.appendChild(document.createTextNode(css));
        (document.querySelector("head") || document.documentElement).appendChild(styleNode);
    }
}
//样式结束-|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|--|-