Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to embed github code into an iframe?

Tags:

github

iframe

The question may seem confusing so let me clarify.

Github lets you see the source code of a files in a repo. I want to include them in iframes but am unsure how and suspect someone has already done this before.

In my case I want to add https://github.com/ileathan/hubot-mubot/blob/master/src/mubot.coffee to an iframe so that from my website people can see the code as it evolves.

like image 888
Banned_User Avatar asked Feb 05 '15 07:02

Banned_User


People also ask

What can you embed with iframe?

Iframes are most often used to embed specific content from one web page — like a video, form, document, or even a full web page — within a different web page. This is a powerful capability in HTML — you can take any content from any website (with permission) and place it on your own site to enhance your content.


1 Answers

Here's a concrete example of how this can be done via the GitHub API. We request the encoded content and insert it directly into the iframe.

<iframe id="github-iframe" src=""></iframe>
<script>
    fetch('https://api.github.com/repos/ileathan/hubot-mubot/contents/src/mubot.coffee')
        .then(function(response) {
            return response.json();
        }).then(function(data) {
            var iframe = document.getElementById('github-iframe');
            iframe.src = 'data:text/html;base64,' + encodeURIComponent(data['content']);
        });
</script>

Here's the code in action https://jsfiddle.net/j8brpdsg/2/

like image 172
AlexG Avatar answered Oct 02 '22 01:10

AlexG