Javascript 为.mht文件启用脚本?

Javascript 为.mht文件启用脚本?,javascript,google-chrome,restriction,mhtml,Javascript,Google Chrome,Restriction,Mhtml,每次我在Google Chrome中加载MHT时,都会出现以下错误: 已阻止“”中的脚本执行file:///C:/...'因为文档的框架是沙盒,并且未设置'allow-scripts'权限 我知道allow scripts是iframe的sandbox属性的值,因此我无法将其值设置为allow scripts,因为顶部框架似乎是内部框架。当使用file://加载文件时,此内部框架仅添加sandbox属性是否有一种方法允许脚本在不使用http://的情况下运行? 我还试图在开发工具的设置中取消选中

每次我在Google Chrome中加载MHT时,都会出现以下错误:

已阻止“”中的脚本执行file:///C:/...'因为文档的框架是沙盒,并且未设置'allow-scripts'权限

我知道
allow scripts
iframe
sandbox
属性的值,因此我无法将其值设置为
allow scripts
,因为顶部框架似乎是内部框架。当使用
file://
加载文件时,此内部框架仅添加
sandbox
属性是否有一种方法允许脚本在不使用http://的情况下运行?


我还试图在开发工具的设置中取消选中禁用JavaScript,但此复选框已变灰。

这似乎是一个已知的错误:

在具有此结构的mhtml文件的基础中:

MIME-Version: 1.0
Content-Type: multipart/related; boundary="----=_NextPart_EXSTREAMHTML"; type="text/html"

This is a multi-part message in MIME format.

------=_NextPart_EXSTREAMHTML
Content-Type: image/jpg
Content-Transfer-Encoding: base64
Content-Location: file:///HTMLImage1.jpg

...............................

------=_NextPart_EXSTREAMHTML
Content-Type: text/css; charset="us-ascii"
Content-Location: file:///CSS1.css

...............................
您可以使用类似的脚本从mhtml创建html并打开一个新窗口:

var w = window.open(URL,"popUpWindow_","width=650,height=450,resizable=yes,scrollbars=yes,menubar=no,toolbar=no,location=no,directories=no,status=no");
w.document.write(getContentFromMHTML(mhtmlFileContent));
w.document.close();

/**
 * Getting the html content and css + img in base of MHTML parameter.
 * @param strMHTML
 * @return
 */
function getContentFromMHTML(strMHTML){

    //setting the bundary. For default is "------=_NextPart_EXSTREAMHTML"
    var boundary_text = "------=_NextPart_EXSTREAMHTML"; 
    var boundary_index = strMHTML.indexOf("boundary=\"");
        if (boundary_index!=-1) boundary_text = "--" + strMHTML.substring(boundary_index + 10, strMHTML.indexOf("\";", boundary_index + 10));

    //Setting the html and css content
    var returnHTML = setHTMLAndCSSFromMHTML(strMHTML, boundary_text);

    //Setting the img content to all img-src
    returnHTML = setImgFromMHTML(returnHTML, strMHTML, boundary_text);

    return returnHTML;

}

/**
 * Get the html content.<br> 
 * Extract and add the css files in style tags to head.<br>
 * Finally, add the final content to popUp
 * @param strMHTML
 * @param boundary_text
 * @return
 */
function setHTMLAndCSSFromMHTML(strMHTML, boundary_text){

    //begin tags
    var tag_begin_doc = "<!DOCTYPE HTML"; 
    var tag_begin = "<html>"; 
    var tag_begin_upper = "<HTML>";

    //end tags
    var tag_end = "</html>"; 
    var tag_end_upper = "</HTML>";

    //getting the start of html content
    var begin = strMHTML.indexOf(tag_begin_doc);
        if(begin==-1) begin = strMHTML.indexOf(tag_begin); 
        if(begin==-1) begin = strMHTML.indexOf(tag_begin_upper);

    //getting the end of html content
    var end = strMHTML.indexOf(tag_end); 
        if(end==-1) end = strMHTML.indexOf(tag_end_upper);

    //getting the html content
    var final_str = strMHTML.substring(begin, end) + tag_end;

    //Is necessary add the css before to add the html content to popUp
    var strHTML = setCssFromMHTML(final_str, boundary_text, strMHTML);

    return strHTML;

}

/**
 * Extract the css content from MHTML and push as style tags.
 * @param strHTMLtext
 * @param boundary_text
 * @param strMHTML
 * @return
 */
function setCssFromMHTML(strHTMLtext, boundary_text, strMHTML) {

    var begin_content_text = boundary_text;
    var content_type_text = "Content-Type: text/css";

    //getting the first index of contents
    var begin_content_index = strMHTML.indexOf(begin_content_text);

    //while exists a contents we must process all parts of mhtml
    while(begin_content_index!=-1){

        //getting the final index of current content
        var end_content_index = strMHTML.indexOf(begin_content_text, begin_content_index + begin_content_text.length);
        if (end_content_index==-1) end_content_index = strMHTML.length;

        //getting the current content
        var current_content = strMHTML.substring(begin_content_index, end_content_index);

        //getting the indexes
        var content_type_index = current_content.indexOf(content_type_text);

        //Check that content is  css
        if (content_type_index!=-1) {

            //getting the css in content (eg. p { margin-top: 0; margin-bottom: 0 }...)
            var css_content = current_content.substring(current_content.indexOf(".css") + 4, end_content_index);

            //Getting the index of tag head of the html content
            var head_index = strHTMLtext.indexOf("</head>");
                if (head_index==-1) head_index = strHTMLtext.indexOf("</HEAD>");

            //Getting the html to head end tag
            var head_begin = strHTMLtext.substring(0, head_index);

            //Getting the html later of head end tag
            var head_body = strHTMLtext.substring(head_index, strHTMLtext.length);

            //Adding the css to end of head of html
            var strHTMLtext = "";
            if (css_content.indexOf("<style>")==-1 && css_content.indexOf("<STYLE>")==-1){
                strHTMLtext = head_begin + "<style>" + css_content + "</style>" +  head_body;
            } else {
                strHTMLtext = head_begin + css_content + head_body;
            }

        }

        //getting the next index of contents
        begin_content_index = strMHTML.indexOf(begin_content_text, end_content_index);

    }

    return strHTMLtext;

}

