0

I need to run a RegEx on the content of any HTML Pro module before the page loads. I assume the custom code would go into this file: /DesktopModules/HTML/HtmlModule.ascx -- "OnPreRender" looks like a good place to me. Is this the right place/file to modify?

The purpose of the RegEX is to replace email addresses found in the text with a modified version of themselves, to prevent spambots from scraping them. (There used to be some modules available in DNN Store that did that, but they're now discontinued; I've used them before but they slowed the page load times down too much.)

I've done some research but haven't found any usable examples. What I did find isn't enough for me to implement what I want:

Can anyone help me with a starter example of code to do the following:

  1. Before the content of HTML Pro module is sent to the browser, grab the latest version of HTML content
  2. Run a Regex.Replace on it
  3. Proceed with loading the page using the modified HTML content

Right now the section I'm looking at looks like this -- where would I insert that code?

    protected override void OnPreRender(System.EventArgs e) 
{
    if (true && HttpRuntime.Cache.Get("avt.MyTokens2.CorePatched") == "true") {
        base.OnPreRender(e);
        return;
    }

    if (Convert.ToBoolean(Settings["MyTokensReplace"]) == true) {
            (lblContent.Controls[0] as LiteralControl).Text = Tokenize((lblContent.Controls[0] as LiteralControl).Text, ModuleConfiguration, false, true);
        }

    base.OnPreRender(e);
}

Thanks for any and all help!

Community
  • 1
  • 1
Bogdan
  • 129
  • 10

1 Answers1

0

Bogdan,

Good hearing from you as well! The below code is working for me, though I don't have the HTML Pro module, so some slight adjustments may be needed. Not sure what you're wanting to do with the email address, but the below code simply adds an "X" where the @ sign was at previously. You can adjust as needed.

<%@ Import namespace="DotNetNuke.Modules.Html" %>
<%@ Import namespace="DotNetNuke.Entities.Portals" %>
<script language="c#" runat="server">

 protected override void OnPreRender(System.EventArgs e) 
{
    HtmlTextController htmlTextController = new HtmlTextController();
    WorkflowStateController workflowStateController = new WorkflowStateController();
    int workflowId = htmlTextController.GetWorkflow(ModuleId, TabId, PortalId).Value;

    HtmlTextInfo htmlContent = htmlTextController.GetTopHtmlText(ModuleId, false, workflowId);
    if (htmlContent == null)
    {
        htmlContent = new HtmlTextInfo();
        htmlContent.ItemID = -1;
        htmlContent.StateID = workflowStateController.GetFirstWorkflowStateID(workflowId);
        htmlContent.WorkflowID = workflowId;
        htmlContent.ModuleID = ModuleId;
        htmlContent.IsPublished = true;
        htmlContent.Approved = true;
        htmlContent.IsActive = true;
    }

    string pattern = "@";
    string replacement = "X";
    Regex rgx = new Regex(pattern);
    string result = rgx.Replace(htmlContent.Content,replacement);

    htmlContent.Content = result;

    int draftStateId = workflowStateController.GetFirstWorkflowStateID(workflowId);
    int nextWorkflowStateId = workflowStateController.GetNextWorkflowStateID(workflowId, htmlContent.StateID);
    int publishedStateId = workflowStateController.GetLastWorkflowStateID(workflowId);

    htmlTextController.UpdateHtmlText(htmlContent, htmlTextController.GetMaximumVersionHistory(PortalId));
}

</script>
jacton
  • 3
  • 4
  • Justin, we still do use MyTokens, but I can't really imagine how you'd use this functionality. I want to keep the input method the same/default and only modify the data before outputting it onto the web page. What can MyTokens do that cannot be done with straight C# (which also supports RegEx)? Please clarify. P.S. Nice to hear from you, man! – Bogdan Jan 19 '17 at 17:06