コード例 #1
0
        /// <summary>
        /// Unpins a previously pinned script from the browser.
        /// </summary>
        /// <param name="script">The <see cref="PinnedScript"/> object to unpin.</param>
        /// <returns>A task that represents the asynchronous operation.</returns>
        public async Task UnpinScript(PinnedScript script)
        {
            if (this.pinnedScripts.ContainsKey(script.Handle))
            {
                await this.session.Value.Domains.JavaScript.Evaluate(script.RemovalScript);

                await this.session.Value.Domains.JavaScript.RemoveScriptToEvaluateOnNewDocument(script.ScriptId);

                this.pinnedScripts.Remove(script.Handle);
            }
        }
コード例 #2
0
        /// <summary>
        /// Pins a JavaScript snippet for execution in the browser without transmitting the
        /// entire script across the wire for every execution.
        /// </summary>
        /// <param name="script">The JavaScript to pin</param>
        /// <returns>A task containing a <see cref="PinnedScript"/> object to use to execute the script.</returns>
        public async Task <PinnedScript> PinScript(string script)
        {
            // We do an "Evaluate" first so as to immediately create the script on the loaded
            // page, then will add it to the initialization of future pages.
            PinnedScript pinnedScript = new PinnedScript(script);

            await this.EnableDomains();

            await this.session.Value.Domains.JavaScript.Evaluate(pinnedScript.CreationScript);

            pinnedScript.ScriptId = await this.session.Value.Domains.JavaScript.AddScriptToEvaluateOnNewDocument(pinnedScript.CreationScript);

            this.pinnedScripts[pinnedScript.Handle] = pinnedScript;
            return(pinnedScript);
        }
コード例 #3
0
 /// <summary>
 /// Executes JavaScript in the context of the currently selected frame or window
 /// </summary>
 /// <param name="script">A <see cref="PinnedScript"/> object containing the JavaScript code to execute.</param>
 /// <param name="args">The arguments to the script.</param>
 /// <returns>The value returned by the script.</returns>
 public object ExecuteScript(PinnedScript script, params object[] args)
 {
     return(this.ExecuteScript(script.ExecutionScript, args));
 }