/**
 * Find all images from MHTML and add to HTML content.
 * @param strMHTML
 * @param boundary_text
 * @return
 */
function setImgFromMHTML(returnHTML, strMHTML, boundary_text){

    var begin_content_text = boundary_text;
    var content_type_text = "Content-Type: ";
    var context_encoding_text = "Content-Transfer-Encoding: ";
    var content_location_text = "Content-Location: ";

    //getting the first index of contents
    var begin_content_index = strMHTML.indexOf(begin_content_text);

    //while exists a contents we must process all parts of mhtml
    while(begin_content_index!=-1){

        //getting the final index of current content
        var end_content_index = strMHTML.indexOf(begin_content_text, begin_content_index + begin_content_text.length);
            if (end_content_index==-1) end_content_index = strMHTML.length;

        //getting the current content
        var current_content = strMHTML.substring(begin_content_index, end_content_index);

        //checking that content contains the all necessary data
        if (current_content.indexOf(content_type_text) != -1 &&
            current_content.indexOf(context_encoding_text) != -1 &&
            current_content.indexOf(content_location_text) != -1) {

            //getting the indexes
            var content_type_index = current_content.indexOf(content_type_text);
            var context_encoding_index = current_content.indexOf(context_encoding_text);
            var content_location_index = current_content.indexOf(content_location_text);

            //getting the context type (eg. image/jpg)
            var context_type = current_content.substring(content_type_index + content_type_text.length , context_encoding_index);

            //Check if the content is a image
            if (context_type.indexOf("image/")!=-1) {

                //getting the context encoding (eg. base64)
                var context_encoding = current_content.substring(context_encoding_index  + context_encoding_text.length, content_location_index);

                //getting the content location (eg. file:///HTMLImage1.jpg)
                var content_location = current_content.substring(content_location_index  + content_location_text.length, 
                        current_content.indexOf(".", content_location_index) + 4);

                //getting the image name (eg. HTMLImage1.jpg)
                var img_name = content_location.substring(content_location.lastIndexOf("/")+1, content_location.length);

                //getting the image in base64 (eg. /9j/4AAQSkZJRgABAgEBLAEsAAD...)
                var image_content = current_content.substring(current_content.indexOf(img_name) + img_name.length, 
                        end_content_index);
                //image_content = image_content.replace(/( |\n|\r)/g, "");

                //creating the image src (eg. data:image/jpg;base64,/9j/4AAQSkZJRgABAgEBLAEsAAD...))
                var textContent = "src=\"data:" + context_type + ";" + context_encoding + "," + image_content;

                //replace the src in the image
                returnHTML = returnHTML.replace("src=\""+img_name, textContent);

            }

        }

        //getting the next index of contents
        begin_content_index = strMHTML.indexOf(begin_content_text, end_content_index);

    }

    return returnHTML;

}

//
var w=window.open(URL,“popUpWindow”;“宽度=650,高度=450,可调整大小=yes,滚动条=yes,菜单栏=no,工具栏=no,位置=no,目录=no,状态=no”);
w、 write(getContentFromMHTML(mhtmlFileContent));
w、 document.close();
/**
*在MHTML参数的基础上获取html内容和css+img。
*@param strMHTML
*@返回
*/
函数getContentFromMHTML(strMHTML){
//设置bundary.For默认值为“----=\u NextPart\u EXSTREAMHTML”
var boundary_text=“---=_NextPart_EXSTREAMHTML”;
var boundary\u index=strMHTML.indexOf(“boundary=\”);
如果(boundary_index!=-1)boundary_text=“--”+strMHTML.substring(boundary_index+10,strMHTML.indexOf(“\”;”,boundary_index+10));
//设置html和css内容
var returnHTML=setHTMLAndCSSFromMHTML(strMHTML,边界文本);
//将img内容设置为所有img src
returnHTML=setImgFromMHTML(returnHTML、strMHTML、boundary_text);
返回HTML;
}
/**
*获取html内容。
*提取样式标签中的css文件并将其添加到头部。
*最后,将最终内容添加到弹出窗口 *@param strMHTML *@param boundary_text *@返回 */ 函数setHtmlLandCssFrommHTML(strMHTML,边界文本){ //开始标记
var tag_begin_doc=“我也遇到了这个问题,并报告了这个问题:太遗憾了:(如果谷歌有一天能修复它,那就太好了。我想把它们像ZIP文件一样用于那些不会改变的文档,但包含脚本、链接和图像……他们没有修复就关闭了这个bug!)!