/// <exception cref="InvalidOperationException">When the underlying <see cref="WebViewControl"/> is not yet initialized.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="scriptName"/> is <see langword="null"/></exception>
        internal Task <string> InvokeScriptAsync(string scriptName, IEnumerable <string> arguments)
        {
            Verify.IsFalse(IsDisposed);
            Verify.IsNotNull(_webViewControl);
            Verify.IsNeitherNullNorEmpty(scriptName);

            if (string.IsNullOrEmpty(scriptName))
            {
                throw new ArgumentNullException(nameof(scriptName));
            }

            // Cannot invoke script
            if (_webViewControl == null)
            {
                throw new InvalidOperationException(DesignerUI.E_WEBVIEW_CANNOT_INVOKE_BEFORE_INIT);
            }

            // Protect against the cross domain scripting attacks
            // If it is our internal navigation to about:blank for navigating to null or load string or before navigation has happened, Source will be null
            var currentSource = Source;

            if (currentSource != null)
            {
                Security.DemandWebPermission(currentSource);
            }

            return(_webViewControl
                   .InvokeScriptAsync(scriptName, arguments)
                   .AsTask());
        }
Example #2
0
        private async void InvokeScript()
        {
            // Invoke the javascript function called 'doSomething' that is loaded into the WebView.
            try
            {
                string result = await WebViewControl.InvokeScriptAsync("doSomething", null);

                rootPage.NotifyUser($"Script returned \"{result}\"", NotifyType.StatusMessage);
            }
            catch (Exception ex)
            {
                string errorText = null;
                switch (ex.HResult)
                {
                case unchecked ((int)0x80020006):
                    errorText = "There is no function called doSomething";
                    break;

                case unchecked ((int)0x80020101):
                    errorText = "A JavaScript error or exception occured while executing the function doSomething";
                    break;

                case unchecked ((int)0x800a138a):
                    errorText = "doSomething is not a function";
                    break;

                default:
                    // Some other error occurred.
                    errorText = ex.Message;
                    break;
                }
                rootPage.NotifyUser(errorText, NotifyType.ErrorMessage);
            }
        }
Example #3
0
        private async void WebViewControl_LoadCompleted(object sender, NavigationEventArgs e)
        {
            // var result = await GetKeywordsAS.Autosuggest();
            StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Helpers/WebviewScript.js"));

            if (file != null)
            {
                string js = await FileIO.ReadTextAsync(file);

                //await GetContentFromFile(file);
                //eval函数大家都知道,就是执行一段字符串
                await WebViewControl.InvokeScriptAsync("eval", new string[] { js });
            }
        }
        private async void GetBrowserCookie()
        {
            bool loggedin = false;

            try
            {
                var httpBaseProtocolFilter = new HttpBaseProtocolFilter();
                var cookieManager          = httpBaseProtocolFilter.CookieManager;
                var cookieCollection       = cookieManager.GetCookies(HomeUri);
                loggedin = cookieCollection.Where(c => c.Name.Equals("SESSID")).Count() > 0;
                if (loggedin)
                {
                    string channel = await registerPushChannel();

                    string[] objArray = new string[1];
                    objArray[0] = "updatePushToken({ mpn: \"" + channel + "\"})";
                    await WebViewControl.InvokeScriptAsync("eval", objArray);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                this.showNoInternetMessageAndCloseApp();
            }

            if (loggedin == false)
            {
                if (alreadyhandled == false)
                {
                    try
                    {
                        List <string> parms = new List <string>();
                        parms.Add("$('.showSideNav').trigger('click');");
                        await WebViewControl.InvokeScriptAsync("eval", parms);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                        this.showNoInternetMessageAndCloseApp();
                    }
                }
            }
            alreadyhandled = true;
        }
 private async void WebView_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
 {
     string result = await WebViewControl.InvokeScriptAsync("eval", new string[] { "window.alert = function (AlertMessage) {window.external.notify(AlertMessage)}" });
 }
 public async Task <string?> ExecuteScriptAsync(string script)
 {
     return(await webview.InvokeScriptAsync("eval", new string[] { script }));
 }