抓取公众号文章里面视频

元初芸提供标准化的零售行业标准SAAS,为您便捷、快速地完成微信相关公众号、小程序的快速部署!! https://console.tebieshuang.xyz

Fra 06.10.2020. Se den seneste versjonen.

// ==UserScript==
// @name         抓取公众号文章里面视频
// @namespace    https://console.tebieshuang.xyz
// @supportURL   https://console.tebieshuang.xyz
// @version      4.1.0
// @description  元初芸提供标准化的零售行业标准SAAS,为您便捷、快速地完成微信相关公众号、小程序的快速部署!! https://console.tebieshuang.xyz
// @author       Leo
// @include        *://mp.weixin.qq.com/s/*
// @include        *://mp.weixin.qq.com/s?*
// @include        *//v.qq.com/x/page/*
// @include        *//v.qq.com/x/cover/*
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @grant        GM_download
// @grant        GM_setClipboard
// @grant        unsafeWindow
// @grant window.onurlchange
// ==/UserScript==

(function() {
    'use strict';

GM_addStyle(`

#video-data-src-mask{display:flex;position:fixed;top:0;left:0;z-index:99999999;height:100vh;width:100vw;background:rgba(0,0,0,0.9);color:#fff;font-weight:bold;font-size:18px;}
#video-data-src-mask-inner{width:100%;height:100%;position:relative;}
#video-data-src-main-inner{width:100%;height:100%;margin:auto;top:50px;left:0;right:0;bottom:0;position:absolute;}
.video-data-src-title{width:100%;text-align:center}
.video-data-src-link{color:#fff;}
#video-data-src-close{width:100%;text-align:center;margin-top:100px;}
#closeVideoMask{color:#fff;cursor:pointer;}
.yuanchuyun-text-center{color:#fff;text-align:center;}
#yuanchuyun-margin-top{margin-top:50px;}
`);

    function closeMask(){
        let el = document.querySelector( '#video-data-src-mask' );
        el.parentNode.removeChild( el );
    }

    function showDetail( title, media, image ){
        let filename = media.split('?')[0].split('/').slice(-1).pop();
        GM_setClipboard(title);
        GM_download({url:media,name: filename + '.mp4'});
        GM_download({url:image,name: filename + '.jpg'});
        let html = '<div id="video-data-src-mask"><div id="video-data-src-mask-inner"><div id="video-data-src-main-inner"><div class="video-data-src-title">标题:' + title + '</div><div class="video-data-src-title">视频:<a class="video-data-src-link" href="' + media + '" target="_blank">点击查看</a></div><div class="video-data-src-title">图片:<a class="video-data-src-link" href="' + image + '" target="_blank">点击查看</a><div class="yuanchuyun-text-center" id="yuanchuyun-margin-top">元初芸温馨提醒您,您的视频和图片已经自动下载,视频标题已经复制好,直接去发布即可!!</div></div><div id="video-data-src-close"><span id="closeVideoMask" href="javascript:;">点击这里关闭</span></div></div></div></div>', e = document.createElement('div');
        e.innerHTML = html;
        document.body.appendChild( e );
    }
    function addCloseListener(){
        setTimeout( function(){
            document.querySelector('#closeVideoMask').addEventListener('click', function(){
                closeMask();
            }, false);
        }, 1000 );
    }
    function getVid( url ){
        //v.qq.com/x/page/
        //v.qq.com/x/cover/
        if( url.substr( 0, 24 ) == 'https://v.qq.com/x/page/' || url.substr(0, 25 ) == 'https://v.qq.com/x/cover/' ){
            return url.split('?')[0].split('/').slice(-1).pop().replace('.html','');
        }
        return '';
    }
    function getVideo( vid, title, image ){
        window.jsonpCallback_video = function (data) {
           let video = data.vl.vi[0];
           let media= 'https://ugcws.video.gtimg.com/' + video.fn + '?vkey=' + video.fvkey;
            image = image || 'https://puui.qpic.cn/qqvideo_ori/0/' + vid + '_496_280/0';
            title = title || video.ti;
           showDetail( title, media, image );
           addCloseListener();
        };
        var callback = "jsonpCallback_video", url = "http://vv.video.qq.com/getinfo?vids="+vid+"&platform=101001&charge=0&otype=json&callback=" + callback;
        //var s = document.createElement("script");
        //s.src = url;
        //document.body.appendChild(s);
        GM_xmlhttpRequest({method:"GET",url:url,onload: function(response){if (response.status == 200){eval(response.responseText);}}});
    }
    function downloadVideo(){
        setTimeout( function(){
            let ifs = document.querySelectorAll('iframe.video_iframe'), vds = document.querySelectorAll('.js_inner video'), ss = document.querySelector('iframe[src^="//v.qq.com/"]'), title = document.querySelector('meta[property="og:title"]').getAttribute('content');
            if( ifs.length > 0 ){
                if(ifs[0].hasAttribute('data-mpvid')&&(!ifs[0].hasAttribute('data-vidtype')||ifs[0].getAttribute('data-vidtype')!=2)){
                    getVideo(ifs[0].getAttribute('data-mpvid'), title);
                }else{
                    let domDocument = ifs[0].contentDocument, thumb = domDocument.querySelector('.poster_cover').style.backgroundImage, media = domDocument.querySelector('video').getAttribute('origin_src'), image = thumb.substring(5, thumb.length-2);
                    showDetail( title, media, image );
                    addCloseListener();
                }
            }else if( vds.length > 0 ){
                let thumb = document.querySelectorAll('.js_poster_cover')[0].style.backgroundImage, media = vds[0].getAttribute('origin_src'), image = thumb.substring(5, thumb.length-2);
                showDetail( title, media, image );
                addCloseListener();
            }else if( ss != null ){
                //imageElement = iframeBody.querySelector('[data-role="txp-ui-poster"]')
                let videoUrl = ss.getAttribute('src'), videoInfo = videoUrl.split("&vid=")[1].split("&")[0], iframeBody = null, imageElement = null, image;
                if( imageElement !== null ){
                    image = imageElement.style.backgroundImage.replace(/(url\(|\)|")/g, '');
                }
                getVideo(videoInfo, title, image );
            }else if( location.hostname == 'v.qq.com' ){
                let tvid = getVid(location.href);
                if( tvid != '' ){
                    getVideo(tvid, title );
                }
            }
        }, 4000 );
    }
    downloadVideo();
    if (window.onurlchange === null) {
        window.addEventListener('urlchange', function(){
            downloadVideo();
        });
    }
})();