How to Use the JavaScript Context in Python

How to Use the JavaScript Context in Python

The scripting layer of Aspose.HTML FOSS for Python is explicit and policy-driven: nothing executes during parsing, and when you do evaluate scripts, JSContext runs them with module access controlled by ModuleRegistry and ModuleLoadPolicy. This guide explains the pieces and the failure model, using pip for installation.

Step-by-Step Guide

Step 1: Install the Package

pip install aspose-html-foss

Step 2: Import Required Classes

from aspose_html.js import JSContext, ModuleRegistry, ModuleLoadPolicy

Step 3: Understand the Evaluation Model

JSContext hosts evaluation. Script failures surface as the typed JSEvaluationError on the Python side, keeping script bugs distinct from pipeline bugs.


Step 4: Control Module Access

ModuleRegistry maps module specifiers to implementations — the registry is the sandbox. Missing modules raise the library’s typed ModuleNotFoundError; ModuleLoadPolicy governs what a context may load at all.

Common Issues and Fixes

Evaluation raises ModuleNotFoundError. The script imports a module you have not registered — add it to ModuleRegistry or treat the error as a policy signal.

A script failure crashes the pipeline. Catch JSEvaluationError at the evaluation boundary.

Scripts access more than intended. Tighten ModuleLoadPolicy and shrink the registry — small registries are the point.

Frequently Asked Questions

Do scripts run during parsing?

No. Parsing never executes scripts; evaluation is explicit through JSContext.

How do I sandbox script behaviour?

Register only needed modules in ModuleRegistry and restrict loading with ModuleLoadPolicy.

How do script errors surface in Python?

As JSEvaluationError exceptions.

See Also