Пример #1
0
        /// <summary>
        /// Execute a chunk of JavaScript, wait for completion & return response
        /// </summary>
        /// <param name="script"></param>

        public static object ExecuteScript(
            HelmWinFormsBrowserMsft helmBrowser,
            string script)
        {
            //if (DebugMx.True) return new JavascriptResponse(); // disable - debug

            Stopwatch sw = Stopwatch.StartNew();

            if (helmBrowser?.Browser?.Document == null)
            {
                AssertMx.IsNotNull(helmBrowser?.Browser?.Document, "HelmBrowser?.Browser?.Document");
            }

            WebBrowser browser = helmBrowser.Browser;

            if (Debug)
            {
                DebugLog.Message("Script: " + script + helmBrowser.IdText);
            }

            object result = browser.Document.InvokeScript("execScript", new Object[] { script, "JavaScript" });

            if (Debug)
            {
                DebugLog.StopwatchMessage("Script complete, Time: ", sw);
            }

            return(result);
        }
Пример #2
0
        /// <summary>
        /// Call a function in a script
        /// </summary>
        /// <param name="jsMethodName"></param>
        /// <param name="args">Itemized list of args, not an array</param>
        /// <returns>Function result</returns>

        public static string CallFunction(
            HelmWinFormsBrowserMsft helmBrowser,
            string jsMethodName,
            params string[] args)
        {
            object resultObj = null;

            Stopwatch sw = Stopwatch.StartNew();

            WebBrowser browser = helmBrowser.Browser;

            if (browser == null || browser.IsDisposed || browser.Document == null)
            {
                return(null);
            }

            string funcCallString = jsMethodName + "(" + Lex.ArrayToCsvString(args) + ")";

            if (Debug)
            {
                DebugLog.Message("Script function call: " + funcCallString + helmBrowser.IdText);
            }

            if (args == null || args.Length == 0)             // no args
            {
                resultObj = browser.Document.InvokeScript(jsMethodName);
            }

            else             // convert args string array to object array
            {
                object[] oArgs = new object[args.Length];
                args.CopyTo(oArgs, 0);
                resultObj = browser.Document.InvokeScript(jsMethodName, args);
            }

            string resultString = (resultObj != null ? resultObj.ToString() : null);

            if (Debug)
            {
                DebugLog.StopwatchMessage("Script function call complete, Time: ", sw);
            }

            return(resultString);
        }
Пример #3
0
 public JavaScriptManager(HelmWinFormsBrowserMsft helmBrowser)         // create a script manager and reference it back to us
 {
     HelmBrowser = helmBrowser;
 }