Pornhub视频下载

Pornhub视频下载与链接显示

2023-09-23 기준 버전입니다. 최신 버전을 확인하세요.

// ==UserScript==
// @name         Pornhub视频下载
// @name:en      Pornhub website video download
// @namespace    http://tampermonkey.net/
// @version      0.7
// @description  Pornhub视频下载与链接显示
// @description:en Pornhub video download and link display
// @author       geigei717
// @license      MIT
// @match        https://*.pornhub.com/view_video.php?viewkey=*
// @icon         https://greasyfork.s3.us-east-2.amazonaws.com/fc67t00gsk98w7pbhs97xr94g1hl
// @grant        unsafeWindow
// @grant        GM_download
// @grant        GM_xmlhttpRequest
// @require      https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js
// ==/UserScript==

(function() {
    url()
    function url(){
        var video_id = $("#player").data("video-id");
        var urlJson = eval("flashvars_"+video_id).mediaDefinitions
        console.log(urlJson)
        $(".video-actions-menu").after('<div class="myporhub" style="color: red;font-size: 13px;"><div class="mp4Url">视频链接:<textarea style="width: 90%;height:50px;background-color: black;color: white;padding: 0 5px; margin-left: 65px;margin-top: -15px;"  onclick="this.focus();this.select()" readonly="readOnly"></textarea></div><div class="mp4Donwload" style="height: 40px;line-height: 40px;" >视频下载:<div  style="width: 90%;display: -webkit-flex;display: flex;-webkit-justify-content: space-around;justify-content: space-around;margin-top: -40px;margin-left: 10%;" ></div></div>')
        var masterUrl = urlJson.pop().videoUrl
        console.log(masterUrl)
        $.getJSON(masterUrl, function(data){
            console.log(data)
            var goodV ;
            for(let i=0;i<data.length;i++){
                var s = data[i]
                //if(s.defaultQuality==true){
                //     $(".mp4Url").children("textarea").val(s.videoUrl)
                //}
                if( i > 0 && parseInt( s.quality ) < parseInt( data[i-1].quality ) ){
                    goodV = data[i-1].videoUrl
                }else{
                    goodV = s.videoUrl
                }
                if(s.format="mp4"){
                    $(".mp4Donwload>div").append('<input type="button" style="width: 15%;height: 40px;border-radius: 7px;border: none;background-color: #048011;color: white;text-align: center;cursor: pointer;" onclick="downloadUrlFile(\''+s.videoUrl+'\',this)" class="'+s.quality+'p" value="'+s.quality+'p" >')
                }else{
                    $(".mp4Donwload>div").append('<input type="button" style="width: 15%;height: 40px;border-radius: 7px;border: none;background-color: #7d7e85;color: white;text-align: center;" data-src="'+s.videoUrl+'" class="'+s.quality+'p" title="非MP4格式,暂不能下载" disabled value="'+s.quality+'p" >')
                }
            }
            $(".mp4Url").children("textarea").val(goodV)
        })
    }

    unsafeWindow.GM_D = [];
    /**
     * 下载
     * @param url 文件url
     */
    unsafeWindow.downloadUrlFile = function (url,obj){
        var th = $(obj),name = $(".inlineFree").text()+"_"+th.val()+".mp4",start = th.attr("onclick");
        th.val("解析中")
        //console.log(th)
        url= url.replace(/\\/g, '/');
        var request = [];
        var blob = [];
        var loadSize = [];
        var xhrs = 0
        var num = -1
        GM_xmlhttpRequest({
            method: "HEAD",
            fetch: true,
            url: url,
            onerror: function(x) {
                th.val("错误").attr('onclick',start).attr("title","下载出错")
            },
            onload: function(response) {
                if( response.status /100 >=4){
                    th.val("错误").attr('onclick',start).attr("title","下载出错")
                    return;
                }
                var Length =response.responseHeaders.match(/content-length:\s*[\d]+\s/im)
                if(Length==null||Length==undefined||Length==""){
                    request.push(GM_download({
                        url: url,
                        name: name,
                        onprogress : function (event) {
                            if (event.lengthComputable) {
                                var loaded = parseInt(event.loaded / event.total * 100);
                                if(loaded==100){
                                    $(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl"+u).css("display","none");
                                }else{
                                    $(that).next(".StopSaveUrl"+u).text(loaded+"%");
                                }
                            }
                        },
                        onload : function () {
                            $(that).text("下载").css("display","inline-block").attr("title","下载").next(".StopSaveUrl"+u).css("display","none").text("0%");
                        },
                        onerror : function () {
                            $(that).text("错误").css("display","inline-block").attr("title","下载出错。").next(".StopSaveUrl"+u).css("display","none").text("0%");
                        }
                    }))
                    num =GM_D.push(request)
                    $(that).next('.StopSaveUrl'+u).data('num',num-1);
                    return;
                };
                Length = parseInt(Length[0].match(/\d+/)[0]);
                //console.log("video")
                console.log("mp4视频下载中ing。")
                var RangeSize = parseInt((Length/5).toFixed(0))
                for(var i=0,z=0;i<Length;i=i+RangeSize,z++){
                    var range_start=i,range_end=i+RangeSize-1;
                    if (range_end+1>=Length) {range_end = Length}
                    //console.log(range_start,range_end)
                    eval('function onprogress'+z+' (event){'+
                         'loadSize['+z+'] = event.loaded;'+
                         'var x =0;'+
                         'loadSize.forEach(function(item){'+
                         '    x = x + item'+
                         '});'+
                         'var loaded = parseInt(x / Length * 100);'+
                         'if(loaded==100){'+
                         '     th.val("下载完成").attr("onclick",start).attr("title","下载");;'+
                         '}else{'+
                         '     th.val("下载中:"+loaded+"%"); '+
                         //'     console.log(loaded+"%")'+
                         '}'+
                         '}'+
                         'request['+z+'] = GM_xmlhttpRequest({'+
                         'method: "GET",'+
                         'url: url,'+
                         'fetch: false,'+
                         'responseType: "arraybuffer",'+
                         'headers: { "Range":"bytes="+range_start+"-"+range_end},'+
                         'onprogress: onprogress'+ z +','+
                         'onload: function(response) {'+
                         '    blob[' + z + '] = new Blob([response.response]);' +
                         '    xhrs++;'+
                         '    var x=0;y=0;' +
                         '    loadSize.forEach(function(item){' +
                         '        x = x + item' +
                         '    });' +
                         '    blob.forEach(function(item){'+
                         '        y = y +item.size'+
                         '    });'+
                         '    console.log(y+" "+ x+" "+Length);'+
                         '    if (x == Length && y ==Length) {' +
                         //'       console.log(x,xhrs);'+
                         '       var link = document.createElement("a");' +
                         '       link.href = window.URL.createObjectURL(new Blob(blob));' +
                         '       link.download = name;' +
                         '       link.click();' +
                         '       link.remove();' +
                         '       th.val("下载完成").attr("onclick",start).attr("title","下载");;'+
                         '    }' +
                         '},'+
                         'onabort: function(){'+
                         'console.log("error!");'+
                         '},'+
                         'onerror: function(x) {'+
                         'console.log("error!更换线路ing");'+
                         'request.forEach(function(item){'+
                         '   item.abort()'+
                         '});'+
                         'request['+z+'] = GM_download({'+
                         '    url: url,'+
                         '    name: name,'+
                         '    onprogress : function (event) {'+
                         '        if (event.lengthComputable) {'+
                         '            var loaded = parseInt(event.loaded / event.total * 100);'+
                         '            if(loaded==100){'+
                         '                th.val("下载完成").attr("onclick",start).attr("title","下载");;'+
                         '            }else{'+
                         '                th.val("下载中:"+loaded+"%")'+
                         '            }'+
                         '        }'+
                         '    },'+
                         '    onload : function () {'+
                         '        th.val("下载完成").attr("onclick",start).attr("title","下载");'+
                         '    },'+
                         '    onerror : function () {'+
                         '        th.val("错误").attr("onclick",start).attr("title","下载出错");'+
                         '    }'+
                         '});'+
                         'var numi = parseInt( th.data("num") );'+
                         'GM_D[numi] = request;'+
                         '},'+
                         '});')
                }
                //console.log(request)
                num =GM_D.push(request)
                th.attr('onclick','StopdownloadUrlFile(this)').data('num',num-1)

            }
        })
    }

    //下载暂停
    unsafeWindow.StopdownloadUrlFile = function (obj){
        var num = $(obj).data("num")
        GM_D[num].forEach(function(item){
            item.abort()
        })
        $(obj).data("num","").val("继续").attr("title","下载中断").attr("onclick",start)
    }


})();