【问题标题】:How to remove <script> tags from an HTML page using C#?如何使用 C# 从 HTML 页面中删除 <script> 标签?
【发布时间】:2013-10-16 22:09:23
【问题描述】:
<html>
    <head>
        <script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript">
            if (window.self === window.top) { $.getScript("Wing.js"); }
        </script>
   </head>
</html>

C#中有没有办法修改上面的HTML文件并转换成这种格式:

<html>
    <head>
    </head>
</html>

基本上,我的目标是从 HTML 页面中删除所有 JavaScript。我不知道修改 HTML 文件的最佳方法是什么。我想以编程方式进行,因为有数百个文件需要修改。

【问题讨论】:

  • Smihit,要非常小心边缘情况(如果你幸运的话,你不会遇到),我在我的回答中提到,你在

标签: javascript c# html


【解决方案1】:

可以使用正则表达式来完成:

Regex rRemScript = new Regex(@"<script[^>]*>[\s\S]*?</script>");
output = rRemScript.Replace(input, "");

【讨论】:

  • 有什么问题?如果存在嵌套脚本标签的可能性,可以使用 Replace while Matches.Count > 0。
  • 这适用于上面给出的示例。我同意这不是最好的方法,应该使用 HTML 敏捷包。但它有效。谢谢大家的回答
  • 请注意,这与&lt;script&gt;asdf&lt;/script&gt; 不匹配(开始标记中没有空格),因此不会删除没有声明类型的脚本。另外,@Jerry,为什么要在标签中匹配[\s\S]*(即任何空格和任何非空格),而不是.*
  • 值得通读XSS Filter Evasion Cheat Sheet 以了解有效的脚本标签有多少种格式化可能性
【解决方案2】:

可能值得一看:HTML Agility Pack

编辑:具体的工作代码

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
string sampleHtml = 
    "<html>" +
        "<head>" + 
                "<script type=\"text/javascript\" src=\"jquery.js\"></script>" +
                "<script type=\"text/javascript\">" + 
                    "if (window.self === window.top) { $.getScript(\"Wing.js\"); }" +
                "</script>" +
        "</head>" +
    "</html>";
MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(sampleHtml));

doc.Load(ms);

List<HtmlNode> nodes = new List<HtmlNode>(doc.DocumentNode.Descendants("head"));
int childNodeCount = nodes[0].ChildNodes.Count;
for (int i = 0; i < childNodeCount; i++)
    nodes[0].ChildNodes.Remove(0);
Console.WriteLine(doc.DocumentNode.OuterHtml);

【讨论】:

  • 我同意,但也许你的答案可以更具体一点?
  • 脚本标签不在头部怎么办?
  • 只需将对 Descendants("head") 的调用替换为它所来自的任何标签。如果“html”位于头部之外,我相信它会起作用
  • -!该示例不删除脚本标签,它从头中删除所有元素。 -!不需要 MemoryStream。 doc.LoadHtml(sampleHtml);
【解决方案3】:

我认为正如其他人所说,HtmlAgility 包是最好的路线。我用它来并移除大量难以角落的情况。但是,如果您的目标是一个简单的正则表达式,那么也许您可以尝试&lt;script(.+?)*&lt;/script&gt;。这将删除讨厌的嵌套 javascript 以及正常的东西,即链接中提到的类型 (Regular Expression for Extracting Script Tags):

<html>
<head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">
        if (window.self === window.top) { $.getScript("Wing.js"); }
    </script>
    <script> // nested horror
    var s = "<script></script>";
    </script>
</head>
</html>

用法:

Regex regxScriptRemoval = new Regex(@"<script(.+?)*</script>");
var newHtml = regxScriptRemoval.Replace(oldHtml, "");

return newHtml; // etc etc

【讨论】:

    【解决方案4】:

    这似乎是一个奇怪的解决方案。

    如果您不想使用任何第三方库来执行此操作并且不需要实际删除脚本代码,只需禁用它,您可以这样做:

    html = Regex.Replace(html , @"<script[^>]*>", "<!--");
    html = Regex.Replace(html , @"<\/script>", "-->");
    

    这会从脚本标签中创建一个 HTML 注释。

    【讨论】:

      【解决方案5】:

      使用正则表达式:

      string result = Regex.Replace(
          input, 
          @"</?(?i:script|embed|object|frameset|frame|iframe|meta|link|style)(.|\n|\s)*?>", 
          string.Empty, 
          RegexOptions.Singleline | RegexOptions.IgnoreCase
      );
      

      【讨论】:

      • 我试过这个,它删除了脚本标签 - 以及我的 HTML 中的所有其他标签。 (我只剩下一个空白字符串)
      猜你喜欢
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 2014-01-11
      • 2022-12-05
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-03-06
      相关资源
      最近更新 更多