大网数据广告活动
Web 标准与前端开发

Worklet: addModule() method

· 52CE 我爱测

The **`addModule()`** method of the Worklet interface loads the module in the given JavaScript file and adds it to the current `Worklet`.

Worklets

The addModule() method of the Worklet interface loads the module in the given JavaScript file and adds it to the current Worklet.

Syntax

addModule(moduleURL)
addModule(moduleURL, options)

Parameters

  • moduleURL
    • : A String containing the URL of a JavaScript file with the module to add.
  • options
    • : An object with any of the following options:
      • credentials
        • : A Request.credentials value that indicates whether to send credentials (e.g., cookies and HTTP authentication) when loading the module. Can be one of "omit", "same-origin", or "include". Defaults to "same-origin". See also Request.credentials.

Return value

A Promise that resolves once the module from the given URL has been added. The promise doesn't return any value.

Exceptions

If addModule() fails, it rejects the promise, delivering one of the following errors to the rejection handler.

  • AbortError DOMException
    • : The specified script is invalid or could not be loaded.
  • SyntaxError DOMException
    • : The specified moduleURL is invalid.

Examples

AudioWorklet example

const audioCtx = new AudioContext();
const audioWorklet = audioCtx.audioWorklet;
audioWorklet.addModule("modules/bypassFilter.js", {
  credentials: "omit",
});

PaintWorklet example

CSS.paintWorklet.addModule(
  "https://mdn.github.io/houdini-examples/cssPaint/intro/worklets/hilite.js",
);

Once the script has been added to the paint worklet, the CSS image/paint function can be used to include the image created by the worklet:

@supports (background-image: paint(id)) {
  h1 {
    background-image: paint(hollow-highlights, filled, 3px);
  }
}

Specifications

Browser compatibility

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。