<?xml version="1.0" encoding="utf-8"?>
<parser delay="1500" referer="true" date="2020.12.19">
    <url><![CDATA[https?://(?:www\.)?youtu(?:be)?\.(?:com|be)/(?:watch\?v=)?(?<id>[^&?]+)]]></url>
    <title><![CDATA[title="([^"]+)"]]></title>
    <thumbnail><![CDATA[thumbnail=([^,]+)]]></thumbnail>
    <duration><![CDATA[duration=([^,]+)]]></duration>
    <subtitle><![CDATA[subtitle=([^,]+)]]></subtitle>
    <mediaUrl><![CDATA[cr_video_url=([^\$]+)]]></mediaUrl>
    <delegate><![CDATA[about:blank]]></delegate>
    <jsCode><![CDATA[
        try {
            var title = '', thumbnail = '', duration = '', subtitle = '', video_url = '';
            var api_url = 'https://qdownloader.io/download?url=' + encodeURIComponent(window.external.url);
            var data = window.external.getHtmlCode(api_url, 'https://qdownloader.io/');
            if (data.match(/<span\s+class=\"title[^>]+>([^<]+)/))
                title = data.match(/<span\s+class=\"title[^>]+>([^<]+)/)[1];
            if (data.match(/src=\"([^\"]+ytimg[^\"]+)/))
                thumbnail = data.match(/src=\"([^\"]+ytimg[^\"]+)/)[1];
            if (data.match(/Duration:\s+<\/span>([^<]+)/))
                duration = data.match(/Duration:\s+<\/span>([^<]+)/)[1];
            if (data.match(/downloadBtn\"\s+download=[^>]+href=\"([^\"]+)/))
                video_url = data.match(/downloadBtn\"\s+download=[^>]+href=\"([^\"]+)/)[1];

            if (!video_url) {
                //全局方可
                api_url = 'https://www.clipconverter.cc/check.php';
                args = 'mediaurl=' + encodeURIComponent(window.external.url);
                data = window.external.getPostResult(api_url, args, 'application/x-www-form-urlencoded');
                if (data) {
                    data = data.replace(/,\"playercode.*?iframe>\"/, '');
                    var jo = JSON.parse(data);
                    title = jo.id3title || jo.filename;
                    thumbnail = jo.thumb;
                    for (var v in jo.url) {
                        if (jo.url[v].checked) {
                            video_url = jo.url[v].url;
                            break;
                        }
                    }
                }
            }

            //subtitle
            if (video_url && window.external.sub_lang) {
                var api_url = 'https://downsub.com/?url=' + encodeURIComponent(window.location.href);
                data = window.external.getHtmlCode(api_url);
                if (data.indexOf('context=') != -1) {
                    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) {
                            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');
                            }

                            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;
                        }
                    }
                }
            }

            title = title.replace(/,/g, '_');
            document.body.innerHTML = 'title=\"' + title + '\",thumbnail=' + thumbnail + ',duration=' + duration + ',subtitle=' + subtitle + ',cr_video_url=' + video_url + '$';
        }
        catch (ex) {
        }
    ]]></jsCode>
</parser>