Avatar billede alexanderss Nybegynder
25. marts 2011 - 16:12 Der er 9 kommentarer

Problemer med Regex.Replace af JavaScript

Basalt set vil jeg fjerne nogle SharePoint elementer fra HTML-koden
Jeg fjerner ViewState osv., men vil også fjerne nogle standard SharePoint JS filer og CSS. Via. internettet har jeg fundet frem til nogle forskellige måder at gøre det på og har fundet ud af at fjerne alt undtagen SharePoint javascriptet.. Jeg håber I kan hjælpe mig ... Prøv mig frem med en kode som den her:

html = Regex.Replace(html, @"<script[^>]*src=""/_layouts/1033/.*?></script>\s*", "");

Men den virker ikke og giver mig fejl .. Det er filer som WebResource.axd og init.js, der skal fjernes. Koden er følgende:

<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>

<script runat="server" language="c#">

protected override void Render(HtmlTextWriter writer)
{
   using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))
   {
      base.Render(htmlwriter);
      string html = htmlwriter.InnerWriter.ToString();


                // removing of all hidden MSO... and __SPCEditMenu fields for page editing
                html = Regex.Replace(html, @"<input type=""hidden"" name=""(MSO|__SPSCEditMenu|__VIEWSTATE|__EVENTTARGET|__EVENTARGUMENT|__REQUESTDIGEST).*?/>\s*", String.Empty); 
                // removes extra /1033/ CSS.
                html = Regex.Replace(html, @"<link .*?href=""/_layouts/1033.*? rel=""stylesheet""/>", "");
           

      writer.Write(html.Trim());
   }
}
</script>

</head>

<input type="hidden" name="__SPSCEditMenu" id="__SPSCEditMenu" value="true" />
<input type="hidden" name="MSOWebPartPage_PostbackSource" id="MSOWebPartPage_PostbackSource" value="" />
<input type="hidden" name="MSOTlPn_SelectedWpId" id="MSOTlPn_SelectedWpId" value="" />
<input type="hidden" name="MSOTlPn_View" id="MSOTlPn_View" value="0" />
<input type="hidden" name="MSOTlPn_ShowSettings" id="MSOTlPn_ShowSettings" value="False" />
<input type="hidden" name="MSOGallery_SelectedLibrary" id="MSOGallery_SelectedLibrary" value="" />
<input type="hidden" name="MSOGallery_FilterString" id="MSOGallery_FilterString" value="" />
<input type="hidden" name="MSOTlPn_Button" id="MSOTlPn_Button" value="none" />
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__REQUESTDIGEST" id="__REQUESTDIGEST" value="0x046616CC476E811A09929B12A414476254C102F885265E9B7140452956ADCCF9DA7701F5FF05FB7EEF7420917C583425B976A96BBA067D0278327CDF18A67264,25 Mar 2011 13:13:18 -0000" />
<input type="hidden" name="MSOAuthoringConsole_FormContext" id="MSOAuthoringConsole_FormContext" value="" />
<input type="hidden" name="MSOAC_EditDuringWorkflow" id="MSOAC_EditDuringWorkflow" value="" />
<input type="hidden" name="MSOSPWebPartManager_DisplayModeName" id="MSOSPWebPartManager_DisplayModeName" value="Browse" />
<input type="hidden" name="MSOWebPartPage_Shared" id="MSOWebPartPage_Shared" value="" />
<input type="hidden" name="MSOLayout_LayoutChanges" id="MSOLayout_LayoutChanges" value="" />
<input type="hidden" name="MSOLayout_InDesignMode" id="MSOLayout_InDesignMode" value="" />
<input type="hidden" name="MSOSPWebPartManager_OldDisplayModeName" id="MSOSPWebPartManager_OldDisplayModeName" value="Browse" />
<input type="hidden" name="MSOSPWebPartManager_StartWebPartEditingName" id="MSOSPWebPartManager_StartWebPartEditingName" value="false" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKLTM4NTU0ODUwNw9kFgJmD2QWAgIBD2QWBAIBD2QWBAIFD2QWAmYPZBYCAgEPFgIeE1ByZXZpb3VzQ29udHJvbE1vZGULKYgBTWljcm9zb2Z0LlNoYXJlUG9pbnQuV2ViQ29udHJ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FoZAIFD2QWAgIBDxYCHwFoFgJmD2QWBAICD2QWBgIBDxYCHwFoZAIDDxYCHwFoZAIFDxYCHwFoZAIDDw8WAh8MBQEvZGQCBQ8PFgQfAgUHbWFpbm5hdh8DAgJkZAIXD2QWCAIBDxYCHwFoZAIDD2QWAgIBDxYEHwUFI2h0dHA6Ly93d3cua2VtcGluc2tpLmNvbS9lbi9kcmVzZGVuHglpbm5lcmh0bWwFKUhvdGVsIFRhc2NoZW5iZXJncGFsYWlzIEtlbXBpbnNraSBEcmVzZGVuZAIPD2QWAgIJDxYCHwFnFgJmDxYCHwUFPGh0dHA6Ly93d3cua2VtcGluc2tpLmNvbS9lbi9jb250YWN0dXMvUGFnZXMvQ29udGFjdEZvcm0uYXNweGQCEQ88KwASAQA8KwAJAQAPFgIfAWhkZGTJH+NXCicG9wmrr+rVyL0eb43DzQ==" />

