My JSON functions to insert anchor, video, audio

Insert into head tag:
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js'/>

<script>
//<![CDATA[

function dataURItoBlob(dataURI, callback) {
    // convert base64 to raw binary data held in a string
    // doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
    var byteString = atob(dataURI.split(',')[1]);

    // separate out the mime component
    var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]

    // write the bytes of the string to an ArrayBuffer
    var ab = new ArrayBuffer(byteString.length);
    var ia = new Uint8Array(ab);
    for (var i = 0; i < byteString.length; i++) {
        ia[i] = byteString.charCodeAt(i);
    }

    // write the ArrayBuffer to a blob, and you're done
    var bb = new Blob([ab], {
        type: mimeString
    });
    return bb;
}

// detect IE browser
var isIE = false;
if (navigator.userAgent.indexOf('MSIE') !== -1 || navigator.appVersion.indexOf('Trident/') > 0 || window.navigator.userAgent.indexOf("Edge") > -1) {
    isIE = true;
}

// insert mp4 video
function mp4id(v, id) {
    $.getJSON("/feeds/posts/default/" + v + "?alt=json", function(data) {
        $(id).append("<source src='" + data.entry.content.$t + "' type='video\/mp4'><a href='" + data.entry.content.$t + "'>" + data.entry.title.$t + "</a>");
    });
};

// insert any kind of file
function dl(postid, insert) {
    if (!isIE) {
        $.getJSON("/feeds/posts/default/" + postid + "?alt=json", function(data) {
            $(insert).append("<a href='" + data.entry.content.$t + "' download='" + data.entry.title.$t + "'>" + data.entry.title.$t + "</a>");
        });
    } else {
        $.getJSON("/feeds/posts/default/" + postid + "?alt=json", function(data) {
            $(insert).append("<a href='javascript:void(0);' onClick='javascript:window.navigator.msSaveBlob(dataURItoBlob(&quot;" + data.entry.content.$t + "&quot;), &quot;" + data.entry.title.$t + "&quot;);'>" + data.entry.title.$t + "</a>");
        });
    }
};

// insert mp3 file
function mp3id(v, id) {
    $.getJSON("/feeds/posts/default/" + v + "?alt=json", function(data) {
        $(id).append("<source src='" + data.entry.content.$t + "' type='audio\/mpeg'>");
    });
};

//]]>
</script>
Insert into body tag:
<!-- insert video -->
<div class="c"><video id="7736911013445444045" controls poster="" class="b"></video><script>mp4id("7736911013445444045","#7736911013445444045");</script></div>

<!-- insert mp3 -->
<audio id="1485063402237621389" controls></audio><script>mp3id("1485063402237621389","#1485063402237621389");</script>

<!-- insert additional file -->
<div id="4320230787029059361"></div><script>dl("4320230787029059361","#4320230787029059361");</script>

<!-- insert additional file and actually show the code -->
<div id="4320230787029059361"></div><script>ac("4320230787029059361","#4320230787029059361");</script>

<!-- show external output without direct download -->
<div id="4288502077795028058"></div><script>out("4288502077795028058","#4288502077795028058");</script>
Output:
Video sample:
Audio sample:
File sample:
Show the content:
Output content:

No comments: