From 09ca49e62558479f89bbbfee1231284ce13c03f8 Mon Sep 17 00:00:00 2001 From: Timo Kluck Date: Tue, 26 Sep 2023 20:33:30 +0100 Subject: [PATCH 1/3] Explicit support for protocol and port This makes it easier to test using a localhost server --- htmlpreview.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/htmlpreview.js b/htmlpreview.js index a38e616..492fc76 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,7 +23,7 @@ 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 } } //Links @@ -22,9 +31,9 @@ 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 + a[i].href = location.protocol + '//' + location.hostname + ':' + location.port + 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 + a[i].href = rewrite(href); //Then rewrite URL so it can be loaded using CORS proxy } } //Stylesheets From 78e3fb082392a4876ae2ccf95c849a086d27ba6a Mon Sep 17 00:00:00 2001 From: Timo Kluck Date: Tue, 26 Sep 2023 20:35:33 +0100 Subject: [PATCH 2/3] Add support for object tags (e.g. embedded svg) --- htmlpreview.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/htmlpreview.js b/htmlpreview.js index 492fc76..fb01402 100644 --- a/htmlpreview.js +++ b/htmlpreview.js @@ -26,6 +26,14 @@ 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) { From 47759e20856a6b8cb1f60b670f7eb3597f0e92d6 Mon Sep 17 00:00:00 2001 From: Timo Kluck Date: Tue, 26 Sep 2023 20:41:46 +0100 Subject: [PATCH 3/3] Remove special casing for anchors This breaks support for anchors _into other pages_ because it seems to assume every anchor is within the same page. --- htmlpreview.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/htmlpreview.js b/htmlpreview.js index fb01402..77fa823 100644 --- a/htmlpreview.js +++ b/htmlpreview.js @@ -38,9 +38,7 @@ 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.protocol + '//' + location.hostname + ':' + location.port + 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 + 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 } }