这是 实验性技术
检查 浏览器兼容性表格 要小心谨慎在生产中使用这之前。

addModule() 方法在 Worklet interface loads the module in the given JavaScript file and adds it to the current Worklet .

句法

addPromise = worklet.addModule(moduleURL);
addPromise = worklet.addModule(moduleURL, options);
					

参数

moduleURL
字符串 containing the URL of a JavaScript file with the module to add.
选项 可选
An object with any of the following options:
  • credentials : A RequestCredentials value that indicates whether to send credentials (e.g. cookies and HTTP authentification) when loading the module. Can be one of "omit" , "same-origin" ,或 "include" 。默认为 "same-origin" 。另请参阅 Request.credentials .

返回值

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

异常

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

AbortError

The specified script is invalid or could not be loaded.

SyntaxError
指定 moduleURL 无效。

范例

AudioWorklet example

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

PaintWorklet example

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

一旦 paintWorklet is included, the CSS paint() function can be used to include the image created by the worklet:

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

规范

规范 状态 注释
Worklets Level 1
The definition of 'addModule()' in that specification.
工作草案

浏览器兼容性

更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
addModule Chrome 65 Edge 79 Firefox No Disabled
不支持 No Disabled
Available only in Firefox Nightly.
Disabled ). To change preferences in Firefox, visit about:config.
IE No Opera 52 Safari No WebView Android 65 Chrome Android 65 Firefox Android No Disabled
不支持 No Disabled
Available only in Firefox Nightly.
Disabled ). To change preferences in Firefox, visit about:config.
Opera Android 47 Safari iOS No Samsung Internet Android 9.0

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

元数据

  • 最后修改:
  1. Worklet
  2. 方法
    1. addModule()

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1