Skip to content

lucacicada/dom-polyfill

Repository files navigation

DOM-Polyfill

DOM for every js runtime!

Mainly for Jint, ClearScript and other pure JS runtimes that do not support even the basic DOM.

Features

It has support for:

  • URL
  • URLSearchParams
  • Headers
  • DOMParser

Usage

Since most of those runtimes to not allow downloading scripts from the internet, you will have to either compile this yourself or download the compiled version from the releases.

You have 2 options to choose from, polyfill the global object, or use the es6 module.

Global Object

DOM.js is a self-invoking function, when imported it set globalThis.URL to a polyfill implementation.

// Jint
engine.ExecuteFile(File.ReadAllText("DOM.min.js"));
// example.js
const url = new URL('http://example.com')
const params = new URLSearchParams('a=1&b=2')
const headers = new Headers()

ES6 Module

DOM.esm.js is an es6 module that exports URL, URLSearchParams, Headers and DOMParser.

// ClearScript
v8.AddSystemDocument("dom", File.ReadAllText("DOM.esm.min.js"));
// example.js
import { URL } from 'dom'

const url = new URL('http://example.com')
const params = new URLSearchParams('a=1&b=2')
const headers = new Headers()

Implementations

Performance

A pure js implementation is not gonna be as fast as a native implementation. This is a cheap way to get a consistent set of APIs across different runtimes for your scripts.

For more complex scenarios, consider embedding Deno, Node or any other JS runtime.

I want MORE!

Here's a gist with MORE (your browser might explode):

About

DOM for every js runtime!

Topics

Resources

License

Stars

Watchers

Forks