简体   繁体   English

将HTML转换为document.write javascript

[英]Convert Html to document.write javascript

I've see a web page that when I try to view the source, I only see a single JavaScript statement. 我看到一个网页,当我尝试查看源代码时,只看到一条JavaScript语句。 I searched and could not figure out how they have done that. 我搜寻了一下,无法弄清楚他们是如何做到的。 you can do and see yourself. 你可以做,看看自己。

The web page is: Weird Page 该网页是: 奇怪的页面

when I view the source I only see something like below which also looks like a comment: 当我查看源代码时,我只会看到下面类似的内容,也看起来像是评论:

<script type='text/javascript' language='Javascript'>
<!--document.write(unescape('%3C%2........................
................

How they have done that? 他们是如何做到的? How I can change my web page so when someone view the source it look like that? 我如何更改网页,以便当有人查看源代码时看起来像那样?

This article may help to clarify what's happening there. 本文可能有助于弄清楚那里发生了什么。

Basically, this company has encoded their entire page using standard escape characters. 基本上,该公司使用标准的转义字符对整个页面进行了编码。 If you copy the bit from unencode(...) all the way to the next to last parens, and paste it into your browser console, you'll see the actual HTML content. 如果将位从unencode(...)一直复制到倒数第二个括号,然后将其粘贴到浏览器控制台,您将看到实际的HTML内容。

If you look at the source code for the page I linked, you'll see the function that converts normal text to all escape characters: 如果您查看我链接的页面的源代码,则会看到将普通文本转换为所有转义字符的函数:

// CONVERTS *ALL* CHARACTERS INTO ESCAPED VERSIONS.
function escapeTxt(os){
    var ns='';
    var t;
    var chr='';
    var cc='';
    var tn='';
    for(i=0;i<256;i++){
        tn=i.toString(16);
        if(tn.length<2)tn="0"+tn;
        cc+=tn;
        chr+=unescape('%'+tn);
    }
    cc=cc.toUpperCase();
    os.replace(String.fromCharCode(13)+'',"%13");
    for(q=0;q<os.length;q++){
        t=os.substr(q,1);
        for(i=0;i<chr.length;i++){
            if(t==chr.substr(i,1)){
                t=t.replace(chr.substr(i,1),"%"+cc.substr(i*2,2));
                i=chr.length;
            }
        }
    ns+=t;
    }
    return ns;
}

What is happening is pretty much what it sounds like. 发生的事情几乎听起来像是。 The server is serving a single <script> element, which contains an HTML comment to make it easier for browser that don't support JS (read more here: Why does Javascript ignore single line HTML Comments? ), and in the comment it's writing to the document the unescaped version of the string, which is sent escaped. 服务器只提供一个<script>元素,其中包含HTML注释,以使不支持JS的浏览器更容易(请在此处了解更多信息: Javascript为什么会忽略单行HTML注释? ),并且在注释中进行了编写到文档的字符串的未转义版本,即转义发送的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM