Awesomium 1.7.5.1, WinForms, WebControl obj.ExecuteJavascript

518 views Asked by At

I want to insert html code into existing html code. But I do not see the result. Here is the code C #:

1) Program.cs

public partial class Form1 : Form
{
    public Form1()
    {            
        InitializeComponent();
        wUI.DocumentReady += wUI_DocumentReady;

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // code here ?           
    }

    void wUI_DocumentReady(object sender, DocumentReadyEventArgs e)
    {
        wUI.LoadHTML("<html><body>sadasdsad</body></html>");
        HtmlManager html = HtmlManager.Instance;
        string[] placeholders = { "asset://customdatastore/path/to/any", "type-button", "no-action", "Example link" };
        html.Add("<a href=\"{0}\" class=\"{1}\" id=\"{2}\">{3}</a>", placeholders);
        html.InnerCode(html.Code, wUI, "body");
        wUI.Refresh();
    }
}

2) HtmlManager.cs

public sealed class HtmlManager
{
    private static readonly Lazy<HtmlManager> InstanceField = new Lazy<HtmlManager>(() => new HtmlManager());
    private StringBuilder _stringBuilder = null;
    public string Code { get { return _stringBuilder.ToString(); } }  

    private HtmlManager() 
    {
        if (_stringBuilder != null)
            _stringBuilder.Clear();
        _stringBuilder = new StringBuilder();
    }

    public static HtmlManager Instance { get { return InstanceField.Value; } }

    public void Add(string row, string[] placeholders = null)
    {
        if (placeholders != null)
            _stringBuilder.AppendLine(string.Format(row, placeholders));
        _stringBuilder.AppendLine(row);
    }

    public void InnerCode(string code, object sender, string afterTag = "html")
    {
        Awesomium.Windows.Forms.WebControl ui = (Awesomium.Windows.Forms.WebControl)sender;
        ui.ExecuteJavascript(string.Format("document.getElementsByTagName({0})[0].innerHTML({1})", afterTag, code));            
    }

    public void Clear()
    {
        _stringBuilder.Clear();
    }
} 

The event (DocumentReady) does not happen, I do not believe, maybe I'm wrong somewhere?

UP: I try do it:

private void Form1_Load(object sender, EventArgs e)
    {
        wUI.LoadHTML("<html><body>sadasdsad</body></html>");       
    }

    void wUI_DocumentReady(object sender, DocumentReadyEventArgs e)
    {

        HtmlManager html = HtmlManager.Instance;
        string[] placeholders = { "asset://customdatastore/path/to/any", "type-button", "no-action", "Example link" };
        html.Add("<a href=\"{0}\" class=\"{1}\" id=\"{2}\">{3}</a>", placeholders);

        wUI.ExecuteJavascript("document.getElementsByTagName('body').innerHTML(\"sometext\")");  

        //html.InnerCode(html.Code, wUI, "body");
        //wUI.Refresh();
    }

No result

UP 2:

public void Add(string row, string[] placeholders = null)
    {
        if (placeholders != null)
            _stringBuilder.AppendLine(string.Format(row, placeholders));
        if (placeholders == null)
            _stringBuilder.AppendLine(row);
    }

UP 3:

Work with:

wUI.Source = new Uri(@"http://google.com");

in Form1_Load

1

There are 1 answers

0
voytek On

You can use LoadHtml method, but only after document is fully loaded (don't confuse with DocumentReadyState.Ready) It works for me at least:

    private void WebControl_DocumentReady(object sender, DocumentReadyEventArgs e)
    {
        if (e.ReadyState != DocumentReadyState.Loaded) return;


    }

But as an initialisation, you should use Source property, like you wrote in your third update