Skip to content

Commit 296f172

Browse files
committed
Fixed double invoke of scripts, added autofocus
1 parent 2fa31c3 commit 296f172

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

htmlpreview.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,14 @@ var HTMLPreview = {
4040
HTMLPreview.send(href, 'loadCSS'); //Then load it using YQL
4141
}
4242
}
43-
script = document.querySelectorAll('script');
43+
script = document.querySelectorAll('script[type="text/htmlpreview"]');
4444
for(i = 0; i < script.length; ++i) {
4545
src = script[i].src; //Get absolute URL
4646
if(src.indexOf('//raw.githubusercontent.com') > 0 || src.indexOf('//bitbucket.org') > 0) { //Check if it's from raw.github.com or bitbucket.org
4747
HTMLPreview.send(src, 'loadJS'); //Then load it using YQL
4848
}
49-
else if(!src && script[i].innerHTML.indexOf('HTMLPreview') < 0) { //Move all inline scripts except HTMLPreview.replaceAssets()
49+
else { //Append all inline scripts
50+
script[i].removeAttribute('type');
5051
document.write(script[i].outerHTML);
5152
}
5253
}
@@ -65,7 +66,7 @@ var HTMLPreview = {
6566
&& data.query.results.resources
6667
&& data.query.results.resources.content
6768
&& data.query.results.resources.status == 200) {
68-
HTMLPreview.content = data.query.results.resources.content.replace(/<head>/i, '<head><base href="' + HTMLPreview.raw() + '">').replace(/<\/body>/i, '<script src="//' + location.hostname + '/htmlpreview.min.js"></script><script>HTMLPreview.replaceAssets();</script></body>').replace(/<\/head>\s*<frameset/gi, '<script src="//' + location.hostname + '/htmlpreview.min.js"></script><script>document.addEventListener("DOMContentLoaded",HTMLPreview.replaceAssets,false);</script></head><frameset'); //Add <base> just after <head> and inject <script> just before </body> or </head> if <frameset>
69+
HTMLPreview.content = data.query.results.resources.content.replace(/<head>/i, '<head><base href="' + HTMLPreview.raw() + '">').replace(/<script( type=["'](text|application)\/javascript["'])?/gi, '<script type="text/htmlpreview"').replace(/<\/body>/i, '<script src="//' + location.hostname + '/htmlpreview.min.js"></script><script>HTMLPreview.replaceAssets();</script></body>').replace(/<\/head>\s*<frameset/gi, '<script src="//' + location.hostname + '/htmlpreview.min.js"></script><script>document.addEventListener("DOMContentLoaded",HTMLPreview.replaceAssets,false);</script></head><frameset'); //Add <base> just after <head> and inject <script> just before </body> or </head> if <frameset>
6970
setTimeout(function() {
7071
document.open();
7172
document.write(HTMLPreview.content);

htmlpreview.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55
<title>GitHub &amp; BitBucket HTML Preview</title>
66
<style>
77
body {
8-
font: 13px Helvetica, arial, freesans, clean, sans-serif;
8+
font: 12px 'Helvetica Neue', Helvetica, Arial, freesans, clean, sans-serif;
99
color: #333;
1010
}
1111
h1 {
12-
font-size: 26px;
12+
font-size: 20px;
1313
}
1414
a {
1515
color: #666;
@@ -33,8 +33,8 @@
3333
<body>
3434
<form id="previewform" action="">
3535
<h1>GitHub &amp; BitBucket HTML Preview</h1>
36-
<p>Enter URL of the HTML file to preview: <input type="url" id="file" value="" placeholder="e.g. https://github.com/user/repo/blob/master/index.html" size="70"> <input type="submit" value="&raquo;"></p>
37-
<p>or prepend to the URL: <strong>http://htmlpreview.github.io/?</strong>https://raw.github.com/twbs/bootstrap/gh-pages/2.3.2/index.html</p>
36+
<p>Enter URL of the HTML file to preview: <input type="url" id="file" value="" placeholder="e.g. https://github.com/user/repo/blob/master/index.html" size="60" autofocus> <input type="submit" value="&raquo;"></p>
37+
<p>or prepend to the URL: <strong>http://htmlpreview.github.io/?</strong>https://github.com/twbs/bootstrap/blob/gh-pages/2.3.2/index.html</p>
3838
<p>or use this bookmarklet while browsing GitHub or BitBucket: <a href="javascript:void('http://htmlpreview.github.io/'==window.location?alert('Drag me to your bookmarks bar!'):window.location='http://htmlpreview.github.io/?'+window.location)"><strong>HTMLPreview</strong></a></p>
3939
<p id="footer">Developed by <a href="https://github.com/niutech">niu tech</a> | Contribute on <a href="https://github.com/htmlpreview/htmlpreview.github.com">GitHub</a></p>
4040
</form>

0 commit comments

Comments
 (0)