vscode-live-server icon indicating copy to clipboard operation
vscode-live-server copied to clipboard

Extension fails to work when add specific comments in html

Open hmpthz opened this issue 5 years ago • 4 comments

  • Live Server: 5.6.1
  • Platform: Windows 10
  • Visual Studio Code: 1.33.1
  • Browser: Chrome

I found that live server will inject some js scripts in the webpage, when add specific comments in html like // </body>, it fails to work.

my html source code:

<!DOCTYPE html>
<html>

<head>
<script type="text/javascript">
    // </body>
</script>
</head>

<body>

<div id="test">
    aaaa
</div>
</body>

</html>

source code after live server inject scripts:

<!DOCTYPE html>
<html>

<head>
<script type="text/javascript">
    // <!-- Code injected by live-server -->
<script type="text/javascript">
	// <![CDATA[  <-- For SVG support
	if ('WebSocket' in window) {
		(function () {
			function refreshCSS() {
				var sheets = [].slice.call(document.getElementsByTagName("link"));
				var head = document.getElementsByTagName("head")[0];
				for (var i = 0; i < sheets.length; ++i) {
					var elem = sheets[i];
					var parent = elem.parentElement || head;
					parent.removeChild(elem);
					var rel = elem.rel;
					if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") {
						var url = elem.href.replace(/(&|\?)_cacheOverride=\d+/, '');
						elem.href = url + (url.indexOf('?') >= 0 ? '&' : '?') + '_cacheOverride=' + (new Date().valueOf());
					}
					parent.appendChild(elem);
				}
			}
			var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://';
			var address = protocol + window.location.host + window.location.pathname + '/ws';
			var socket = new WebSocket(address);
			socket.onmessage = function (msg) {
				if (msg.data == 'reload') window.location.reload();
				else if (msg.data == 'refreshcss') refreshCSS();
			};
			if (sessionStorage && !sessionStorage.getItem('IsThisFirstTime_Log_From_LiveServer')) {
				console.log('Live reload enabled.');
				sessionStorage.setItem('IsThisFirstTime_Log_From_LiveServer', true);
			}
		})();
	}
	else {
		console.error('Upgrade your browser. This Browser is NOT supported WebSocket for Live-Reloading.');
	}
	// ]]>
</script></body>
</script>
</head>

<body>

<div id="test">
    aaaa
</div>
<!-- Code injected

Of course it's not a big issue, but is there any method to solve it? Thanks!

hmpthz avatar May 13 '19 11:05 hmpthz

It is a big issue because it corrupts my code at the totally wrong place How can I diasble it?

function addRow(tabelId)

  | {   | var email="est   |

ouwe-knutselaar avatar Jul 09 '19 09:07 ouwe-knutselaar

same error

  • Live Server: 5.6.1
  • Platform: Windows 10
  • Visual Studio Code: 1.33.1
  • Browser: Chrome

I found that live server will inject some js scripts in the webpage, when add specific comments in html like // </body>, it fails to work.

my html source code:

<!DOCTYPE html>
<html>

<head>
<script type="text/javascript">
    // </body>
</script>
</head>

<body>

<div id="test">
    aaaa
</div>
</body>

</html>

source code after live server inject scripts:

<!DOCTYPE html>
<html>

<head>
<script type="text/javascript">
    // <!-- Code injected by live-server -->
<script type="text/javascript">
	// <![CDATA[  <-- For SVG support
	if ('WebSocket' in window) {
		(function () {
			function refreshCSS() {
				var sheets = [].slice.call(document.getElementsByTagName("link"));
				var head = document.getElementsByTagName("head")[0];
				for (var i = 0; i < sheets.length; ++i) {
					var elem = sheets[i];
					var parent = elem.parentElement || head;
					parent.removeChild(elem);
					var rel = elem.rel;
					if (elem.href && typeof rel != "string" || rel.length == 0 || rel.toLowerCase() == "stylesheet") {
						var url = elem.href.replace(/(&|\?)_cacheOverride=\d+/, '');
						elem.href = url + (url.indexOf('?') >= 0 ? '&' : '?') + '_cacheOverride=' + (new Date().valueOf());
					}
					parent.appendChild(elem);
				}
			}
			var protocol = window.location.protocol === 'http:' ? 'ws://' : 'wss://';
			var address = protocol + window.location.host + window.location.pathname + '/ws';
			var socket = new WebSocket(address);
			socket.onmessage = function (msg) {
				if (msg.data == 'reload') window.location.reload();
				else if (msg.data == 'refreshcss') refreshCSS();
			};
			if (sessionStorage && !sessionStorage.getItem('IsThisFirstTime_Log_From_LiveServer')) {
				console.log('Live reload enabled.');
				sessionStorage.setItem('IsThisFirstTime_Log_From_LiveServer', true);
			}
		})();
	}
	else {
		console.error('Upgrade your browser. This Browser is NOT supported WebSocket for Live-Reloading.');
	}
	// ]]>
</script></body>
</script>
</head>

<body>

<div id="test">
    aaaa
</div>
<!-- Code injected

Of course it's not a big issue, but is there any method to solve it? Thanks!

same error

firmanjabar avatar Nov 06 '19 14:11 firmanjabar

So, Could you fix the problem?

Angstromico avatar May 02 '21 00:05 Angstromico

It happens to me also when using script tag, using live server extension

parothia avatar Jul 04 '22 17:07 parothia