不允许运行JavaScript的可编辑iframe

不允许运行JavaScript的可编辑iframe,javascript,iframe,sandbox,Javascript,Iframe,Sandbox,我有一个iframe: <iframe id="msgContainer" sandbox="allow-same-origin"></iframe> 我想在其中插入HTML,但不允许运行HTML中可能包含的任何JavaScript(这包括标记和*属性上的。我知道如何插入HTML(只需使用document.getElementById('msgContainer')).contentDocument.body.innerHTML=myHTML但我想阻止myHTML中

我有一个iframe:

<iframe id="msgContainer" sandbox="allow-same-origin"></iframe>

我想在其中插入HTML,但不允许运行HTML中可能包含的任何JavaScript(这包括
标记和*属性上的
。我知道如何插入HTML(只需使用
document.getElementById('msgContainer')).contentDocument.body.innerHTML=myHTML
但我想阻止
myHTML
中的任何JS运行。我尝试的方法是使用
sandbox
属性,只允许相同的源代码,但JS仍在运行。有什么方法可以做到这一点吗


谢谢

除了从插入iframe的html字符串中解析JS之外,我找不到任何答案。以下是我的代码(如果它对其他人有帮助的话):

/**从html字符串中删除javascript
*html:要清理的字符串
*/
函数清理(html){
函数stripHTML(){
html=html.slice(0,strip)+html.slice(j);
j=条带;
strip=false;
}
var strip=false,
lastQuote=false,
标签=假;
const prefix=“任何内容”,
sandbox=“sandbox=”;
对于(变量i=0;i 0){
html=html.slice(0,i+索引)+前缀+html.slice(i+索引);
j+=前缀长度;
}
html=html.slice(0,j)+沙盒+html.slice(j);
j+=沙箱长度;
}
i=j;
打破
}
如果(!tag&&html[j]==“”){
tag=html.slice(i,j).toLowerCase();
}
if(lastQuote==html[j]){
lastQuote=false;
继续;
}
如果(!lastQuote&&html[j-1]==”&&&(html[j]==”'“| | html[j]==”)){
lastQuote=html[j];
}
/*在语句中查找*/
如果(!lastQuote&&html[j-2]===”&&html[j-1]===“o”&&html[j]==“n”){
条带=j-2;
}
if(strip&&html[j]==“”&&&!lastQuote){
stripHTML();
}
}
}
}
html=条带脚本(html);
返回html;
}
/**返回该字符是否为标记中的有效第一个字符
*str:第一个字符
*/
函数为validtaghar(str){
返回str.match(/[a-z?\\\/!]/i);
}
/**从html字符串中剥离脚本
*html:要剥离的html字符串
*/
//注意:标记不会在此上下文中运行
函数脚本(html){
var div=document.createElement('div');
div.innerHTML=html;
var scripts=div.getElementsByTagName(“脚本”);
var i=scripts.length;
而(我--){
脚本[i].parentNode.removeChild(脚本[i]);
}
返回div.innerHTML;
}

可能重复的是的,我看到了那篇文章。它没有完全回答我的问题,因为:1.我提到了沙盒属性,但找不到编辑
iframe
内容的方法,同时阻止使用沙盒执行JS。2.第一个答案唯一有用的建议是regex,它不起作用,因为regex不是上下文无关的
/** Removes javascript from html string
 * html: the string to be cleaned
*/
function clean(html) {
    function stripHTML(){
        html = html.slice(0, strip) + html.slice(j);
        j = strip;
        strip = false;
    }

    var strip = false,
    lastQuote = false,
    tag = false;
    const prefix = "ANYTHING",
    sandbox = " sandbox=''";

    for(var i=0; i<html.length; i++){
        if(html[i] === "<" && html[i+1] && isValidTagChar(html[i+1])) {
            i++;
            tag = false;
            /* Enter element */
            for(var j=i; j<html.length; j++){
                if(!lastQuote && html[j] === ">"){
                    if(strip) {
                        stripHTML();
                    }
                    /* sandbox iframes */
                    if(tag === "iframe"){
                        var index = html.slice(i, j).toLowerCase().indexOf("sandbox");
                        if(index > 0) {
                            html = html.slice(0, i+index) + prefix + html.slice(i+index);
                            j += prefix.length;
                        }
                        html = html.slice(0, j) + sandbox + html.slice(j);
                        j += sandbox.length;
                    }
                    i = j;
                    break;
                }
                if(!tag && html[j] === " "){
                    tag = html.slice(i, j).toLowerCase();
                }
                if(lastQuote === html[j]){
                    lastQuote = false;
                    continue;
                }
                if(!lastQuote && html[j-1] === "=" && (html[j] === "'" || html[j] === '"')){
                    lastQuote = html[j];
                }
                /* Find on statements */
                if(!lastQuote && html[j-2] === " " && html[j-1] === "o" && html[j] === "n"){
                    strip = j-2;
                }
                if(strip && html[j] === " " && !lastQuote){
                    stripHTML();
                }
            }
        }
    }
    html = stripScripts(html);
    return html;
}

/** Returns whether or not the character is a valid first character in a tag
 * str: the first character
*/
function isValidTagChar(str) {
    return str.match(/[a-z?\\\/!]/i);
}

/** Strips scripts from a string of html
 * html: the string of html to be stripped
*/
// NOTE: <script> tags won't run in this context
function stripScripts(html) {
    var div = document.createElement('div');
    div.innerHTML = html;
    var scripts = div.getElementsByTagName('script');
    var i = scripts.length;
    while (i--) {
      scripts[i].parentNode.removeChild(scripts[i]);
    }
    return div.innerHTML;
}