﻿<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" />
    <title></title>
    <script>
        function getSameOriginIframeIndex(){
            var sameOrigIframe;
            for(var i = 0; i< window.parent.frames.length; i++){
                try{
                    var href = window.parent.frames[i].location.href
                    if(href.indexOf("myiframeplaybycrossdomain")==-1){
                        sameOrigIframe = window.parent.frames[i].document;
                    }
                } catch(e){}
            }
            return sameOrigIframe;
        }
        var theIntervalCount = 0;
        var interval = setInterval(function videoTagFinder() {
            var iframeDocument = getSameOriginIframeIndex();
            console.log("检查video_" + theIntervalCount + "_" + iframeDocument);
            if (iframeDocument == null) {
                return;
            }
            var videoTags = iframeDocument.querySelectorAll("video");
            if (videoTags.length == 0 && theIntervalCount < 10) {
                theIntervalCount++;
            } else {
                clearInterval(interval);
            }
            if (videoTags.length > 0) {
                for (var index = 0; index < videoTags.length; index++) {
                    videoTags[index].pause();
                    videoTags[index].addEventListener("play", onVideoPlayEvent, true);
                    console.log("检查video_" + videoTags[index].src);
                }
            } else {
                console.log("检查iframe");
                var iframeObj = iframeDocument.querySelectorAll("iframe");
                for (var i = 0; i < iframeObj.length; i++) {
                    if (iframeObj[i].offsetHeight > 0) {
                        buildCrossDomainIframe(iframeObj[i].src, iframeDocument);
                    }
                }
            }
        
            function onVideoPlayEvent(e) {
                this.pause();
                e.stopPropagation();
            }
        }, 300);

        function buildCrossDomainIframe(mySrc, objDocument) {
            var myIframeTag = "myiframeplaybycrossdomain";
            var myObj = objDocument.querySelectorAll("." + myIframeTag);
            for (var i = 0; i < myObj.length; i++) {
                if (myObj[i].src == mySrc + myIframeTag) {
                    return;
                }
            }
            var obj = objDocument.createElement("iframe");
            obj.src = mySrc + myIframeTag;
            obj.className = myIframeTag;
            obj.style.display = "none";
            objDocument.body.appendChild(obj);
        }
    </script>
</head>
<body>
</body>
</html>