<link href="/_layouts/1033/styles/Menu.css?rev=jQ88ZMCVEKRn%2FLeYuywntQ%3D%3D" rel="stylesheet"/>
<script src="/_layouts/1033/init.js?rev=qX%2BG3yl4pldKy9KbPLXf9w%3D%3D"></script>


<script type="text/javascript" language="javascript" src="/_layouts/1033/msstring.js?rev=fcpiBo%2BQtJqYMECz%2BNiH7Q%3D%3D"></script>
<script type="text/javascript" language="javascript" src="/_layouts/1033/non_ie.js?rev=yfNry4hY0Gwa%2FPDNGrqXVg%3D%3D"></script>

<h1></h1><script src="/WebResource.axd?d=PCgIEmCWf1L3tTbJVFg6dw2&amp;t=634132956547557233" type="text/javascript"></script>




<body>
<p>zHello World!</p>
</body>
</html>
Avatar billede johny Nybegynder
26. marts 2011 - 16:14 #1
Hmm, hvilke fejl er det den giver? I Expresso ser RegEx'en ud til at virke som den skal?
Avatar billede alexanderss Nybegynder
26. marts 2011 - 16:17 #2
Hej Johny,
virker html = Regex.Replace(html, @"<script[^>]*src=""/_layouts/1033/.*?></script>\s*", ""); i Expresso???
Avatar billede johny Nybegynder
26. marts 2011 - 16:27 #3
<script[^>]*src="/_layouts/1033/.*?></script>\s*

Ovenstående finder 3 hits i din HTML:
<script src="/_layouts/1033/init.js?rev=qX%2BG3yl4pldKy9KbPLXf9w%3D%3D"></script>

<script type="text/javascript" language="javascript" src="/_layouts/1033/msstring.js?rev=fcpiBo%2BQtJqYMECz%2BNiH7Q%3D%3D"></script>

<script type="text/javascript" language="javascript" src="/_layouts/1033/non_ie.js?rev=yfNry4hY0Gwa%2FPDNGrqXVg%3D%3D"></script>

Men du skriver bare at det fejler, så var derfor jeg gerne ville vide hvordan det fejler?
Avatar billede alexanderss Nybegynder
26. marts 2011 - 16:30 #4
Kan du evt. teste det på en ASP.NET server eller lokalt, for at se om det er min konfiguration, der er noget galt med?
Avatar billede alexanderss Nybegynder
26. marts 2011 - 16:31 #5
Jeg får den her fejl:

SServer Error in '/' Application

Compilation Error

Description: Error compiling a resource required to service this request. Review your source file and modify it to fix this error.

Compiler Error Message: CS1525: Unexpected symbol `~'

Source Error:

Line 26:    }
Line 27: }
Line 28: </script>
Line 29:
Line 30: </head>

Source File: /Users/Alexander/virk.aspx  Lines: 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 129
Avatar billede johny Nybegynder
26. marts 2011 - 16:41 #6
Og hvis du fjerner C# koden fra filen, får du så stadig fejlen?
Avatar billede alexanderss Nybegynder
26. marts 2011 - 17:05 #7
Altså hvilken C# kode er det du mener?
Avatar billede alexanderss Nybegynder
26. marts 2011 - 17:07 #8
Men som sagt koden virker perfekt indtil jeg tilføjer denne ekstra kode: html = Regex.Replace(html, @"<script[^>]*src=""/_layouts/1033.*?></script>", "");
Avatar billede johny Nybegynder
26. marts 2011 - 18:01 #9
Har desværre ikke nogen webserver jeg kan teste på hverken lokalt eller på et webhotel lige pt. desværre.

Er der en bestemt grund til at du har c# koden til at ligge i aspx filen og ikke code behind?

Men fejlen, skyldes det så ikke i stedet at du fjerner noget der ikke kan undværes?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester