Skip to content Skip to sidebar Skip to footer

Enabling Scripts For .mht-files?

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' per

Solution 1:

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:

varw= 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"varboundary_text="------=_NextPart_EXSTREAMHTML"; 
    varboundary_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 contentvarreturnHTML= 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 tagsvartag_begin_doc="<!DOCTYPE HTML"; 
    vartag_begin="<html>"; 
    vartag_begin_upper="<HTML>";

    //end tagsvartag_end="</html>"; 
    vartag_end_upper="</HTML>";

    //getting the start of html contentvarbegin= 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 contentvarend= strMHTML.indexOf(tag_end); 
        if(end==-1) end = strMHTML.indexOf(tag_end_upper);

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

    //Is necessary add the css before to add the html content to popUpvarstrHTML= 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) {

    varbegin_content_text= boundary_text;
    varcontent_type_text="Content-Type: text/css";

    //getting the first index of contentsvarbegin_content_index= strMHTML.indexOf(begin_content_text);

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

        //getting the final index of current contentvarend_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 contentvarcurrent_content= strMHTML.substring(begin_content_index, end_content_index);

        //getting the indexesvarcontent_type_index= current_content.indexOf(content_type_text);

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

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

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

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

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

            //Adding the css to end of head of htmlvarstrHTMLtext="";
            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){

    varbegin_content_text= boundary_text;
    varcontent_type_text="Content-Type: ";
    varcontext_encoding_text="Content-Transfer-Encoding: ";
    varcontent_location_text="Content-Location: ";

    //getting the first index of contentsvarbegin_content_index= strMHTML.indexOf(begin_content_text);

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

        //getting the final index of current contentvarend_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 contentvarcurrent_content= strMHTML.substring(begin_content_index, end_content_index);

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

            //getting the indexesvarcontent_type_index= current_content.indexOf(content_type_text);
            varcontext_encoding_index= current_content.indexOf(context_encoding_text);
            varcontent_location_index= current_content.indexOf(content_location_text);

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

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

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

                //getting the content location (eg. file:///HTMLImage1.jpg)varcontent_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)varimg_name= content_location.substring(content_location.lastIndexOf("/")+1, content_location.length);

                //getting the image in base64 (eg. /9j/4AAQSkZJRgABAgEBLAEsAAD...)varimage_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...))vartextContent="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,

Solution 2:

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

Post a Comment for "Enabling Scripts For .mht-files?"