Skip to content

Uvacoder/html-preview-tool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GIT HTML PREVIEW TOOL .

-----------------------------------------------------

TOC
AboutHow It WorksDevelopmentContact

-----------------------------------------------------

GitJavaScriptCSS3HTML5BootstrapBitbucket

-----------------------------------------------------

Usage:

demo

GIT HTML PREVIEW TOOL was tested under the latest Google Chrome and Mozilla Firefox for GitHub & BitBucket

Still working on getting it to render html hosted on GitLabb

-----------------------------------------------------

GIT HTML PREVIEW TOOL

What it does is: load HTML using CORS proxy, then process all links, frames, scripts and styles, and load each of them using CORS proxy, so they can be evaluated by the browser.

Live Deployment

[Live Website]

-----------------------------------------------------

➤ About

Many GitHub repositories don't use GitHub Pages to host their HTML files. GIT HTML PREVIEW TOOL allows you to render those files without cloning or downloading whole repositories. It is a client-side solution using a CORS proxy to fetch assets.

If you try to open raw version of any HTML, CSS or JS file in a web browser directly from GitHub, all you will see is a source code. GitHub forces them to use the "text/plain" content-type, so they cannot be interpreted. This script overrides it by using a CORS proxy.


Cross-Origin Resource Sharing (CORS) is a security mechanism that uses additional HTTP headers to tell browsers to give a web application running at one origin, access to selected resources from a different origin. A web application executes a cross-origin HTTP request when it requests a resource(Images, Scripts, CSS files, etc.)that has a different origin (domain, protocol, or port) from its own.

Cross-origin requests, however, mean that servers must implement ways to handle requests from origins outside of their own. CORS allows servers to specify who (i.e., which origins) can access the assets on the server, among many other things.

Access-Control-Allow-Origin Header When Site A tries to fetch content from Site B, Site B can send an Access-Control-Allow-Origin response header to tell the browser that the content of this page is accessible to certain origins. (An origin is a domain, plus a scheme and port number.) By default, Site B's pages are not accessible to any other origin; using the Access-Control-Allow-Origin header opens a door for cross-origin access by specific requesting origins. The Access-Control-Allow-Origin header is critical to resource security. You can find a description of each CORS header at the following: CORS Headers. Pre-Flight Request Most servers will allow GET requests but may block requests to modify resources on the server. Servers don't just blindly block such requests though; they have a process in place that first checks and then communicates to the client (your web browser) which requests are allowed. When a request is made using any of the following HTTP request methods, a standard preflight request will be made before the original request. PUT DELETE CONNECT OPTIONS TRACE PATCH Preflight requests use the OPTIONS header. The preflight request is sent before the original request, hence the term "preflight." The purpose of the preflight request is to determine whether or not the original request is safe (for example, a DELETE request). The server will respond to the preflight request and indicate whether or not the original request is safe. If the server specifies that the original request is safe, it will allow the original request. Otherwise, it will block the original request.


➤ Bryan Guner


(Full-stack software developer)


PortfolioResume PDFBryan's emailBlogLinkedinAngelListGitHub bgoonz


-----------------------------------------------------

logo


-----------------------------------------------------

➤ Development:

