|
| 1 | +(() => { |
| 2 | + |
| 3 | + const previewForm = document.getElementById('previewform'); |
| 4 | + |
| 5 | + const url = location.search.substring(1).replace(/\/\/github\.com/, '//raw.githubusercontent.com').replace(/\/blob\//, '/'); //Get URL of the raw file |
| 6 | + |
| 7 | + const replaceAssets = () => { |
| 8 | + let frame; |
| 9 | + let a; |
| 10 | + let link; |
| 11 | + const links = []; |
| 12 | + let script; |
| 13 | + const scripts = []; |
| 14 | + let i; |
| 15 | + let href; |
| 16 | + let src; |
| 17 | + //Framesets |
| 18 | + if (document.querySelectorAll('frameset').length) |
| 19 | + return; //Don't replace CSS/JS if it's a frameset, because it will be erased by document.write() |
| 20 | + //Frames |
| 21 | + frame = document.querySelectorAll('iframe[src],frame[src]'); |
| 22 | + for (i = 0; i < frame.length; ++i) { |
| 23 | + src = frame[i].src; //Get absolute URL |
| 24 | + if (src.indexOf('//raw.githubusercontent.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org |
| 25 | + frame[i].src = '//' + location.hostname + location.pathname + '?' + src; //Then rewrite URL so it can be loaded using CORS proxy |
| 26 | + } |
| 27 | + } |
| 28 | + //Links |
| 29 | + a = document.querySelectorAll('a[href]'); |
| 30 | + for (i = 0; i < a.length; ++i) { |
| 31 | + href = a[i].href; //Get absolute URL |
| 32 | + if (href.indexOf('#') > 0) { //Check if it's an anchor |
| 33 | + a[i].href = '//' + location.hostname + location.pathname + location.search + '#' + a[i].hash.substring(1); //Then rewrite URL with support for empty anchor |
| 34 | + } 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 |
| 35 | + a[i].href = '//' + location.hostname + location.pathname + '?' + href; //Then rewrite URL so it can be loaded using CORS proxy |
| 36 | + } |
| 37 | + } |
| 38 | + //Stylesheets |
| 39 | + link = document.querySelectorAll('link[rel=stylesheet]'); |
| 40 | + for (i = 0; i < link.length; ++i) { |
| 41 | + href = link[i].href; //Get absolute URL |
| 42 | + if (href.indexOf('//raw.githubusercontent.com') > 0 || href.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org |
| 43 | + links.push(fetchProxy(href, null, 0)); //Then add it to links queue and fetch using CORS proxy |
| 44 | + } |
| 45 | + } |
| 46 | + Promise.all(links).then(res => { |
| 47 | + for (i = 0; i < res.length; ++i) { |
| 48 | + loadCSS(res[i]); |
| 49 | + } |
| 50 | + }); |
| 51 | + //Scripts |
| 52 | + script = document.querySelectorAll('script[type="text/htmlpreview"]'); |
| 53 | + for (i = 0; i < script.length; ++i) { |
| 54 | + src = script[i].src; //Get absolute URL |
| 55 | + if (src.indexOf('//raw.githubusercontent.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org |
| 56 | + scripts.push(fetchProxy(src, null, 0)); //Then add it to scripts queue and fetch using CORS proxy |
| 57 | + } else { |
| 58 | + script[i].removeAttribute('type'); |
| 59 | + scripts.push(script[i].innerHTML); //Add inline script to queue to eval in order |
| 60 | + } |
| 61 | + } |
| 62 | + Promise.all(scripts).then(res => { |
| 63 | + for (i = 0; i < res.length; ++i) { |
| 64 | + loadJS(res[i]); |
| 65 | + } |
| 66 | + document.dispatchEvent(new Event('DOMContentLoaded', {bubbles: true, cancelable: true})); //Dispatch DOMContentLoaded event after loading all scripts |
| 67 | + }); |
| 68 | + }; |
| 69 | + |
| 70 | + const loadHTML = data => { |
| 71 | + if (data) { |
| 72 | + 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"> |
| 73 | + setTimeout(() => { |
| 74 | + document.open(); |
| 75 | + document.write(data); |
| 76 | + document.close(); |
| 77 | + replaceAssets(); |
| 78 | + }, 10); //Delay updating document to have it cleared before |
| 79 | + } |
| 80 | + }; |
| 81 | + |
| 82 | + var loadCSS = data => { |
| 83 | + if (data) { |
| 84 | + const style = document.createElement('style'); |
| 85 | + style.innerHTML = data; |
| 86 | + document.head.appendChild(style); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + var loadJS = data => { |
| 91 | + if (data) { |
| 92 | + const script = document.createElement('script'); |
| 93 | + script.innerHTML = data; |
| 94 | + document.body.appendChild(script); |
| 95 | + } |
| 96 | + }; |
| 97 | + |
| 98 | + var fetchProxy = (url, options, i) => { |
| 99 | + const proxy = [ |
| 100 | + 'https://cors-anywhere.herokuapp.com/', |
| 101 | + 'https://yacdn.org/proxy/', |
| 102 | + 'https://api.codetabs.com/v1/proxy/?quest=' |
| 103 | + ]; |
| 104 | + return fetch(proxy[i] + url, options).then(res => { |
| 105 | + if (!res.ok) throw new Error('Cannot load ' + url + ': ' + res.status + ' ' + res.statusText); |
| 106 | + return res.text(); |
| 107 | + }).catch(error => { |
| 108 | + if (i === proxy.length - 1) |
| 109 | + throw error; |
| 110 | + return fetchProxy(url, options, i + 1); |
| 111 | + }); |
| 112 | + }; |
| 113 | + |
| 114 | + if (url && url.indexOf(location.hostname) < 0) |
| 115 | + fetch(url).then(res => { |
| 116 | + if (!res.ok) throw new Error('Cannot load ' + url + ': ' + res.status + ' ' + res.statusText); |
| 117 | + return res.text(); |
| 118 | + }).then(loadHTML).catch(error => { |
| 119 | + console.error(error); |
| 120 | + previewForm.style.display = 'block'; |
| 121 | + previewForm.innerText = error; |
| 122 | + }); |
| 123 | + else |
| 124 | + previewForm.style.display = 'block'; |
| 125 | + |
| 126 | +})(); |
0 commit comments