let js_code: String = format!(r#"
document.open();
document.write(`{}`);
document.close();
"#,safe_html);
tab.evaluate(&js_code, true)?;
具体细节:
//转义html
fn escape_js_template(html: &str) -> String {
html.replace('\\', r"\\") // 转义反斜杠
.replace('`', r"\`") // 转义反引号
.replace('$', r"\$") // 防止 `${}` 误解析
}
let html = r#"<doctype html>
<html>
<head><title>标题</title></head>
<body>主体
`ffffff`
<a href="http://www.baidu.com">百度</a>
</body>
</html>"#;
let safe_html = escape_js_template(html);
let js_code: String = format!(r#"
document.open();
document.write(`{}`);
document.close();
"#,safe_html);
tab.evaluate(&js_code, true)?;