(() => {
  
  const previewForm = document.getElementById('previewform');

  const url = location.search.substring(1).replace(/\/\/github\.com/, '//raw.githubusercontent.com').replace(/\/blob\//, '/'); //Get URL of the raw file

  const replaceAssets = () => {
        let frame;
        let a;
        let link;
        const links = [];
        let script;
        const scripts = [];
        let i;
        let href;
        let src;
        //Framesets
        if (document.querySelectorAll('frameset').length)
      return; //Don't replace CSS/JS if it's a frameset, because it will be erased by document.write()
        //Frames
        frame = document.querySelectorAll('iframe[src],frame[src]');
        for (i = 0; i < frame.length; ++i) {
      src = frame[i].src; //Get absolute URL
      if (src.indexOf('//raw.githubusercontent.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
        frame[i].src = '//' + location.hostname + location.pathname + '?' + src; //Then rewrite URL so it can be loaded using CORS proxy
      }
    }
        //Links
        a = document.querySelectorAll('a[href]');
        for (i = 0; i < a.length; ++i) {
      href = a[i].href; //Get absolute URL
      if (href.indexOf('#') > 0) { //Check if it's an anchor
        a[i].href = '//' + location.hostname + location.pathname + location.search + '#' + a[i].hash.substring(1); //Then rewrite URL with support for empty anchor
      } else if ((href.indexOf('//raw.githubusercontent.com') > 0 || href.indexOf('//bitbucket.org') > 0) && (href.indexOf('.html') > 0 || href.indexOf('.htm') > 0)) { //Check if it's from raw.github.com or bitbucket.org and to HTML files
        a[i].href = '//' + location.hostname + location.pathname + '?' + href; //Then rewrite URL so it can be loaded using CORS proxy
      }
    }
        //Stylesheets
        link = document.querySelectorAll('link[rel=stylesheet]');
        for (i = 0; i < link.length; ++i) {
      href = link[i].href; //Get absolute URL
      if (href.indexOf('//raw.githubusercontent.com') > 0 || href.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
        links.push(fetchProxy(href, null, 0)); //Then add it to links queue and fetch using CORS proxy
      }
    }
        Promise.all(links).then(res => {
      for (i = 0; i < res.length; ++i) {
        loadCSS(res[i]);
      }
    });
        //Scripts
        script = document.querySelectorAll('script[type="text/htmlpreview"]');
        for (i = 0; i < script.length; ++i) {
      src = script[i].src; //Get absolute URL
      if (src.indexOf('//raw.githubusercontent.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
        scripts.push(fetchProxy(src, null, 0)); //Then add it to scripts queue and fetch using CORS proxy
      } else {
        script[i].removeAttribute('type');
        scripts.push(script[i].innerHTML); //Add inline script to queue to eval in order
      }
    }
        Promise.all(scripts).then(res => {
      for (i = 0; i < res.length; ++i) {
        loadJS(res[i]);
      }
      document.dispatchEvent(new Event('DOMContentLoaded', {bubbles: true, cancelable: true})); //Dispatch DOMContentLoaded event after loading all scripts
    });
    };

  const loadHTML = data => {
    if (data) {
      data = data.replace(/<head([^>]*)>/i, '<head$1><base href="' + url + '">').replace(/<script(\s*src=["'][^"']*["'])?(\s*type=["'](text|application)\/javascript["'])?/gi, '<script type="text/htmlpreview"$1'); //Add <base> just after <head> and replace <script type="text/javascript"> with <script type="text/htmlpreview">
      setTimeout(() => {
        document.open();
        document.write(data);
        document.close();
        replaceAssets();
      }, 10); //Delay updating document to have it cleared before
    }
  };

  var loadCSS = data => {
    if (data) {
      const style = document.createElement('style');
      style.innerHTML = data;
      document.head.appendChild(style);
    }
  };

  var loadJS = data => {
    if (data) {
      const script = document.createElement('script');
      script.innerHTML = data;
      document.body.appendChild(script);
    }
  };
  
  var fetchProxy = (url, options, i) => {
    const proxy = [
      'https://cors-anywhere.herokuapp.com/',
      'https://yacdn.org/proxy/',
      'https://api.codetabs.com/v1/proxy/?quest='
    ];
    return fetch(proxy[i] + url, options).then(res => {
      if (!res.ok) throw new Error('Cannot load ' + url + ': ' + res.status + ' ' + res.statusText);
      return res.text();
    }).catch(error => {
      if (i === proxy.length - 1)
        throw error;
      return fetchProxy(url, options, i + 1);
    });
  };

  if (url && url.indexOf(location.hostname) < 0)
    fetch(url).then(res => {
      if (!res.ok) throw new Error('Cannot load ' + url + ': ' + res.status + ' ' + res.statusText);
      return res.text();
    }).then(loadHTML).catch(error => {
      console.error(error);
      previewForm.style.display = 'block';
      previewForm.innerText = error;
    });
  else
    previewForm.style.display = 'block';

})();