Enabling scripts for .mht-files?

眉间皱痕 提交于 2020-01-04 03:56:29

问题


Everytime I load a MHT in Google Chrome, I get following error:

Blocked script execution in 'file:///C:/...' because the document's frame is sandboxed and the 'allow-scripts' permission is not set.

I know that allow-scripts is a value for the iframe's sandbox attribute, so I can't set its value to allow-scripts, because the top frame seems to be an internal frame. And this internal frame only adds the sandbox attribute, when loading files with file:///. Is there a way to allow scripts to run without using http://?

I also tried to uncheck Disable JavaScript in the settings of the Development Tools, but this checkbox was greyed out.


回答1:


This seems to be a known bug: https://code.google.com/p/chromium/issues/detail?id=439832




回答2:


In base of mhtml file with this structure:

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

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

You can to use a similar script for create a html from mhtml and open a new window:

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;

}

//

Regards,



来源:https://stackoverflow.com/questions/27986868/enabling-scripts-for-mht-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!