diff --git a/htmlpreview.js b/htmlpreview.js index a38e616..77fa823 100644 --- a/htmlpreview.js +++ b/htmlpreview.js @@ -4,6 +4,15 @@ var url = location.search.substring(1).replace(/\/\/github\.com/, '//raw.githubusercontent.com').replace(/\/blob\//, '/'); //Get URL of the raw file + var rewrite = function (url) { + if (location.port.length) { + port_part = ':' + location.port + } else { + port_part = '' + } + return location.protocol + '//' + location.hostname + port_part + location.pathname + '?' + url + } + var replaceAssets = function () { var frame, a, link, links = [], script, scripts = [], i, href, src; //Framesets @@ -14,17 +23,23 @@ 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 + frame[i].src = rewrite(src); //Then rewrite URL so it can be loaded using CORS proxy + } + } + //Objects + object = document.querySelectorAll('object[data]'); + for (i = 0; i < object.length; ++i) { + src = object[i].data; //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 + object[i].data = rewrite(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 + 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 = rewrite(href); //Then rewrite URL so it can be loaded using CORS proxy } } //Stylesheets