<?xml version="1.0" encoding="utf-8"?>
<parser delay="500" referer="true" date="2020.11.29">
    <url><![CDATA[https?://(?:v\.)?huya\.com/play/(?<id>[^.]+)\.html?]]></url>
    <title><![CDATA[<h1[^>]+>([^<]+)</h1>]]></title>
    <thumbnail><![CDATA[data-img="([^"]+)"]]></thumbnail>
    <duration><![CDATA[duration=([^,]+)]]></duration>
    <mediaUrl><![CDATA[cr_video_url=([^\$]+)]]></mediaUrl>
    <jsCode><![CDATA[
        try {
            var video_url = '';
            var duration = '';
            var vq = parseInt((window.external.vq && window.external.vq.match(/\d+/) ? window.external.vq : '1080').match(/\d+/)[0]);
            var vid = this.vid;
            var api_url = 'https://v-api-player-ssl.huya.com/?r=vhuyaplay%2Fvideo&format=mp4%2Cm3u8&vid=' + vid + '&_=' + Date.now();
            var data = window.external.getHtmlCode(api_url);
            if (data) {
                var jo = JSON.parse(data);
                video_url = jo.result.items[jo.result.items.length - 1].transcode.urls[0];
                for (let idx in jo.result.items) {
                    var video = jo.result.items[idx];
                    duration = video.duration;
                    if (video.height == vq) {
                        video_url = video.transcode.urls[0];
                        break;
                    }
                }
            }
            if (!video_url) {
                var m = document.body.innerHTML.match(/<video[^>]+src=\"([^\"]+)/);
                if (m) {
                    video_url = m[1];
                    duration = document.body.innerHTML.match(/(\d+:\d+)<\/span>\s+<\/div>/)[1];
                }
            }

            document.body.innerHTML = 'duration=' + duration + ',cr_video_url=' + video_url + '$' + document.body.innerHTML;
        }
        catch (ex) {
        }
    ]]></jsCode>
</parser>