Pins a JavaScript snippet for execution in the browser without transmitting the entire script across the wire for every execution. The JavaScript to pin A task containing a object to use to execute the script. If is
([StringSyntax(StringSyntaxConstants.JavaScript)] string script)
| 221 | /// <returns>A task containing a <see cref="PinnedScript"/> object to use to execute the script.</returns> |
| 222 | /// <exception cref="ArgumentNullException">If <paramref name="script"/> is <see langword="null"/>.</exception> |
| 223 | public async Task<PinnedScript> PinScript([StringSyntax(StringSyntaxConstants.JavaScript)] string script) |
| 224 | { |
| 225 | ArgumentNullException.ThrowIfNull(script); |
| 226 | |
| 227 | string newScriptHandle = Guid.NewGuid().ToString("N"); |
| 228 | |
| 229 | // We do an "Evaluate" first so as to immediately create the script on the loaded |
| 230 | // page, then will add it to the initialization of future pages. |
| 231 | await this.EnableDomains().ConfigureAwait(false); |
| 232 | |
| 233 | string creationScript = PinnedScript.MakeCreationScript(newScriptHandle, script); |
| 234 | await this.session.Value.Domains.JavaScript.Evaluate(creationScript).ConfigureAwait(false); |
| 235 | string scriptId = await this.session.Value.Domains.JavaScript.AddScriptToEvaluateOnNewDocument(creationScript).ConfigureAwait(false); |
| 236 | |
| 237 | PinnedScript pinnedScript = new PinnedScript(script, newScriptHandle, scriptId); |
| 238 | this.pinnedScripts[pinnedScript.Handle] = pinnedScript; |
| 239 | return pinnedScript; |
| 240 | } |
| 241 | |
| 242 | /// <summary> |
| 243 | /// Unpins a previously pinned script from the browser. |