抓取公众号文章里面视频

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

当前为 2020-10-07 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         抓取公众号文章里面视频
// @namespace    https://console.tebieshuang.xyz
// @supportURL   https://console.tebieshuang.xyz
// @version      4.2.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/*
// @include        *//v.qq.com/*
// @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 ){
        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 );
           addCloseListener();
        };
        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( 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 );
                }
            }
    }
    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()});
                }
            }else{
               downloadVideo();
            }
        },3000);
    }
    doIt();
    if (window.onurlchange === null) {
        window.addEventListener('urlchange', function(){
            doIt();
        });
    }
})();