javascript - Invisible script tag in HTML document with websockets code in it - Stack Overflow

I have a simple html page<!DOCTYPE html><html><head><title>Sample<title&g

I have a simple html page

<!DOCTYPE html>
<html>
<head>
    <title>Sample</title>
</head>
<body>
    <div>
        Simple div
    </div>
</body>
</html>

I opened this HTML document in Chrome. Then in the debugger tool, I execute following

document.getElementsByTagName("script")[0]

I got a script tag (which I never added in HTML) with a bunch of code like following

<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];
                    head.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());
                    }
                    head.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>

Can anyone tell me what this is and why is it there?

I have a simple html page

<!DOCTYPE html>
<html>
<head>
    <title>Sample</title>
</head>
<body>
    <div>
        Simple div
    </div>
</body>
</html>

I opened this HTML document in Chrome. Then in the debugger tool, I execute following

document.getElementsByTagName("script")[0]

I got a script tag (which I never added in HTML) with a bunch of code like following

<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];
                    head.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());
                    }
                    head.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>

Can anyone tell me what this is and why is it there?

Share Improve this question edited May 10, 2018 at 18:59 Waleed Iqbal asked May 10, 2018 at 18:41 Waleed IqbalWaleed Iqbal 1,3222 gold badges20 silver badges37 bronze badges 4
  • 1 It's probably ing from whatever server you're running. – SLaks Commented May 10, 2018 at 18:42
  • 1 It’s probably added by a live reload plugin installed in your browser – Clive Commented May 10, 2018 at 18:42
  • Are you using Brackets? – z3nth10n Commented May 10, 2018 at 18:45
  • Thanks, you are right. I was running this from VS code live server. I stopped the server and opened the HTML document in browser, then then executing document.getElementsByTagName("script")[0] returned nothing. – Waleed Iqbal Commented May 10, 2018 at 18:46
Add a ment  | 

2 Answers 2

Reset to default 4

it's from vscode-live-server : https://github./ritwickdey/vscode-live-server/blob/master/lib/live-server/injected.html

obviously, you're using vscode-live-server and this script is related to that. but it's not the primary problem, the problem is the global selector you use for the display property.

you have this rule in your CSS: * { display: block; }

that includes the "title" in your "head" and the script the live-server is running.

you need to get rid of the global selector and the problem will be gone. or maybe you can find a better solution to fix this while continuing to use live-server.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744185914a4562205.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信