如何解决Cheerio 加载字符串而不替换内部脚本内容
我有带有脚本的字符串 HTML 并做了一些事情。所以我使用cheerio npm来替换/附加内容。
示例 Html 字符串:
var htmlStr = `
<!DOCTYPE html>
<html lang="en">
<head>
<script>
(function(e,t)("$script",function(){
e.indexOf(".js")===-1&&!/^https?:\/\//.test(e)
}));
</script>
</head>
<body>
<div class="content"> Test html</div>
</body>
</html>
`;
const $ = cheerio.load(htmlStr);
//const $ = cheerio.load(htmlStr,{ decodeEntities: false });
$('.content').html('Changed content');
console.log($.html());
电流输出:
<!DOCTYPE html><html lang="en"><head>
<script>
(function(e,function(){
e.indexOf(".js")===-1&&!/^https?:///.test(e)
}));
</script>
</head>
<body>
<div class="content">Changed content</div>
</body></html>
这里,https?:\/\//
替换为 https?:///
。但我期待没有替换脚本的输出。
我尝试与 { decodeEntities: false }
一起使用但不起作用。
我只在这段代码中添加了示例..我有更多的脚本代码.. 预期输出:
<!DOCTYPE html>
<html lang="en">
<head>
<script>
(function(e,function(){
e.indexOf(".js")===-1&&!/^https?:\/\//.test(e)
}));
</script>
</head>
<body>
<div class="content">Changed content</div>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。