<?xml version="1.0" encoding="utf-8"?>
<parser date="2020.12.09">
    <title><![CDATA["title":\s*"([^"]+)"]]></title>
    <thumbnail><![CDATA["image":\s*"([^"]+)"]]></thumbnail>
    <duration><![CDATA["duration":\s*([^}]+)]]></duration>
    <subtitle><![CDATA[subtitle=([^\$]+)]]></subtitle>
    <medias><![CDATA[\{"format":\s+"(?<format>[^"]+)",\s+"quality":\s+"(?<quality>[^"]*)",\s+"size":\s+(?<file_size>[^,]+),\s+(?:"has_audio":\s+(?<has_audio>[^,]*),\s+)?"url":\s+"(?<url>[^"]+)"\}]]></medias>
    <delegate><![CDATA[about:blank]]></delegate>
    <jsCode><![CDATA[
        try {
            var html = '', data = '';
            //...2020.12.08测试其不能用
            /*var api_url = 'https://api.online-downloader.com/DL/dd.php?videourl=' + encodeURIComponent(window.external.url.replace('youtube', 'onlineonlineonline'));
            api_url += '&mstr=' + parseInt(Math.random() * 10000) + '&_' + Date.now();
            data = window.external.getHtmlCode(api_url, 'https://vk.online-downloader.com/');
            if (data && data.indexOf('(') == 0)
                data = data.substr(1, data.length - 2);
            if (data && data.indexOf('\"Video_DownloadURL\":null') == -1) {
                var jo = JSON.parse(data);
                data = '\"title\": \"' + jo.Video_Title + '\", \"image\": \"' + jo.Video_Image + '\", \"duration\": ' + jo.Video_Time;
                var i = 0;
                //音频置前
                while (true) {
                    i++;
                    var vf = 'Video_' + i;
                    if (!jo[vf + '_Ext'])
                        break;
                    if (jo[vf + '_WH'] != 'audio')
                        continue;

                    data += '{\"format\": \"' + jo[vf + '_Ext'] + '\", \"quality\": \"' + jo[vf + '_WH'] + '\", \"size\": ' + jo[vf + '_File_Size'] + ', \"has_audio\": , \"url\": \"' + jo[vf + '_Url'] + '\"},';
                }
                i = 0;
                //视频
                while (true) {
                    i++;
                    var vf = 'Video_' + i;
                    if (!jo[vf + '_Ext'])
                        break;
                    if (jo[vf + '_WH'] == 'audio')
                        continue;

                    var has_audio = jo[vf + '_Acodec'] != 'none';
                    data += '{\"format\": \"' + jo[vf + '_Ext'] + '\", \"quality\": \"' + jo[vf + '_WH'] + '\", \"size\": ' + jo[vf + '_File_Size'] + ', \"has_audio\": ' + has_audio + ', \"url\": \"' + jo[vf + '_Url'] + '\"},';
                }
                html = data;
            }*/

            if (!html) {
                data = window.external.getHtmlCode('https://keepv.id');
                var sid = data.match(/sid='([^']+)/)[1];
                var args = 'url=' + encodeURIComponent(window.external.url) + '&sid=' + sid;
                var headers = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\n\Cookie: PHPSESSID=' + sid;
                data = window.external.getPostResultEx('https://keepv.id', args, headers);
                if (data.indexOf('>Download Video<') != 0) {
                    var title = data.match(/<h2\s+class=\"mb-3\">([^<]+)/)[1];
                    var thumbnail = data.match(/<img\s+src=\"([^\"]+default\.[^\"]+)\"/)[1];
                    var duration = data.match(/Duration:\s+([^<]+)/)[1];
                    html = '\"title\": \"' + title + '\", \"image\": \"' + thumbnail + '\", \"duration\": ' + duration + '$';

                    //formats...不用名字分组，原因在IE中，名字分组会报[错误的数量词]
                    var p = new RegExp('<tr><td>([^>]*)</td><td[^>]+><span[^>]+>([^<]*)</span></td><td[^>]+>([^<]*)</td><td[^>]+><a[^>]+href=\"([^\"]+).*?</td></tr>', 'gi');
                    while ((m = p.exec(data)) != null) {
                        var has_audio = m[0].indexOf('btn btn-sm btn-outline-success shadow') == -1;
                        if (!m[1])
                            has_audio = '';
                        html += '{\"format\": \"' + m[2] + '\", \"quality\": \"' + m[1] + '\", \"size\": ' + m[3] + ', \"has_audio\": ' + has_audio + ', \"url\": \"' + m[4] + '\"},';
                    }
                    html = html.substr(0, html.length - 1);
                }
            }

            if (!html) {
                var vid = window.external.url.match(/[=\/]([0-9A-Za-z_-]{11})(?:[\?&]|$)/)[1];
                var referer = 'https://www.y2mate.info/downloads/' + vid + '/';
                args = 'url=' + vid;
                headers = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8\r\nReferer: ' + referer;
                data = window.external.getPostResultEx('https://www.y2mate.info/downl0ader/', args, headers);
                for (let i = 0; i <= 2; i++) {
                    if (data.indexOf('data') != -1) {
                        var jo = JSON.parse(data);
                        //音前视后，程序如是处理
                        if (jo.data && jo.data.data && jo.data.data.audios) {
                            var audios = jo.data.data.audios;
                            for (var a in audios) {
                                for (var m in audios[a]) {
                                    var media = audios[a][m];
                                    html += '{\"format\": \"' + media.format + '\", \"quality\": \"' + media.quality + '\", \"size\": ' + media.size + ', \"has_audio\": , \"url\": \"' + media.url + '\"},';
                                }
                            }
                        }
                        if (jo.data && jo.data.data && jo.data.data.videos) {
                            var videos = jo.data.data.videos;
                            for (var v in videos) {
                                for (var m in videos[v]) {
                                    var media = videos[v][m];
                                    html += '{\"format\": \"' + media.format + '\", \"quality\": \"' + media.quality + '\", \"size\": ' + media.size + ', \"has_audio\": ' + media.has_audio + ', \"url\": \"' + media.url + '\"},';
                                }
                            }
                        }
                        break;
                    }

                    args = 'url=' + window.external.url;
                    if (i == 1)
                        headers += '\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36';
                    else if (i == 2)
                        headers += '\r\nX-Requested-With: XMLHttpRequest';
                    data = window.external.getPostResultEx('https://www.y2mate.info/downl0ader/', args, headers);
                }
            }

            var subtitle = '';
            if (window.external.sub_lang) {
                var api_url = 'https://downsub.com/?url=' + encodeURIComponent(window.external.url);
                data = window.external.getHtmlCode(api_url);
                if (data.indexOf('context=') != -1) {
                    var lang = window.external.sub_lang;
                    if (lang == 'Default' || lang == 'All') {
                        lang = navigator.language;
                        lang = lang.replace('zh-CN', 'zh-Hans').replace('zh-TW', 'zh-Hant').replace('zh-HK', 'zh-Hant');
                    }
                    var ct = atob(data.match(/context='([^\']+)/)[1]);
                    var jo = JSON.parse(ct);
                    if (jo.id) {
                        var id = jo.id;
                        data = window.external.getHtmlCode('https://get-info.downsub.com/' + id, api_url);
                        if (data) {
                            jo = JSON.parse(data);
                            for (let i in jo.subtitles) {
                                //default->en
                                var sub_code = jo.subtitles[i].code;
                                sub_code = sub_code.replace('zh-CN', 'zh-Hans').replace('zh-TW', 'zh-Hant').replace('zh-HK', 'zh-Hant');
                                if (!subtitle && sub_code == 'en')
                                    subtitle = jo.subtitles[i].url;
                                if (lang.indexOf(sub_code) != -1) {
                                    subtitle = jo.subtitles[i].url;
                                    break;
                                }
                            }
                            if (!subtitle && jo.subtitlesAutoTrans.length != 0) {
                                for (let i in jo.subtitlesAutoTrans) {
                                    var sub_name = jo.subtitlesAutoTrans[i].name;
                                    var sub_code = sub_name.replace('English', 'en').replace('Chinese Simplified', 'zh-Hans').replace('Chinese Traditional', 'zh-Hant');
                                    if (lang.indexOf(sub_code) != -1) {
                                        subtitle = jo.subtitlesAutoTrans[i].url;
                                        break;
                                    }
                                }
                            }
                            if (subtitle)
                                subtitle = 'https://subtitle.downsub.com/?title=' + lang + '&url=' + subtitle;
                        }
                    }
                }
            }

            document.body.innerHTML = 'subtitle=' + subtitle + '$' + html;
        }
        catch (ex) {
        }
    ]]></jsCode>
</parser>