locked
Run script in HTML editor in WebView WP8.1

    Question

  • I am developing an app in which I need to give HTML editing facility to the user. So I tried different HTML editors but finally TinyMCE was able to show controls for editing. But I am not able to set the contents of Editor. It gives Exception Exception from HRESULT: 0x80020101. And I tried all different solutions but could not figure it out. Here is link to my project

        string tinyMice = "<script type='text/javascript'> function myfun() {tinymce.execCommand('mceInsertContent', false, getQueryStrings());}myfun()</script>";
        
                        await System.Threading.Tasks.Task.Delay(TimeSpan.FromSeconds(1));
                        await webview_demo.InvokeScriptAsync("eval", new string[] { tinyMice });
                    

    Can somebody help?
    • Edited by RohitrkKUmar Wednesday, February 11, 2015 10:52 AM
    Wednesday, February 11, 2015 10:48 AM

Answers

  • hey,

    I am not sure what you exactly want to do but I took a look at the project you uploaded.

    First of all, you better use the NavigationCompleted or FrameNavigationCompleted event for executing onload functions.

    I have no idea about the tinymce js plugin but here is what I did to create a similar scenario.

    1) First, create a js function in the removeformat.html to return a string (replacing your getQueryStrings functions)

    function returnMessageValue() {
        return "Hello JS World";
    }

    2) then create a script notify event handler and wire it up to the page so we can get alerts from the html page loaded.

    In NavigationCompleted event:

    string result = await this.webview_demo.InvokeScriptAsync("eval", new string[] { "window.alert = function (AlertMessage) {window.external.notify(AlertMessage)}" });
    

    this will notify the webview about window.alert's. So we subscribe to the Script notify event:

    webview_demo.ScriptNotify += (sender, args) =>
    {
        MessageDialog m = new MessageDialog(args.Value);
        m.ShowAsync();
    };

    and finally the code execution for our function:

    await webview_demo.InvokeScriptAsync("eval", new[] { "window.alert(returnMessageValue())" });
    
    // await webview_demo.InvokeScriptAsync("eval",
    //        new[] { "tinymce.execCommand(\"mceInsertContent\", false, getQueryStrings())" });


    result:

    hope it helps


    Can Bilgin
    Blog Samples CompuSight


    • Edited by Can BilginMVP Wednesday, February 11, 2015 2:07 PM code samples formated
    • Marked as answer by RohitrkKUmar Monday, February 16, 2015 4:30 AM
    Wednesday, February 11, 2015 2:05 PM

All replies

  • hey,

    I am not sure what you exactly want to do but I took a look at the project you uploaded.

    First of all, you better use the NavigationCompleted or FrameNavigationCompleted event for executing onload functions.

    I have no idea about the tinymce js plugin but here is what I did to create a similar scenario.

    1) First, create a js function in the removeformat.html to return a string (replacing your getQueryStrings functions)

    function returnMessageValue() {
        return "Hello JS World";
    }

    2) then create a script notify event handler and wire it up to the page so we can get alerts from the html page loaded.

    In NavigationCompleted event:

    string result = await this.webview_demo.InvokeScriptAsync("eval", new string[] { "window.alert = function (AlertMessage) {window.external.notify(AlertMessage)}" });
    

    this will notify the webview about window.alert's. So we subscribe to the Script notify event:

    webview_demo.ScriptNotify += (sender, args) =>
    {
        MessageDialog m = new MessageDialog(args.Value);
        m.ShowAsync();
    };

    and finally the code execution for our function:

    await webview_demo.InvokeScriptAsync("eval", new[] { "window.alert(returnMessageValue())" });
    
    // await webview_demo.InvokeScriptAsync("eval",
    //        new[] { "tinymce.execCommand(\"mceInsertContent\", false, getQueryStrings())" });


    result:

    hope it helps


    Can Bilgin
    Blog Samples CompuSight


    • Edited by Can BilginMVP Wednesday, February 11, 2015 2:07 PM code samples formated
    • Marked as answer by RohitrkKUmar Monday, February 16, 2015 4:30 AM
    Wednesday, February 11, 2015 2:05 PM
  • I tried doing the same but it did not work. Can you share the edited code please?
    Thursday, February 12, 2015 6:57 AM
  • Friday, February 13, 2015 11:10 AM
  • Thanks. And it worked on standalone project but when I try the same code in my project it does not show keyboard on tapping textarea and also shows exception in calling script.
    Monday, February 16, 2015 4:30 AM