You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit ff2b369
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: docs/general/01-Basics.md
+36-37Lines changed: 36 additions & 37 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,11 +4,7 @@ section: "AngleSharp.Js"
4
4
---
5
5
# Getting Started
6
6
7
-
AngleSharp.Js runs JavaScript against an AngleSharp document. It integrates the
8
-
[Jint](https://github.com/sebastienros/jint) interpreter with AngleSharp, so scripts can
9
-
read and change the same DOM that your .NET code uses. This is useful when processing pages
10
-
whose behavior depends on script execution, evaluating a snippet in a document context, or
11
-
hosting JavaScript APIs alongside your application.
7
+
AngleSharp.Js runs JavaScript against an AngleSharp document. It integrates the [Jint](https://github.com/sebastienros/jint) interpreter with AngleSharp, so scripts can read and change the same DOM that your .NET code uses. This is useful when processing pages whose behavior depends on script execution, evaluating a snippet in a document context, or hosting JavaScript APIs alongside your application.
12
8
13
9
## Install and configure
14
10
@@ -18,13 +14,9 @@ Install the package:
18
14
Install-Package AngleSharp.Js
19
15
```
20
16
21
-
Add `WithJs()` to the AngleSharp configuration. Add `WithEventLoop()` when scripts, events,
22
-
or resource callbacks must run in the browser-like task queue. Add a loader when the document
23
-
needs to fetch external scripts or other resources.
17
+
Add `WithJs()` to the AngleSharp configuration. Add `WithEventLoop()` when scripts, events, or resource callbacks must run in the browser-like task queue. Add a loader when the document needs to fetch external scripts or other resources.
24
18
25
-
For many real-world scripts, `WithJs()` alone is not enough. AngleSharp.Js exposes interfaces
26
-
from the AngleSharp services present in the browsing context, so you usually want to include
27
-
AngleSharp.Io and AngleSharp.Css to provide most of the required API surface.
19
+
For many real-world scripts, `WithJs()` alone is not enough. AngleSharp.Js exposes interfaces from the AngleSharp services present in the browsing context, so you usually want to include AngleSharp.Io and AngleSharp.Css to provide most of the required API surface.
28
20
29
21
```cs
30
22
varconfiguration=Configuration.Default
@@ -42,12 +34,9 @@ var document = await context.OpenAsync("https://example.com");
42
34
awaitdocument.WaitUntilAvailable();
43
35
```
44
36
45
-
Some highly specialized scripts may also depend on additional AngleSharp packages. For example,
46
-
in niche scenarios, libraries such as AngleSharp.Wasm can be relevant.
37
+
Some highly specialized scripts may also depend on additional AngleSharp packages. For example, in niche scenarios, libraries such as AngleSharp.Wasm can be relevant.
47
38
48
-
`WithJs()` registers the JavaScript scripting service, support for inline event attributes
49
-
such as `onclick`, a navigation handler for `javascript:` URLs, and a default `navigator`
50
-
when the configuration does not already provide one.
39
+
`WithJs()` registers the JavaScript scripting service, support for inline event attributes such as `onclick`, a navigation handler for `javascript:` URLs, structured cloning (for `postMessage` and workers) and the host for custom elements. It does not register a loader, an event loop or a navigator; `WithNavigator()` from AngleSharp.Io provides a full `navigator`.
51
40
52
41
### Customize the event loop
53
42
@@ -93,11 +82,21 @@ var configuration = Configuration.Default
93
82
});
94
83
```
95
84
85
+
### Configure the Jint engine
86
+
87
+
Register an `EngineCreator` to construct the Jint engine yourself, for example to limit the statements, time or memory a script may use. The delegate runs once per window and must build the engine from the supplied options:
HTML `<script>` elements execute while AngleSharp processes the document. For a script you
99
-
want to run yourself, call `ExecuteScript` on the document. The return value is converted to
100
-
the corresponding .NET value where possible.
99
+
HTML `<script>` elements execute while AngleSharp processes the document. For a script you want to run yourself, call `ExecuteScript` on the document. The return value is converted to the corresponding .NET value where possible: numbers become `Double`, DOM objects the AngleSharp objects, `undefined` the string `"undefined"`, and other objects stay Jint values.
@@ -199,7 +198,9 @@ example, adding CSS support also makes its AngleSharp DOM types available to scr
199
198
200
199
In addition to the DOM provided by AngleSharp, the package supplies:
201
200
202
-
-`console.log`, `atob`, `btoa`, `DOMParser`, `Image`, `screen`, and `XMLHttpRequest`.
201
+
-`console`, `atob`, `btoa`, `structuredClone`, `queueMicrotask`, `DOMParser`, `Image`, `Audio`, and `screen`.
202
+
-`requestAnimationFrame`, `requestIdleCallback`, `performance`, `crypto`, `TextEncoder`, and `TextDecoder`.
203
+
-`Worker`, `SharedWorker`, `MessageChannel`, and `window.postMessage`.
203
204
-`javascript:` URL navigation.
204
205
- Inline event-handler attributes and DOM event callbacks.
205
206
- ES modules and import maps through Jint's module loader.
@@ -214,13 +215,13 @@ they meet a script's needs:
214
215
|`DOMParser`|`parseFromString` creates a document through the configured `IDocumentFactory`. The requested MIME type must be supported by that configuration. |
215
216
|`Image`| Creates an AngleSharp `<img>` element; optional width and height become its display dimensions. |
216
217
|`window.postMessage`| Queues a `message` event on the current window. It requires an event loop; it does not transfer objects, deliver to another browsing context, or enforce `targetOrigin`. |
217
-
|`XMLHttpRequest`| Supports `open`, `send`, request headers, status, text responses, and lifecycle events through the configured document loader. |
218
-
|`console`| Supports `console.log` only. |
218
+
|`console`| All methods are available; their arguments (or, for `count`/`time*`, the formatted result) are passed to the `IConsoleLogger`. The severity is not distinguished. |
219
219
|`screen`| Exposes fixed 1920-by-1080 dimensions and 24-bit color depth for compatibility. |
220
220
221
-
To replace the supplied behavior, register your own compatible AngleSharp service before
222
-
calling `WithJs()`. In particular, `WithJs()` preserves an existing `INavigator`, and the
223
-
`WithEventLoop` overloads accept either an existing `IEventLoop` or a factory for one.
221
+
APIs such as `fetch`, `XMLHttpRequest`, `WebSocket` and `localStorage` come from AngleSharp.Io, and `getComputedStyle` or `matchMedia` from AngleSharp.Css. They appear once the respective
222
+
services are registered (e.g. `WithRequesters()` or `WithCss()`).
223
+
224
+
The `WithEventLoop` overloads accept either an existing `IEventLoop` or a factory for one.
224
225
225
226
## Supported behavior and limitations
226
227
@@ -231,15 +232,13 @@ complete browser parity.
231
232
232
233
Notable limitations include:
233
234
234
-
- The package does not calculate layout. Its `scroll*`, `client*`, and `offset*` element
235
-
properties return `0`.
236
-
- The default `navigator` is intentionally minimal. Its platform is empty, registration
237
-
methods are no-ops, and its user-agent value is a fixed compatibility string.
238
-
- Network-backed features such as external scripts and `XMLHttpRequest` require suitable
239
-
AngleSharp requesters and resource loading configuration.
240
-
-`XMLHttpRequest` currently provides text responses only. Its `response`, `responseXML`, and
241
-
`upload` properties are unavailable, `responseType` always has its empty value, and its
242
-
`timeout` and `withCredentials` settings do not affect requests.
243
-
- The JavaScript engine executes application-provided or page-provided code in your process.
244
-
Treat untrusted scripts as untrusted code and apply the constraints appropriate to your
245
-
application.
235
+
- The package does not calculate layout. Its `scroll*`, `client*`, and `offset*` element properties return `0`.
236
+
- Without AngleSharp.Io's `WithNavigator()`, `navigator` is minimal (e.g. an empty `userAgent`).
237
+
- Network-backed features such as external scripts, modules, workers, `fetch` and `XMLHttpRequest` require suitable AngleSharp requesters and resource loading configuration.
238
+
- The JavaScript engine executes application-provided or page-provided code in your process. Treat untrusted scripts as untrusted code and apply the constraints appropriate to your application.
239
+
240
+
## Where to go next
241
+
242
+
-[API Documentation](../tutorials/01-API.md) lists every public type and how .NET types map to JavaScript.
243
+
-[Examples](../tutorials/02-Examples.md) has ready-to-use recipes.
244
+
-[Frequently Asked Questions](../tutorials/03-Questions.md) covers common pitfalls.
0 commit comments