Skip to content

Commit b629034

Browse files
committed
Move script to index.js with console.log messages
1 parent 3a5a4a1 commit b629034

File tree

2 files changed

+53
-45
lines changed

2 files changed

+53
-45
lines changed

index.html

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,8 @@
2424
}
2525
</style>
2626
<script src="/node_modules/mermaid/dist/mermaid.min.js"></script>
27-
<script src="/socket.io/socket.io.js"></script>
28-
<script>
29-
var socket = io('http://'+ window.location.host +'/');
30-
socket.on('connect', function () {
31-
setDisconnected(false);
32-
socket.on('newContent', function(newHTML) {
33-
document.querySelector(".markdown-body").innerHTML = newHTML;
34-
35-
// scroll to the hyperlink with id="marker"
36-
let marker = document.getElementById("marker");
37-
if(marker) {
38-
marker.scrollIntoView();
39-
}
40-
mermaid.init()
41-
});
42-
socket.on('die', function(newHTML) {
43-
window.open('', '_self', '');
44-
window.close();
45-
46-
var firefoxWarning =
47-
"<h1>Oops!</h1>" +
48-
"<h3>Firefox doesn't allow windows to self-close.</h3>" +
49-
"<h3>If you want the preview window to close automatically like in other browsers, go to about:config and set dom.allow_scripts_to_close_windows to true.</h3>"
50-
document.body.innerHTML = firefoxWarning;
51-
});
52-
});
53-
socket.on('disconnect', function() {
54-
setDisconnected(true);
55-
});
56-
57-
try {
58-
eval('// If CSP is active, then this is blocked');
59-
} catch (e) {
60-
// Detected that the CSP was active (by the user's preference).
61-
// Drop capabilities to prevent rendered markdown from executing scripts.
62-
var meta = document.createElement('meta');
63-
meta.setAttribute('http-equiv', 'Content-Security-Policy');
64-
meta.setAttribute('content', "script-src 'none';");
65-
document.head.appendChild(meta);
66-
}
67-
68-
function setDisconnected(isDisconnected) {
69-
document.getElementById('con-error').style.display =
70-
isDisconnected ? 'block' : 'none';
71-
}
27+
<script src="/socket.io/socket.io.min.js"></script>
28+
<script src="/index.js"></script>
7229
</script>
7330
</head>
7431
<body>

index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
'use strict';
2+
let socket = io('http://'+ window.location.host +'/');
3+
4+
socket.on('connect', function () {
5+
setDisconnected(false);
6+
socket.on('newContent', function(newHTML) {
7+
document.querySelector(".markdown-body").innerHTML = newHTML;
8+
9+
// scroll to the hyperlink with id="marker"
10+
let marker = document.getElementById("marker");
11+
if(marker) {
12+
marker.scrollIntoView();
13+
}
14+
mermaid.init()
15+
});
16+
socket.on('die', function(newHTML) {
17+
window.open('', '_self', '');
18+
window.close();
19+
20+
let firefoxWarning =
21+
"<h1>Oops!</h1>" +
22+
"<h3>Firefox doesn't allow windows to self-close.</h3>" +
23+
"<h3>If you want the preview window to close automatically like in other browsers, go to about:config and set dom.allow_scripts_to_close_windows to true.</h3>"
24+
document.body.innerHTML = firefoxWarning;
25+
});
26+
});
27+
28+
socket.on('disconnect', function() {
29+
setDisconnected(true);
30+
});
31+
32+
try {
33+
console.log('Inspecting status of Content Security Policy');
34+
eval('// If CSP is active, then this is blocked');
35+
} catch (e) {
36+
console.log(`
37+
Detected that the CSP was active (by the user's preference).
38+
Droping capabilities to prevent rendered markdown from executing scripts.
39+
If you trust the markdown content, set environment variable
40+
INSTANT_MARKDOWN_ALLOW_UNSAFE_CONTENT=1
41+
`)
42+
let meta = document.createElement('meta');
43+
meta.setAttribute('http-equiv', 'Content-Security-Policy');
44+
meta.setAttribute('content', "script-src 'none';");
45+
document.head.appendChild(meta);
46+
}
47+
48+
function setDisconnected(isDisconnected) {
49+
document.getElementById('con-error').style.display =
50+
isDisconnected ? 'block' : 'none';
51+
}

0 commit comments

Comments
 (0)