BeihaiZhang / 微信公众号视频 助手

// ==UserScript==
// @license           GPL-3.0-or-later; http://www.gnu.org/licenses/gpl-3.0.txt
// @name              微信公众号视频 助手
// @namespace         http://tampermonkey.net/
// @version           0.10
// @description       发现微信公众号页面 的视频, 生成下载链接
// @author            BeihaiZhang
// @create            2020-12-01
// @lastmodified      2020-12-01
// @match             http*://mp.weixin.qq.com/s*
// @grant             GM_openInTab
// @grant             GM_download
// @grant             GM_addStyle

// ==/UserScript==

(function() {
    'use strict';


    var hasOpenAriac2Tab = false;
    let win_title = document.getElementsByTagName("h2")[0].innerText.replace(' ','');
    GM_addStyle(".hide_btn { display:none; }");
    GM_addStyle(".download_btn,.aria_download_btn {min-width:300px;min-height:30px;background:#ff0;color:#f00;font-size:120%;text-align:center;line-height:30px;cursor:pointer;padding:10px;border-radius:10px;border:1px solid #f00;}");


    // 1. 获取视频列表
    // 2. append下载按钮
    // 3. 设置下载动作

    //自定义 log 函数
    function log(param1,param2){
        param1 = param1 ? param1 : "";
        param2 = param2 ? param2 : "";
        console.log("#微信公众号视频助手-log:",param1,param2);
    }

    setTimeout(function(){

        let qr_div = document.getElementsByClassName('qr_code_pc')[0];

        let checkbox = document.createElement('input');
        checkbox.setAttribute("id", "aria_checkbox");
        checkbox.setAttribute("type", "checkbox");
        checkbox.addEventListener("change",function(){
            let abtns = document.getElementsByClassName('aria_download_btn');
            let k = 0;
            for(k = 0; k<abtns.length; k++){
                abtns[k].classList.toggle("hide_btn");
            }
        });
        qr_div.appendChild(checkbox);

        let selectbox_text = document.createElement('p');
        selectbox_text.innerHTML = "选中启用aria按钮";
        qr_div.appendChild(selectbox_text);

        // 1. 获取视频列表
        var video_frame_list = document.getElementsByClassName("video_iframe");
        // 2. append下载按钮
        var i = 0;
        for( i = 0; i < video_frame_list.length ; i++){
            let cur_video_frame = video_frame_list[i];
            let idx = i;
            let tips=document.createElement('div');
            // tips.style="min-width:300px;min-height:30px;background:#ff0;color:#f00;font-size:120%;text-align:center;line-height:30px;cursor:pointer;padding:10px;border-radius:10px;border:1px solid #f00;";
            tips.innerHTML="下载视频-"+(idx+1);
            tips.setAttribute("class", "download_btn");
            tips.addEventListener("click",function(){
                downloadDirectly(idx);
            });
            cur_video_frame.after(tips);


            let adownload=document.createElement('div');
            adownload.innerHTML="ariaNg 下载视频-"+(idx+1);
            adownload.classList.toggle("aria_download_btn");
            adownload.classList.toggle("hide_btn");
            adownload.addEventListener("click",function(){
                downloadDirectly(idx,true);
            });
            tips.after(adownload);
        }

    },2000); // 页面加载完成后2秒执行


    function downloadDirectly(iframe_idx, aria_flag){
        let cur_frame = document.getElementsByClassName("video_iframe")[iframe_idx];
        // 获取视频地址, 页面上的视频 需要点击播放,才能获取到视频播放地址
        var iframe_document = cur_frame.contentWindow.document;
        var js_btn_play = iframe_document.getElementsByClassName("js_btn_play")[0];
        js_btn_play.click();
        setTimeout(function(){
            var js_switch = iframe_document.getElementsByClassName("js_switch")[0];
            js_switch.click();
            var video = iframe_document.getElementsByTagName("video")[0];
            var video_src = video.getAttribute("src");
            let title = win_title+"-"+(iframe_idx+1);
            // window.parent.postMessage(video_src,"*");
            if(!aria_flag){
                GM_download(video_src,title+'.mp4');
            }else{
                sendDownloadTaskToAria2(video_src,title+'.mp4');
            }

        },1000);
    }

    // aria2c远程服务调用
    // https://ariang.mayswind.net/zh_Hans/command-api.html
    function sendDownloadTaskToAria2(download_url,file_name){
        let url_encoded = window.btoa(download_url);
        // 看起来, AriaNg必须要打开控制页面,才能执行完下载操作
//         if (!hasOpenAriac2Tab){
             GM_openInTab('http://192.168.2.1/aria2c.html/#!/new/task?url='+url_encoded+'&out='+file_name,{active:true});
//             hasOpenAriac2Tab = true;
//         }else {
//            console.log('http://192.168.2.1/aria2c.html/#!/new/task?url='+url_encoded+'&out='+file_name);
//             GM_xmlhttpRequest({
//                 method: 'GET',
//                 url: 'http://192.168.2.1/aria2c.html/#!/new/task?url='+url_encoded+'&out='+file_name
//             })
//         }
    }
})();