为 .mht 文件启用脚本



每次我在谷歌浏览器中加载MHT时,都会收到以下错误:

阻止了"file:///C:/..."中的脚本执行因为文档的框架已沙盒化,并且未设置"允许脚本"权限

我知道allow-scriptsiframe 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;
}
//

问候

似乎是一个已知的错误:https://code.google.com/p/chromium/issues/detail?id=439832

最新更新