diff --git a/htmlpreview.js b/htmlpreview.js
index 6f95205..a38e616 100644
--- a/htmlpreview.js
+++ b/htmlpreview.js
@@ -1,116 +1,114 @@
-var HTMLPreview = {
+(function () {
+
+ var previewForm = document.getElementById('previewform');
- content: '',
+ var url = location.search.substring(1).replace(/\/\/github\.com/, '//raw.githubusercontent.com').replace(/\/blob\//, '/'); //Get URL of the raw file
- previewform: document.getElementById('previewform'),
-
- file: function() {
- return location.search.substring(1); //Get everything after the ?
- },
-
- raw: function() {
- return HTMLPreview.file().replace(/\/\/github\.com/, '//raw.github.com').replace(/\/blob\//, '/'); //Get URL of the raw file
- },
-
- replaceAssets: function() {
- var frame, a, link, script, i, href, src;
+ var replaceAssets = function () {
+ var frame, a, link, links = [], script, scripts = [], i, href, 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) {
+ for (i = 0; i < frame.length; ++i) {
src = frame[i].src; //Get absolute URL
- if(src.indexOf('//raw.github.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
- frame[i].src = 'http://' + location.hostname + location.pathname + '?' + src; //Then rewrite URL so it can be loaded using YQL
+ 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) {
+ 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 = 'http://' + location.hostname + location.pathname + location.search + '#' + a[i].hash.substring(1); //Then rewrite URL with support for empty anchor
- }
- else if(href.indexOf('//raw.github.com') > 0 || href.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
- a[i].href = 'http://' + location.hostname + location.pathname + '?' + href; //Then rewrite URL so it can be loaded using YQL
+ 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
}
}
- if(document.querySelectorAll('frameset').length)
- return; //Don't replace CSS/JS if it's a frameset, because it will be erased by document.write()
+ //Stylesheets
link = document.querySelectorAll('link[rel=stylesheet]');
- for(i = 0; i < link.length; ++i) {
+ for (i = 0; i < link.length; ++i) {
href = link[i].href; //Get absolute URL
- if(href.indexOf('//raw.github.com') > 0 || href.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
- HTMLPreview.send(href, 'loadCSS'); //Then load it using YQL
+ 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
}
}
- script = document.querySelectorAll('script');
- for(i = 0; i < script.length; ++i) {
- src = script[i].src; //Get absolute URL
- if(src.indexOf('//raw.github.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
- HTMLPreview.send(src, 'loadJS'); //Then load it using YQL
+ Promise.all(links).then(function (res) {
+ for (i = 0; i < res.length; ++i) {
+ loadCSS(res[i]);
}
- else if(!src && script[i].innerHTML.indexOf('HTMLPreview') < 0) { //Move all inline scripts except HTMLPreview.replaceAssets()
- document.write(script[i].outerHTML);
+ });
+ //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(function (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
+ });
+ };
- loadHTML: function(data) {
- if(data
- && data.query
- && data.query.results
- && data.query.results.resources
- && data.query.results.resources.content
- && data.query.results.resources.status == 200) {
- HTMLPreview.content = data.query.results.resources.content.replace(/
/i, '').replace(/<\/body>/i, '').replace(/<\/head>\s*