rule34video-downloader

One-click download rule34video

// ==UserScript==
// @name         rule34video-downloader
// @namespace    http://tampermonkey.net/
// @name:zh-CN   rule34video下载器
// @version      1.2
// @description  One-click download rule34video
// @description:zh-CN  一键下载rule34视频
// @author       素问
// @match        https://rule34video.com/videos/*
// @match        https://rule34video.com/*
// @connect      *.rule34video.com/remote_control/*
// @icon         https://rule34video.com/favicon1.ico
// @grant        GM_xmlhttpRequest
// @grant        GM_download
// @license      MIT License
// ==/UserScript==

(function () {
    // Your code here...

    let video_url = '';
    let video_name = '';

    /*
    分为两种情况
    一种是video detail详细页面,触发该方法
    一种是视频列表预览页面点击弹窗形式触发方法。
    */
    let target1,target2;
    window.onload = function () {
        let current_url = window.location.href;
        if(current_url.search("^https://rule34video.com/videos")!= -1){
            addDownBtn();
        }else if(current_url.search("https://rule34video.com/")!=-1){
            target2 = setInterval(function(){
                var fancybox = document.querySelector(".fancybox-overlay,.fancybox-overlay-fixed");
                if(fancybox){
                    console.log("fancybox on");
                    if(target1 == undefined){
                        target1 = setInterval(function(){ doVideo() }, 1000);
                    }
                }else{
                    console.log("fancybox off");
                    if(target1 != undefined){
                        clearInterval(target1);
                        target1 = undefined;
                    }
                }
            },1000);
        }
    }

    // 定时查询页面上是否有video元素
    function doVideo(){
        var video = document.querySelector('video.fp-engine');
        if(video){
            clearInterval(target1);
            addDownBtn();
        }else{
            console.log("未查询到指定video");
        }
    }

    // 添加下载按钮
    function addDownBtn(){
        // 视频
        var video = document.querySelector('video.fp-engine');
        video_url = video.src;
        video_name = document.querySelector('.title_video').innerText;
        // 菜单栏
        var menu = document.querySelector('.panel,.tabs-menu');
        // 下载按钮
        var downBtn = document.createElement('li');
        downBtn.setAttribute("class", "btn-favourites tab");
        downBtn.innerHTML = '<a href="javascript:void();" class="toggle-button button_fav download">Download</a>';
        menu.appendChild(downBtn);

        let a = document.querySelector("a.download");
        a.addEventListener("click", download);
    }

    // start pause  continue

    function download() {
        let a = document.querySelector("a.download");
        let downloadStatus = a.getAttribute("download");
        let downloadJob = GM_download({
                name: video_name + ".mp4",
                url: video_url,
                header: { "Content-Type": "video/mp4" },
                saveAs: true,
                onload: function (res) {
                    a.style.backgroundColor = "#98FB98";
                    a.innerText = "Download Success";
                },
                onprogress: function (res) {
                    //let size = res.totalSize;
                    //let load = res.loaded;
                    //let a = document.querySelector("a.download");
                    //a.innerText = parseFloat((load/size) * 100).toFixed(2) + "%";
                    a.innerText = "Downloading...";
                },
                onerror: function (msg) {
                    console.log(msg)
                }
            });
    }
})();