Skip to content

Commit fa2b172

Browse files
committed
Fixed infinite loop in Chrome
1 parent cadc7e0 commit fa2b172

File tree

3 files changed

+30
-37
lines changed

3 files changed

+30
-37
lines changed

htmlpreview.js

+20-30
Original file line numberDiff line numberDiff line change
@@ -14,47 +14,37 @@ var HTMLPreview = {
1414

1515
replaceAssets: function() {
1616
var frame, a, link, script, i, href, src;
17-
frame = [].concat.apply([].concat.apply([], document.getElementsByTagName('iframe')), document.getElementsByTagName('frame'));
17+
frame = document.querySelectorAll('iframe[src],frame[src]');
1818
for(i = 0; i < frame.length; ++i) {
19-
if(frame[i].src) {
20-
src = frame[i].src; //Get absolute URL
21-
if(src.indexOf('//raw.github.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
22-
frame[i].src = 'http://' + location.hostname + location.pathname + '?' + src; //Then rewrite URL so it can be loaded using YQL
23-
}
19+
src = frame[i].src; //Get absolute URL
20+
if(src.indexOf('//raw.github.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
21+
frame[i].src = 'http://' + location.hostname + location.pathname + '?' + src; //Then rewrite URL so it can be loaded using YQL
2422
}
2523
}
26-
a = document.getElementsByTagName('a');
24+
a = document.querySelectorAll('a[href]');
2725
for(i = 0; i < a.length; ++i) {
28-
if(a[i].href) {
29-
href = a[i].href; //Get absolute URL
30-
if(href.indexOf('#') > 0) { //Check if it's an anchor
31-
a[i].href = 'http://' + location.hostname + location.pathname + location.search + '#' + a[i].hash.substring(1); //Then rewrite URL with support for empty anchor
32-
}
33-
else if(href.indexOf('//raw.github.com') > 0 || href.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
34-
a[i].href = 'http://' + location.hostname + location.pathname + '?' + href; //Then rewrite URL so it can be loaded using YQL
35-
}
26+
href = a[i].href; //Get absolute URL
27+
if(href.indexOf('#') > 0) { //Check if it's an anchor
28+
a[i].href = 'http://' + location.hostname + location.pathname + location.search + '#' + a[i].hash.substring(1); //Then rewrite URL with support for empty anchor
29+
}
30+
else if(href.indexOf('//raw.github.com') > 0 || href.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
31+
a[i].href = 'http://' + location.hostname + location.pathname + '?' + href; //Then rewrite URL so it can be loaded using YQL
3632
}
3733
}
38-
if(document.getElementsByTagName('frameset').length)
34+
if(document.querySelectorAll('frameset').length)
3935
return; //Don't replace CSS/JS if it's a frameset, because it will be erased by document.write()
40-
link = document.getElementsByTagName('link');
36+
link = document.getElementsByTagName('link[rel=stylesheet][href]');
4137
for(i = 0; i < link.length; ++i) {
42-
if(link[i].rel
43-
&& link[i].rel.toLowerCase() == 'stylesheet'
44-
&& link[i].href) {
45-
href = link[i].href; //Get absolute URL
46-
if(href.indexOf('//raw.github.com') > 0 || href.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
47-
HTMLPreview.send(href, 'loadCSS'); //Then load it using YQL
48-
}
38+
href = link[i].href; //Get absolute URL
39+
if(href.indexOf('//raw.github.com') > 0 || href.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
40+
HTMLPreview.send(href, 'loadCSS'); //Then load it using YQL
4941
}
5042
}
51-
script = document.getElementsByTagName('script');
43+
script = document.querySelectorAll('script[src]');
5244
for(i = 0; i < script.length; ++i) {
53-
if(script[i].src) {
54-
src = script[i].src; //Get absolute URL
55-
if(src.indexOf('//raw.github.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
56-
HTMLPreview.send(src, 'loadJS'); //Then load it using YQL
57-
}
45+
src = script[i].src; //Get absolute URL
46+
if(src.indexOf('//raw.github.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
47+
HTMLPreview.send(src, 'loadJS'); //Then load it using YQL
5848
}
5949
else if(script[i].innerHTML.indexOf('HTMLPreview') < 0) { //Move all inline scripts except HTMLPreview.replaceAssets()
6050
document.write(script[i].outerHTML);

htmlpreview.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
Many GitHub repositories don't use GitHub Pages to host their HTML files. GitHub HTML Preview allows you to render those files without cloning or downloading whole repositories.
1+
Many GitHub repositories don't use GitHub Pages to host their HTML files. **GitHub & BitBucket HTML Preview** allows you to render those files without cloning or downloading whole repositories. It is a client-side solution and does not involve any third party hosting servers (except for Yahoo! Query Language to fetch assets).
22

3-
If you try to open raw versions of any HTML, CSS or JS files in a web browser directly from GitHub, all you will see are sources. GitHub forces them to use the "text/plain" content-type. This script overrides it by using Yahoo! Query Language.
3+
If you try to open raw versions of any HTML, CSS or JS files in a web browser directly from GitHub, all you will see are sources. GitHub forces them to use the "text/plain" content-type, so they cannot be interpreted. This script overrides it by using Yahoo! Query Language.
44

5-
**Now it works with BitBucket too!**
6-
7-
To use it, just prepend this to the URL of any HTML file: **http://htmlpreview.github.io/?**
5+
In order to use it, just prepend this fragment to the URL of any HTML file: **[http://htmlpreview.github.io/?](http://htmlpreview.github.io/?)**
86

97
E.g.:
108
http://htmlpreview.github.io/?https://raw.github.com/twbs/bootstrap/gh-pages/2.3.2/index.html
119
http://htmlpreview.github.io/?https://github.com/documentcloud/backbone/blob/master/examples/todos/index.html
1210

13-
GitHub HTML Preview is tested under Google Chrome, Apple Safari and Mozilla Firefox, and it should work with any websites, not only GitHub.
11+
What it does is load HTML using YQL, then process all links, frames, scripts and styles, and load each of them using YQL, so they can be evaluted in the browser. Here is the workflow:
12+
```
13+
HTMLPreview.init() -> HTMLPreview.send(HTML) -> YQL fetch HTML -> HTMLPreview.loadHTML(data) -> HTMLPreview.replaceAssets() -> HTMLPreview.send(CSS) -> YQL fetch CSS -> HTMLPreview.loadCSS(data) -> HTMLPreview.send(JS) -> YQL fetch JS -> HTMLPreview.loadJS(data)
14+
```
15+
16+
**GitHub & BitBucket HTML Preview** was tested under Google Chrome, Apple Safari and Mozilla Firefox, and it should work with majority of websites, not only GitHub & BitBucket.

0 commit comments

Comments
 (0)