抓取公众号文章里面视频

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

目前为 2020-12-01 提交的版本。查看 最新版本

// ==UserScript==
// @name         抓取公众号文章里面视频
// @namespace    https://console.tebieshuang.xyz
// @supportURL   https://console.tebieshuang.xyz
// @version      4.2.6
// @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/*
// @include       *//v.qq.com/*
// @grant        GM_addStyle
// @grant        GM_xmlhttpRequest
// @grant        GM_download
// @grant        GM_setClipboard
// @grant        unsafeWindow
// @grant        window.onurlchange
// @run-at       document-start
// ==/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;}
        #video-vid-btn{color:#fff;}
    `);

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

    function copyTo( str ){
        navigator.clipboard.writeText(str).then(function() { alert( "拷贝成功" ); }, function() {alert( "拷贝失败" );});
    }

    function showDetail( title, media, image, vid ){
        //let filename = media.split('?')[0].split('/').slice(-1).pop();
        title = title.replace(/([\uE000-\uF8FF]|\uD83C[\uDF00-\uDFFF]|\uD83D[\uDC00-\uDDFF])/g, '').replace('_腾讯视频','');
        let filename = title, vidTip = vid ? '视频VID是    ' + vid + '    <a id="video-vid-btn" data-vid="' + vid + '" href="javascript:;">点击这里复制</a>' : '没有找到这个视频的VID,请先上传视频到腾讯视频以获取VID';
        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><div class="video-data-src-title">' + vidTip + '</div><div class="yuanchuyun-text-center" id="yuanchuyun-margin-top">元初芸温馨提醒,视频和图片将会自动下载,视频标题已经复制好,届时直接去发布即可!!</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);
        }, 800 );
    }
    function addCopyListener(){
        setTimeout( function(){
            if(document.querySelector('#video-vid-btn')!==null){
                document.querySelector('#video-vid-btn').addEventListener('click', function(){
                    copyTo(document.querySelector('#video-vid-btn').getAttribute('data-vid'));
                }, false);
            }
        }, 900 );
    }
    function getVid( url ){
        if( url.toLowerCase().substr( 0, 24 ) == 'https://v.qq.com/x/page/' || url.toLowerCase().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, vid );
           addCloseListener();
            addCopyListener();
        };
        var callback = "jsonpCallback_video", url = "http://vv.video.qq.com/getinfo?vids="+vid+"&platform=101001&charge=0&otype=json&callback=" + callback;
        GM_xmlhttpRequest({method:"GET",url:url,onload: function(response){if (response.status == 200){eval(response.responseText);}}});
    }
    function downloadVideo(){
            let ifs = document.querySelectorAll('iframe.video_iframe'), vds = document.querySelectorAll('.js_inner video'), ss = document.querySelector('iframe[src^="//v.qq.com/"]'), titleElement = document.querySelector('meta[property="og:title"]'), title;
            if( titleElement !== null ){
                title = titleElement.getAttribute('content');
            }
            if( unsafeWindow.__mpVideoTransInfo ){
                let media = unsafeWindow.__mpVideoTransInfo[0].url.replace("\x26amp;","&"), image = unsafeWindow.__mpVideoCoverUrl;
                showDetail( title, media, image );
                addCloseListener();
            }else 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') || vds[0].getAttribute('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 );
                }
            }
    }
    function doIt(){
        setTimeout(function(){
            if( window.location.href.replace('https://','').replace('http://','').substr(0,19).toLowerCase() == 'v.qq.com/txp/iframe' ){
                let poster = window.document.querySelector('#video_container.txp_player .txp_poster');
                if( poster !== null ){
                    let posterImage = poster.style.backgroundImage.replace(/(url\(|\)|")/g, '').toLowerCase();
                    if( posterImage.substr(0,4)!='http' ){
                        posterImage = 'https:' + posterImage;
                    }
                    GM_download({url:posterImage,name:posterImage.split('?')[0].split('/').slice(-1).pop()+'.jpg'});
                }
            }else{
               downloadVideo();
            }
        },3000);
    }
    doIt();
    if (window.onurlchange === null) {
        window.addEventListener('urlchange', function(){
            doIt();
        });
    }
})();